Esempio n. 1
0
        /// <summary>
        ///     Create new Datahasher with given algorithm
        /// </summary>
        /// <param name="algorithm">Hash algorithm</param>
        public DataHasher(HashAlgorithm algorithm)
        {
            if (algorithm == null)
            {
                throw new ArgumentNullException(nameof(algorithm));
            }

            _algorithm = algorithm;
            _digester  = DigestProvider.GetDigest(algorithm);
        }
Esempio n. 2
0
        /// <summary>
        ///     Calculate HMAC for data with given key.
        /// </summary>
        /// <param name="key">HMAC key</param>
        /// <param name="data">data to calculate HMAC from</param>
        /// <returns>HMAC data hash</returns>
        public DataHash GetHash(byte[] key, byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            HMac hMac = new HMac(DigestProvider.GetDigest(_algorithm));

            hMac.Init(new KeyParameter(key));
            hMac.BlockUpdate(data, 0, data.Length);

            byte[] value = new byte[_algorithm.Length];
            hMac.DoFinal(value, 0);
            return(new DataHash(_algorithm, value));
        }