diff options
author | Stephen Jennings <Stephen.G.Jennings@gmail.com> | 2011-10-19 22:13:06 -0700 |
---|---|---|
committer | Stephen Jennings <Stephen.G.Jennings@gmail.com> | 2011-10-19 22:14:18 -0700 |
commit | 63a47f29e3b791e06c1a9de7a6001c36c5f83f9b (patch) | |
tree | aa1625d31cbc2b50f8b1060fac4e99c632c955f6 /OATH.Net.Test/TimeBasedOtpGeneratorTests.cs | |
parent | f2a9cf7f37f6c19de29879d404d3d8b58e1906a0 (diff) | |
download | OATH.Net-63a47f29e3b791e06c1a9de7a6001c36c5f83f9b.zip OATH.Net-63a47f29e3b791e06c1a9de7a6001c36c5f83f9b.tar.gz OATH.Net-63a47f29e3b791e06c1a9de7a6001c36c5f83f9b.tar.bz2 |
Add SHA256 and SHA512.
Diffstat (limited to 'OATH.Net.Test/TimeBasedOtpGeneratorTests.cs')
-rw-r--r-- | OATH.Net.Test/TimeBasedOtpGeneratorTests.cs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/OATH.Net.Test/TimeBasedOtpGeneratorTests.cs b/OATH.Net.Test/TimeBasedOtpGeneratorTests.cs index ae9f0ad..ede372a 100644 --- a/OATH.Net.Test/TimeBasedOtpGeneratorTests.cs +++ b/OATH.Net.Test/TimeBasedOtpGeneratorTests.cs @@ -90,6 +90,73 @@ namespace OathNet.Test } [Test] + public void GenerateOtp_returns_SHA256_reference_results_with_bytearray_key() + { + var key = new byte[] + { + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, + 0x31, 0x32 + }; + + this.TestSHA256AndAssert(key, 8, new DateTime(1970, 1, 1, 0, 0, 59, DateTimeKind.Utc), "46119246"); + this.TestSHA256AndAssert(key, 8, new DateTime(2005, 3, 18, 1, 58, 29, DateTimeKind.Utc), "68084774"); + this.TestSHA256AndAssert(key, 8, new DateTime(2005, 3, 18, 1, 58, 31, DateTimeKind.Utc), "67062674"); + this.TestSHA256AndAssert(key, 8, new DateTime(2009, 2, 13, 23, 31, 30, DateTimeKind.Utc), "91819424"); + this.TestSHA256AndAssert(key, 8, new DateTime(2033, 5, 18, 3, 33, 20, DateTimeKind.Utc), "90698825"); + this.TestSHA256AndAssert(key, 8, new DateTime(2603, 10, 11, 11, 33, 20, DateTimeKind.Utc), "77737706"); + } + + [Test] + public void GenerateOtp_returns_SHA256_reference_results_with_string_key() + { + var key = "3132333435363738393031323334353637383930" + "313233343536373839303132"; + + this.TestSHA256AndAssert(key, 8, new DateTime(1970, 1, 1, 0, 0, 59, DateTimeKind.Utc), "46119246"); + this.TestSHA256AndAssert(key, 8, new DateTime(2005, 3, 18, 1, 58, 29, DateTimeKind.Utc), "68084774"); + this.TestSHA256AndAssert(key, 8, new DateTime(2005, 3, 18, 1, 58, 31, DateTimeKind.Utc), "67062674"); + this.TestSHA256AndAssert(key, 8, new DateTime(2009, 2, 13, 23, 31, 30, DateTimeKind.Utc), "91819424"); + this.TestSHA256AndAssert(key, 8, new DateTime(2033, 5, 18, 3, 33, 20, DateTimeKind.Utc), "90698825"); + this.TestSHA256AndAssert(key, 8, new DateTime(2603, 10, 11, 11, 33, 20, DateTimeKind.Utc), "77737706"); + } + + [Test] + public void GenerateOtp_returns_SHA512_reference_results_with_bytearray_key() + { + var key = new byte[] + { + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, + 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, + 0x31, 0x32, 0x33, 0x34 + }; + + this.TestSHA512AndAssert(key, 8, new DateTime(1970, 1, 1, 0, 0, 59, DateTimeKind.Utc), "90693936"); + this.TestSHA512AndAssert(key, 8, new DateTime(2005, 3, 18, 1, 58, 29, DateTimeKind.Utc), "25091201"); + this.TestSHA512AndAssert(key, 8, new DateTime(2005, 3, 18, 1, 58, 31, DateTimeKind.Utc), "99943326"); + this.TestSHA512AndAssert(key, 8, new DateTime(2009, 2, 13, 23, 31, 30, DateTimeKind.Utc), "93441116"); + this.TestSHA512AndAssert(key, 8, new DateTime(2033, 5, 18, 3, 33, 20, DateTimeKind.Utc), "38618901"); + this.TestSHA512AndAssert(key, 8, new DateTime(2603, 10, 11, 11, 33, 20, DateTimeKind.Utc), "47863826"); + } + + [Test] + public void GenerateOtp_returns_SHA512_reference_results_with_string_key() + { + var key = "3132333435363738393031323334353637383930" + "3132333435363738393031323334353637383930" + "3132333435363738393031323334353637383930" + "31323334"; + + this.TestSHA512AndAssert(key, 8, new DateTime(1970, 1, 1, 0, 0, 59, DateTimeKind.Utc), "90693936"); + this.TestSHA512AndAssert(key, 8, new DateTime(2005, 3, 18, 1, 58, 29, DateTimeKind.Utc), "25091201"); + this.TestSHA512AndAssert(key, 8, new DateTime(2005, 3, 18, 1, 58, 31, DateTimeKind.Utc), "99943326"); + this.TestSHA512AndAssert(key, 8, new DateTime(2009, 2, 13, 23, 31, 30, DateTimeKind.Utc), "93441116"); + this.TestSHA512AndAssert(key, 8, new DateTime(2033, 5, 18, 3, 33, 20, DateTimeKind.Utc), "38618901"); + this.TestSHA512AndAssert(key, 8, new DateTime(2603, 10, 11, 11, 33, 20, DateTimeKind.Utc), "47863826"); + } + + [Test] public void GenerateOtp_test_with_Google_Authenticator_1() { var key = "48656C6C6F21DEADBEEF"; // Base-32: JBSWY3DPEHPK3PXP @@ -130,5 +197,33 @@ namespace OathNet.Test var result = otp.GenerateOtp(time); Assert.AreEqual(expected, result); } + + private void TestSHA256AndAssert(byte[] key, int digits, DateTime time, string expected) + { + var otp = new TimeBasedOtpGenerator(key, digits, new SHA256HMACAlgorithm()); + var result = otp.GenerateOtp(time); + Assert.AreEqual(expected, result); + } + + private void TestSHA256AndAssert(string key, int digits, DateTime time, string expected) + { + var otp = new TimeBasedOtpGenerator(key, digits, new SHA256HMACAlgorithm()); + var result = otp.GenerateOtp(time); + Assert.AreEqual(expected, result); + } + + private void TestSHA512AndAssert(byte[] key, int digits, DateTime time, string expected) + { + var otp = new TimeBasedOtpGenerator(key, digits, new SHA512HMACAlgorithm()); + var result = otp.GenerateOtp(time); + Assert.AreEqual(expected, result); + } + + private void TestSHA512AndAssert(string key, int digits, DateTime time, string expected) + { + var otp = new TimeBasedOtpGenerator(key, digits, new SHA512HMACAlgorithm()); + var result = otp.GenerateOtp(time); + Assert.AreEqual(expected, result); + } } } |