public static int GenerateCode(SecurityToken securityToken, string modifier = null) { if (securityToken == null) { throw new ArgumentNullException("securityToken"); } // Allow a variance of no greater than 90 seconds in either direction var currentTimeStep = GetCurrentTimeStepNumber(); using (var hashAlgorithm = new HMACSHA1(securityToken.GetDataNoClone())) { return ComputeTotp(hashAlgorithm, currentTimeStep, modifier); } }
public static bool ValidateCode(SecurityToken securityToken, int code, string modifier = null) { if (securityToken == null) { throw new ArgumentNullException("securityToken"); } // Allow a variance of no greater than 90 seconds in either direction var currentTimeStep = GetCurrentTimeStepNumber(); using (var hashAlgorithm = new HMACSHA1(securityToken.GetDataNoClone())) { for (var i = -2; i <= 2; i++) { var computedTotp = ComputeTotp(hashAlgorithm, (ulong) ((long) currentTimeStep + i), modifier); if (computedTotp == code) { return true; } } } // No match return false; }