Esempio n. 1
0
File: Otp.cs Progetto: kappa7194/otp
        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);
        }
        public virtual byte[] CreateSignature(HashAlgorithm hash)
        {
            if (hash == null)
                throw new ArgumentNullException(nameof(hash));

            SetHashAlgorithm(hash.ToAlgorithmName());
            return CreateSignature(hash.Hash);
        }
        public virtual bool VerifySignature(HashAlgorithm hash, byte[] rgbSignature)
        {
            ArgumentNullException.ThrowIfNull(hash);

            SetHashAlgorithm(hash.ToAlgorithmName() !);
            Debug.Assert(hash.Hash != null);
            return(VerifySignature(hash.Hash, rgbSignature));
        }
Esempio n. 4
0
        public virtual byte[] CreateSignature(HashAlgorithm hash)
        {
            ArgumentNullException.ThrowIfNull(hash);

            SetHashAlgorithm(hash.ToAlgorithmName() !);
            Debug.Assert(hash.Hash != null);
            return(CreateSignature(hash.Hash));
        }
        public virtual bool VerifySignature(HashAlgorithm hash, byte[] rgbSignature)
        {
            if (hash == null)
                throw new ArgumentNullException(nameof(hash));

            SetHashAlgorithm(hash.ToAlgorithmName());
            return VerifySignature(hash.Hash, rgbSignature);
        }
Esempio n. 6
0
        public virtual bool VerifySignature(HashAlgorithm hash, byte[] rgbSignature)
        {
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            SetHashAlgorithm(hash.ToAlgorithmName());
            return(VerifySignature(hash.Hash, rgbSignature));
        }
Esempio n. 7
0
        public virtual byte[] CreateSignature(HashAlgorithm hash)
        {
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            SetHashAlgorithm(hash.ToAlgorithmName());
            return(CreateSignature(hash.Hash));
        }