public static int HashData(string hashAlgorithmId, ReadOnlySpan <byte> source, Span <byte> destination)
            {
                HashProvider provider = HashProviderDispenser.CreateHashProvider(hashAlgorithmId);

                provider.AppendHashData(source);
                return(provider.FinalizeHashAndReset(destination));
            }
Beispiel #2
0
        /// <summary>
        /// Retrieve the hash or HMAC for the data accumulated from prior calls to
        /// <see cref="AppendData(byte[])"/>, and return to the state the object
        /// was in at construction.
        /// </summary>
        /// <returns>The computed hash or HMAC.</returns>
        /// <exception cref="ObjectDisposedException">The object has already been disposed.</exception>
        public byte[] GetHashAndReset()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(typeof(IncrementalHash).Name);
            }

            Debug.Assert((_hash != null) ^ (_hmac != null));
            return(_hash != null?
                   _hash.FinalizeHashAndReset() :
                       _hmac.FinalizeHashAndReset());
        }
        private byte[] InitializeKey(ReadOnlySpan <byte> key)
        {
            if (key.Length > _blockSizeValue)
            {
                byte[] result = new byte[_hashSizeValue];
                _hash1.AppendHashData(key);
                int written = _hash1.FinalizeHashAndReset(result);
                Debug.Assert(written == result.Length);

                return(result);
            }

            return(key.ToArray());
        }
        public override int GetCurrentHash(Span <byte> destination)
        {
            if (!_hashing)
            {
                AppendInnerBuffer();
                _hashing = true;
            }

            // finalize the original hash
            Span <byte> hashValue1   = stackalloc byte[_hashSizeValue];
            int         hash1Written = _hash1.GetCurrentHash(hashValue1);

            Debug.Assert(hash1Written == hashValue1.Length);

            // write the outer array
            AppendOuterBuffer();
            // write the inner hash and finalize the hash
            _hash2.AppendHashData(hashValue1);
            return(_hash2.FinalizeHashAndReset(destination));
        }
        /// <summary>
        /// Retrieve the hash or HMAC for the data accumulated from prior calls to
        /// <see cref="AppendData(byte[])"/>, and return to the state the object
        /// was in at construction.
        /// </summary>
        /// <returns>The computed hash or HMAC.</returns>
        /// <exception cref="ObjectDisposedException">The object has already been disposed.</exception>
        public byte[] GetHashAndReset()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(typeof(IncrementalHash).Name);
            }

            byte[] hashValue;

            if (_hash != null)
            {
                hashValue = _hash.FinalizeHashAndReset();
            }
            else
            {
                Debug.Assert(_hmac != null, "Both _hash and _hmac were null");
                hashValue = _hmac.FinalizeHashAndReset();
            }

            return(hashValue);
        }
Beispiel #6
0
 protected sealed override byte[] HashFinal() =>
 _hashProvider.FinalizeHashAndReset();
Beispiel #7
0
 protected sealed override byte[] HashFinal()
 {
     return(_hashProvider.FinalizeHashAndReset());
 }
Beispiel #8
0
 public int Finalize(Span <byte> destination) => _provider.FinalizeHashAndReset(destination);