summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Jennings <Stephen.G.Jennings@gmail.com>2011-11-13 14:05:19 -0800
committerStephen Jennings <Stephen.G.Jennings@gmail.com>2011-11-13 14:05:19 -0800
commiteb6884e45abfa6a79c0e20d1e8c298a881e2b6d7 (patch)
tree410b6165b1ee9d6662d83610154d81446f03760e
parent5cd2d78c4a0085bdafc1292ff21992e9621a4a1a (diff)
downloadOATH.Net-eb6884e45abfa6a79c0e20d1e8c298a881e2b6d7.zip
OATH.Net-eb6884e45abfa6a79c0e20d1e8c298a881e2b6d7.tar.gz
OATH.Net-eb6884e45abfa6a79c0e20d1e8c298a881e2b6d7.tar.bz2
Fix automatic key generation.
-rw-r--r--OATH.Net.Test/KeyTests.cs22
-rw-r--r--OATH.Net/Key.cs13
2 files changed, 34 insertions, 1 deletions
diff --git a/OATH.Net.Test/KeyTests.cs b/OATH.Net.Test/KeyTests.cs
index 7ddf93a..c5e8381 100644
--- a/OATH.Net.Test/KeyTests.cs
+++ b/OATH.Net.Test/KeyTests.cs
@@ -125,5 +125,27 @@ namespace OathNet.Test
Assert.AreEqual(expected, actual);
}
+
+ [Test]
+ public void Key_created_with_seed_returns_expected_key_data_1()
+ {
+ var seed = 870273;
+ var key = new Key(10, seed);
+ var actual = key.Base32;
+ var expected = "YLFDZHEU5CHZ3KDB";
+
+ Assert.AreEqual(expected, actual);
+ }
+
+ [Test]
+ public void Key_created_with_seed_returns_expected_key_data_2()
+ {
+ var seed = 20572632;
+ var key = new Key(8, seed);
+ var actual = key.Base32;
+ var expected = "OI7GKIQ7K63GS===";
+
+ Assert.AreEqual(expected, actual);
+ }
}
}
diff --git a/OATH.Net/Key.cs b/OATH.Net/Key.cs
index 66a98d7..7adb754 100644
--- a/OATH.Net/Key.cs
+++ b/OATH.Net/Key.cs
@@ -19,13 +19,24 @@ namespace OathNet
private byte[] keyData;
/// <summary>
- /// Initializes a new instance of the Key class and generates a random key.
+ /// Initializes a new instance of the Key class and generates a random 10-byte key.
/// </summary>
public Key()
+ : this(10, (new Random()).Next())
{
}
/// <summary>
+ /// Initializes a new instance of the Key class and generates a random key with the specified seed.
+ /// </summary>
+ public Key(int keyLength, int seed)
+ {
+ this.keyData = new byte[keyLength];
+ var gen = new Random(seed);
+ gen.NextBytes(this.keyData);
+ }
+
+ /// <summary>
/// Initializes a new instance of the Key class.
/// </summary>
/// <param name="data">The key to initialize.</param>