public int GetCode(HashAlgorithmType hashAlgorithm, byte[] secret, byte numberOfDigits, byte periodInSeconds) { //https://tools.ietf.org/html/rfc4226#section-5.4 //https://tools.ietf.org/html/rfc6238#section-4.2 secret.CheckArgumentNullOrEmpty(nameof(secret)); if (numberOfDigits < 6 || numberOfDigits > 8) { throw new ArgumentException(Resources.Exception_InvalidNumberOfDigits); } if (periodInSeconds < 30) { throw new ArgumentException(Resources.Exception_InvalidPeriodInSeconds); } var deltaTime = _systemTime.GetUtcNow() - UnixEpoch; var counter = ConvertToBytes((ulong)(deltaTime.TotalSeconds / periodInSeconds)); byte[] hash; using (var hmacAlgorithm = GetHmac(hashAlgorithm)) { hmacAlgorithm.Key = secret; hash = hmacAlgorithm.ComputeHash(counter); } int offset = hash[hash.Length - 1] & 0xf; int code = (hash[offset] & 0x7f) << 24 | (hash[offset + 1] & 0xff) << 16 | (hash[offset + 2] & 0xff) << 8 | (hash[offset + 3] & 0xff); return(code % TenPow[numberOfDigits]); }
private static ICollection <KeyValuePair <string, string> > GetHmacSha1Parameters(string consumerToken, INonceCreator nonceCreator, ISystemTime systemTime, Dictionary <string, string?>?additionalParameters = null) => GetHmacSha1Parameters(consumerToken, nonceCreator.CreateNonce(), DateTimeUtility.ToUnixTimestamp(systemTime.GetUtcNow()).ToInvariantString(), additionalParameters);
public Customer RegisterCustomer(string name) { return(new Customer(_idGenerator.NewId(), _systemTime.GetUtcNow(), name)); }