/// <inheritdoc/> public string GeneratePassword(byte[] key, long counter, IPasswordLength length) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (counter < 0) { throw new ArgumentOutOfRangeException( nameof(counter), "A positive value must be supplied for the counter."); } if (length.Digits < 0 || length.Digits > 8) { throw new ArgumentOutOfRangeException( nameof(length.Digits), "Only passwords of between 1 and 8 digits in length can be generated."); } using (var hmac = new HMACSHA1()) { return(GeneratePassword(key, counter, length, hmac)); } }
public string GeneratePassword(byte[] key, int step, IPasswordLength length, HMAC hmac) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (step < 1) { throw new ArgumentOutOfRangeException(nameof(step), "A positive value must be supplied for step."); } if (length.Digits < 0 || length.Digits > 8) { throw new ArgumentOutOfRangeException( nameof(length.Digits), "Only passwords of between 1 and 8 digits in length can be generated."); } if (hmac == null) { throw new ArgumentNullException(nameof(hmac)); } return(this.GeneratePassword(key, DateTime.UtcNow, step, length, hmac)); }
public void APassword_OfTheCorrectNumberOfDigits_IsGenerated(long counter, IPasswordLength length, int expectedLength) { // Arrange var secret = Encoding.UTF8.GetBytes("000000000000"); var otpGenerator = new HotpGenerator(); // Act var password = otpGenerator.GeneratePassword(secret, counter, length); // Assert Assert.Equal(password.Length, expectedLength); }
public string GeneratePassword(byte[] key, IPasswordLength length, HMAC hmac) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (length.Digits < 0 || length.Digits > 8) { throw new ArgumentOutOfRangeException( nameof(length.Digits), "Only passwords of between 1 and 8 digits in length can be generated."); } if (hmac == null) { throw new ArgumentNullException(nameof(hmac)); } TimeSpan span = (DateTime.Now.ToUniversalTime() - Epoch); return(this.GeneratePassword(key, DateTime.UtcNow, 60, length, hmac)); }
internal string GeneratePassword(byte[] key, long counter, IPasswordLength length, HMAC hmac) { var text = BitConverter.GetBytes(counter); if (!BitConverter.IsLittleEndian) { Array.Reverse(text); } Array.Resize(ref text, 8); Array.Reverse(text); hmac.Key = key; var hash = hmac.ComputeHash(text); int offset = hash[hash.Length - 1] & 0xf; int binary = (hash[offset] & 0x7f) << 24 | (hash[offset + 1] & 0xff) << 16 | (hash[offset + 2] & 0xff) << 8 | (hash[offset + 3] & 0xff); int password = binary % length.Power; return(password.ToString(length.Format)); }
internal string GeneratePassword(byte[] key, DateTime date, int step, IPasswordLength length, HMAC hmac) { TimeSpan span = (date.ToUniversalTime() - Epoch); return(Otp.Hotp.GeneratePassword(key, (long)span.TotalSeconds / step, length, hmac)); }