Albireo.Otp.Otp.GetCode C# (CSharp) Method

GetCode() static private method

static private GetCode ( HashAlgorithm algorithm, string secret, long counter, int digits ) : int
algorithm System.Security.Cryptography.HashAlgorithm
secret string
counter long
digits int
return int
        internal static int GetCode(
            HashAlgorithm algorithm,
            string secret,
            long counter,
            int digits)
        {
            Contract.Requires<ArgumentOutOfRangeException>(Enum.IsDefined(typeof(HashAlgorithm), algorithm));
            Contract.Requires<ArgumentOutOfRangeException>(algorithm != HashAlgorithm.Unknown);
            Contract.Requires<ArgumentNullException>(secret != null);
            Contract.Requires<ArgumentOutOfRangeException>(counter >= 0);
            Contract.Requires<ArgumentOutOfRangeException>(digits > 0);
            Contract.Ensures(Contract.Result<int>() > 0);
            Contract.Ensures(Contract.Result<int>() < Math.Pow(10, digits));

            var generator = HMAC.Create(algorithm.ToAlgorithmName());

            generator.Key = Encoding.ASCII.GetBytes(secret);
            generator.ComputeHash(CounterToBytes(counter));

            var hmac =
                generator
                .Hash
                .Select(b => Convert.ToInt32(b))
                .ToArray();

            var offset = hmac[19] & 0xF;

            var code =
                (hmac[offset + 0] & 0x7F) << 24
                | (hmac[offset + 1] & 0xFF) << 16
                | (hmac[offset + 2] & 0xFF) << 8
                | (hmac[offset + 3] & 0xFF);

            return code % (int) Math.Pow(10, digits);
        }

Usage Example

Esempio n. 1
0
        /// <summary>Compute the one-time code for the given parameters.</summary>
        /// <param name="algorithm">The hashing algorithm for the HMAC computation.</param>
        /// <param name="secret">The ASCII-encoded base32-encoded shared secret.</param>
        /// <param name="datetime">The date with time for which the one-time code must be computed.</param>
        /// <param name="digits">The number of digits of the one-time codes.</param>
        /// <param name="period">The period step used for the HMAC counter computation.</param>
        /// <returns>The one-time code for the given date.</returns>
        public static int GetCode(
            HashAlgorithm algorithm,
            string secret,
            DateTime datetime,
            int digits = Otp.DefaultDigits,
            int period = Totp.DefaultPeriod)
        {
            Contract.Requires <ArgumentOutOfRangeException>(Enum.IsDefined(typeof(HashAlgorithm), algorithm));
            Contract.Requires <ArgumentOutOfRangeException>(algorithm != HashAlgorithm.Unknown);
            Contract.Requires <ArgumentNullException>(secret != null);
            Contract.Requires <ArgumentNullException>(datetime != null);
            Contract.Requires <ArgumentException>(Enum.IsDefined(typeof(DateTimeKind), datetime.Kind));
            Contract.Requires <ArgumentException>(datetime.Kind != DateTimeKind.Unspecified);
            Contract.Requires <ArgumentOutOfRangeException>(digits > 0);
            Contract.Requires <ArgumentOutOfRangeException>(period > 0);
            Contract.Ensures(Contract.Result <int>() > 0);
            Contract.Ensures(Contract.Result <int>() < Math.Pow(10, digits));

            datetime = datetime.Kind == DateTimeKind.Utc ? datetime : datetime.ToUniversalTime();

            var unixTime = datetime.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
            var counter  = (long)(unixTime * 1000) / (period * 1000);

            return(Otp.GetCode(algorithm, secret, counter, digits));
        }
All Usage Examples Of Albireo.Otp.Otp::GetCode