Represents a secret key used for the one-time password generation.
        public void GenerateOtp_test_with_Google_Authenticator_1()
        {
            var keyData = new byte[] // Base-32: 32W3532IMVWGY3ZB
            {
                0xDE, 0xAD, 0xBE, 0xEF, 0x48,
                0x65, 0x6C, 0x6C, 0x6F, 0x21
            };
            var key = new Key(keyData);

            this.TestSHA1AndAssert(key, 6, 1, "092093");
            this.TestSHA1AndAssert(key, 6, 11, "266262");
        }
        public void GenerateOtp_returns_SHA1_reference_values_with_bytearray_key()
        {
            var keyData = new byte[]
            {
                0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
                0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30
            };
            var key = new Key(keyData);

            this.TestSHA1AndAssert(key, 6, 0, "755224");
            this.TestSHA1AndAssert(key, 6, 1, "287082");
            this.TestSHA1AndAssert(key, 6, 2, "359152");
            this.TestSHA1AndAssert(key, 6, 3, "969429");
            this.TestSHA1AndAssert(key, 6, 4, "338314");
            this.TestSHA1AndAssert(key, 6, 5, "254676");
            this.TestSHA1AndAssert(key, 6, 6, "287922");
            this.TestSHA1AndAssert(key, 6, 7, "162583");
            this.TestSHA1AndAssert(key, 6, 8, "399871");
            this.TestSHA1AndAssert(key, 6, 9, "520489");
        }
        public void GenerateOtp_without_hmac_returns_SHA1_with_bytearray_key()
        {
            var keyData = new byte[]
            {
                0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
                0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30
            };
            var key = new Key(keyData);

            this.TestSHA1AndAssert(key, 6, 0, this.GetOtpWithImplicitHMAC(key, 6, 0));
            this.TestSHA1AndAssert(key, 6, 1, this.GetOtpWithImplicitHMAC(key, 6, 1));
            this.TestSHA1AndAssert(key, 6, 2, this.GetOtpWithImplicitHMAC(key, 6, 2));
            this.TestSHA1AndAssert(key, 6, 3, this.GetOtpWithImplicitHMAC(key, 6, 3));
            this.TestSHA1AndAssert(key, 6, 4, this.GetOtpWithImplicitHMAC(key, 6, 4));
            this.TestSHA1AndAssert(key, 6, 5, this.GetOtpWithImplicitHMAC(key, 6, 5));
            this.TestSHA1AndAssert(key, 6, 6, this.GetOtpWithImplicitHMAC(key, 6, 6));
            this.TestSHA1AndAssert(key, 6, 7, this.GetOtpWithImplicitHMAC(key, 6, 7));
            this.TestSHA1AndAssert(key, 6, 8, this.GetOtpWithImplicitHMAC(key, 6, 8));
            this.TestSHA1AndAssert(key, 6, 9, this.GetOtpWithImplicitHMAC(key, 6, 9));
        }
 private void TestSHA1AndAssert(Key key, int digits, int counter, string expected)
 {
     var otp = new CounterBasedOtpGenerator(key, digits, new SHA1HMACAlgorithm());
     var result = otp.GenerateOtp(counter);
     Assert.AreEqual(expected, result);
 }
 private string GetOtpWithImplicitHMAC(Key key, int digits, int counter)
 {
     var otp = new CounterBasedOtpGenerator(key, digits);
     return otp.GenerateOtp(counter);
 }
 private void TestSHA512AndAssert(Key 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 string GetOtpWithImplicitHMAC(Key key, int digits, DateTime time)
 {
     var otp = new TimeBasedOtpGenerator(key, digits);
     return otp.GenerateOtp(time);
 }
        public void GenerateOtp_test_with_Google_Authenticator_2()
        {
            var keyData = new byte[] // Base-32: 32W3532IMVWGY3ZB
            {
                0xDE, 0xAD, 0xBE, 0xEF, 0x48,
                0x65, 0x6C, 0x6C, 0x6F, 0x21
            };
            var key = new Key(keyData);

            this.TestSHA1AndAssert(key, 6, new DateTime(2011, 10, 17, 7, 52, 0, DateTimeKind.Utc), "139594");
        }
        public void GenerateOtp_test_with_Google_Authenticator_1()
        {
            var keyData = new byte[] // Base-32: JBSWY3DPEHPK3PXP
            {
                0x48, 0x65, 0x6C, 0x6C, 0x6F,
                0x21, 0xDE, 0xAD, 0xBE, 0xEF
            };
            var key = new Key(keyData);

            this.TestSHA1AndAssert(key, 6, new DateTime(2011, 10, 17, 7, 49, 45, DateTimeKind.Utc), "010374");
        }
 /// <summary>
 ///     Initializes a new instance of the TimeBasedOtpGenerator class. This
 ///     is used when the client and server do not share a counter
 ///     value but the clocks between the two are synchronized within
 ///     reasonable margins of each other.
 /// </summary>
 /// <param name="secretKey">The secret key.</param>
 /// <param name="otpLength">The number of digits in the OTP to generate.</param>
 public TimeBasedOtpGenerator(Key secretKey, int otpLength)
     : this(secretKey, otpLength, new SHA1HMACAlgorithm())
 {
 }
 /// <summary>
 ///     Initializes a new instance of the TimeBasedOtpGenerator class. This
 ///     is used when the client and server do not share a counter
 ///     value but the clocks between the two are synchronized within
 ///     reasonable margins of each other.
 /// </summary>
 /// <param name="secretKey">The secret key.</param>
 /// <param name="otpLength">The number of digits in the OTP to generate.</param>
 /// <param name="hmacAlgorithm">The HMAC algorithm to use.</param>
 public TimeBasedOtpGenerator(Key secretKey, int otpLength, IHMACAlgorithm hmacAlgorithm)
 {
     this.counterOtp = new CounterBasedOtpGenerator(secretKey, otpLength, hmacAlgorithm);
 }
 /// <summary>
 ///     Initializes a new instance of the CounterBasedOtpGenerator class.
 ///     This is used when the client and server share a counter value.
 /// </summary>
 /// <param name="secretKey">The secret key.</param>
 /// <param name="otpLength">The number of digits in the OTP to generate.</param>
 /// <param name="hmacAlgorithm">The hashing algorithm to use.</param>
 public CounterBasedOtpGenerator(Key secretKey, int otpLength, IHMACAlgorithm hmacAlgorithm)
 {
     this.secretKey = secretKey;
     this.otpLength = otpLength;
     this.hmacAlgorithm = hmacAlgorithm;
 }