summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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>