public void Generate_YieldsExpectedResults() { //Values taken from RFC specification byte[] secret = Encoding.ASCII.GetBytes("12345678901234567890"); //Item1 = secretKey, //Item2 = counter, //Item3 = expected result. List <Tuple <byte[], uint, uint> > testValues = new List <Tuple <byte[], uint, uint> >() { new Tuple <byte[], uint, uint>(secret, 0, 755224), new Tuple <byte[], uint, uint>(secret, 1, 287082), new Tuple <byte[], uint, uint>(secret, 2, 359152), new Tuple <byte[], uint, uint>(secret, 3, 969429), new Tuple <byte[], uint, uint>(secret, 4, 338314), new Tuple <byte[], uint, uint>(secret, 5, 254676), new Tuple <byte[], uint, uint>(secret, 6, 287922), new Tuple <byte[], uint, uint>(secret, 7, 162583), new Tuple <byte[], uint, uint>(secret, 8, 399871), new Tuple <byte[], uint, uint>(secret, 9, 520489), }; foreach (var testValue in testValues) { string result = HmacOneTimePassword.Generate(testValue.Item1, testValue.Item2, new HMACSHA1(), OneTimePasswordLength.SixDigits); Assert.AreEqual(result, testValue.Item3.ToString()); } }
public void Generate_DefaultsToHmacSha1Algorithm() { byte[] secretKey = new byte[] { 0 }; string result1 = HmacOneTimePassword.Generate(secretKey, 1, new HMACSHA1()); string result2 = HmacOneTimePassword.Generate(secretKey, 1); Assert.AreEqual(result1, result2); }
/// <summary> /// Generates a new one time password from the supplied data. /// </summary> /// <param name="secretKey">The secret key to use in the HMAC</param> /// <param name="hmac">The hmac algorithm to use</param> /// <param name="dt">The date and time to generate a code for</param> /// <param name="offset">Any offsets that should be applied to the supplie date time</param> /// <param name="timeStep">The timestep value to use to calculate the current step</param> /// <param name="otpLength">The required legnth of the returned passcode</param> /// <returns>A one time password code</returns> /// <exception cref="System.ArgumentNullException">Thrown if hmac or secret key is null</exception> /// <exception cref="System.ArgumentException">Thrown if secret key is empty, optLength is /// not defined value or timeStep is less than 1 second.</exception> public static string Generate(byte[] secretKey, HMAC hmac, DateTime dt, TimeSpan offset, TimeSpan timeStep, OneTimePasswordLength otpLength) { #region Input validation Insist.IsNotNull(hmac, "hmac"); Insist.IsNotNull(secretKey, "secretKey"); Insist.IsNotEmpty(secretKey, "secretKey"); Insist.IsDefined <OneTimePasswordLength>(otpLength, "optLength"); Insist.IsAtLeast(timeStep.TotalSeconds, 1, "timeStep"); #endregion dt = dt + offset; ulong stepNumber = (ulong)Math.Floor((double)dt.ToUnixTime() / (double)timeStep.TotalSeconds); return(HmacOneTimePassword.Generate(secretKey, stepNumber, hmac, otpLength)); }
public void Generate_UndefinedHOTPLengthThrowsException() { HmacOneTimePassword.Generate(new byte[] { 0 }, 0, new HMACSHA1(), 0); }
public void Generate_EmptySecretKeyThrowsException() { HmacOneTimePassword.Generate(new byte[0], 0, new HMACSHA1()); }
public void Generate_NullSecretKeyThrowsException() { HmacOneTimePassword.Generate(null, 0, new HMACSHA1()); }
public void Generate_NullHmacThrowsException() { HmacOneTimePassword.Generate(new byte[] { 0 }, 0, null); }
public void Generate_CanGenerateEightDigitCodes() { string result = HmacOneTimePassword.Generate(new byte[] { 0 }, 1, new HMACSHA1(), OneTimePasswordLength.EightDigits); Assert.IsTrue(result.Length == 8); }
public void Generate_DefaultsToSixDigitCode() { string result = HmacOneTimePassword.Generate(new byte[] { 0 }, 1); Assert.IsTrue(result.Length == 6); }