diff options
author | Stephen Jennings <Stephen.G.Jennings@gmail.com> | 2011-11-13 15:15:30 -0800 |
---|---|---|
committer | Stephen Jennings <Stephen.G.Jennings@gmail.com> | 2011-11-13 15:15:30 -0800 |
commit | 8500067fa96d9e2e387ab5d26ad5c176894b8b9a (patch) | |
tree | ad91584cb60b763820cd6e30ac5bc955a168d54f | |
parent | eb6884e45abfa6a79c0e20d1e8c298a881e2b6d7 (diff) | |
download | OATH.Net-8500067fa96d9e2e387ab5d26ad5c176894b8b9a.zip OATH.Net-8500067fa96d9e2e387ab5d26ad5c176894b8b9a.tar.gz OATH.Net-8500067fa96d9e2e387ab5d26ad5c176894b8b9a.tar.bz2 |
Verify that ToBase32 and Key(string) throw on invalid arguments.
-rw-r--r-- | OATH.Net.Test/Base32Tests.cs | 18 | ||||
-rw-r--r-- | OATH.Net.Test/KeyTests.cs | 18 | ||||
-rw-r--r-- | OATH.Net/Base32.cs | 31 | ||||
-rw-r--r-- | OATH.Net/Key.cs | 3 |
4 files changed, 51 insertions, 19 deletions
diff --git a/OATH.Net.Test/Base32Tests.cs b/OATH.Net.Test/Base32Tests.cs index 6f928af..fe89283 100644 --- a/OATH.Net.Test/Base32Tests.cs +++ b/OATH.Net.Test/Base32Tests.cs @@ -293,5 +293,23 @@ namespace OathNet.Test Assert.AreEqual(expected, actual); } + + [Test] + public void ToBinary_with_invalid_string_throws_ArgumentException() + { + var invalidChars = new List<string>() + { + "1", "8", "9", "0", + "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", + "[", "]", "{", "}", "|", "\\", + ";", ":", "'", "\"", + ",", ".", "<", ">", "/", "?" + }; + + foreach (var s in invalidChars) + { + Assert.Throws<ArgumentException>(() => new Key("ABCD" + s + "EFG"), "'" + s + "' is not part of the alphabet"); + } + } } } diff --git a/OATH.Net.Test/KeyTests.cs b/OATH.Net.Test/KeyTests.cs index c5e8381..5c12947 100644 --- a/OATH.Net.Test/KeyTests.cs +++ b/OATH.Net.Test/KeyTests.cs @@ -147,5 +147,23 @@ namespace OathNet.Test Assert.AreEqual(expected, actual); } + + [Test] + public void Key_created_with_invalid_string_throws_ArgumentException() + { + var invalidChars = new List<string>() + { + "1", "8", "9", "0", + "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", + "[", "]", "{", "}", "|", "\\", + ";", ":", "'", "\"", + ",", ".", "<", ">", "/", "?" + }; + + foreach (var s in invalidChars) + { + Assert.Throws<ArgumentException>(() => new Key("ABCD" + s + "EFG"), "'" + s + "' is not part of the alphabet"); + } + } } } diff --git a/OATH.Net/Base32.cs b/OATH.Net/Base32.cs index 0e0fb3c..fec5c79 100644 --- a/OATH.Net/Base32.cs +++ b/OATH.Net/Base32.cs @@ -17,20 +17,12 @@ namespace OathNet /// </summary> public static class Base32 { - private static readonly string[] Alphabet = new string[] + private static readonly char[] Alphabet = new char[] { - "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", - "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", - "U", "V", "W", "X", "Y", "Z", "2", "3", "4", "5", - "6", "7" - }; - - private static readonly Dictionary<char, byte> AlphabetReverse = new Dictionary<char, byte> - { - { 'A', 0 }, { 'B', 1 }, { 'C', 2 }, { 'D', 3 }, { 'E', 4 }, { 'F', 5 }, { 'G', 6 }, { 'H', 7 }, { 'I', 8 }, { 'J', 9 }, - { 'K', 10 }, { 'L', 11 }, { 'M', 12 }, { 'N', 13 }, { 'O', 14 }, { 'P', 15 }, { 'Q', 16 }, { 'R', 17 }, { 'S', 18 }, { 'T', 19 }, - { 'U', 20 }, { 'V', 21 }, { 'W', 22 }, { 'X', 23 }, { 'Y', 24 }, { 'Z', 25 }, { '2', 26 }, { '3', 27 }, { '4', 28 }, { '5', 29 }, - { '6', 30 }, { '7', 31 } + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', + 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', + '6', '7' }; private static readonly char Padding = '='; @@ -62,11 +54,12 @@ namespace OathNet /// </summary> /// <param name="base32">A base-32 encoded string.</param> /// <returns>The data represented by the base-32 string.</returns> + /// <exception cref="ArgumentException">The argument is not a valid base32-encoded string.</exception> public static byte[] ToBinary(string base32) { base32 = base32.ToUpper(); - if (base32.Any(c => !AlphabetReverse.ContainsKey(c) && c != Padding)) + if (base32.Any(c => !Alphabet.Contains(c) && c != Padding)) { throw new ArgumentException("String contains invalid characters."); } @@ -111,7 +104,7 @@ namespace OathNet { case 8: resized = true; - result[4] = (byte)(AlphabetReverse[s[6]] << 5 | AlphabetReverse[s[7]]); + result[4] = (byte)(Array.IndexOf(Alphabet, s[6]) << 5 | Array.IndexOf(Alphabet, s[7])); goto case 7; case 7: if (!resized) @@ -120,7 +113,7 @@ namespace OathNet resized = true; } - result[3] = (byte)(AlphabetReverse[s[4]] << 7 | AlphabetReverse[s[5]] << 2 | AlphabetReverse[s[6]] >> 3); + result[3] = (byte)(Array.IndexOf(Alphabet, s[4]) << 7 | Array.IndexOf(Alphabet, s[5]) << 2 | Array.IndexOf(Alphabet, s[6]) >> 3); goto case 5; case 5: if (!resized) @@ -129,7 +122,7 @@ namespace OathNet resized = true; } - result[2] = (byte)(AlphabetReverse[s[3]] << 4 | AlphabetReverse[s[4]] >> 1); + result[2] = (byte)(Array.IndexOf(Alphabet, s[3]) << 4 | Array.IndexOf(Alphabet, s[4]) >> 1); goto case 4; case 4: if (!resized) @@ -138,7 +131,7 @@ namespace OathNet resized = true; } - result[1] = (byte)(AlphabetReverse[s[1]] << 6 | AlphabetReverse[s[2]] << 1 | AlphabetReverse[s[3]] >> 4); + result[1] = (byte)(Array.IndexOf(Alphabet, s[1]) << 6 | Array.IndexOf(Alphabet, s[2]) << 1 | Array.IndexOf(Alphabet, s[3]) >> 4); goto case 2; case 2: if (!resized) @@ -147,7 +140,7 @@ namespace OathNet resized = true; } - result[0] = (byte)(AlphabetReverse[s[0]] << 3 | AlphabetReverse[s[1]] >> 2); + result[0] = (byte)(Array.IndexOf(Alphabet, s[0]) << 3 | Array.IndexOf(Alphabet, s[1]) >> 2); break; default: throw new ArgumentException("Segment is not a valid 8 character block of base32."); diff --git a/OATH.Net/Key.cs b/OATH.Net/Key.cs index 7adb754..c228c1d 100644 --- a/OATH.Net/Key.cs +++ b/OATH.Net/Key.cs @@ -29,6 +29,8 @@ namespace OathNet /// <summary> /// Initializes a new instance of the Key class and generates a random key with the specified seed. /// </summary> + /// <param name="keyLength">Length in bytes of the generated key.</param> + /// <param name="seed">A seed to use for the random key generation.</param> public Key(int keyLength, int seed) { this.keyData = new byte[keyLength]; @@ -49,6 +51,7 @@ namespace OathNet /// Initializes a new instance of the Key class. /// </summary> /// <param name="base32key">The key to initialize.</param> + /// <exception cref="ArgumentException">base32key is not a valid base32-encoded string.</exception> public Key(string base32key) { this.keyData = OathNet.Base32.ToBinary(base32key); |