GetHashAndReset() public method

Retrieve the hash or HMAC for the data accumulated from prior calls to AppendData(byte[]), and return to the state the object was in at construction.
The object has already been disposed.
public GetHashAndReset ( ) : byte[]
return byte[]
Example #1
0
        internal static RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorithmName)
        {
            return(s_lookup.GetOrAdd(
                       hashAlgorithmName,
                       static hashAlgorithmName =>
            {
                using (IncrementalHash hasher = IncrementalHash.CreateHash(hashAlgorithmName))
                {
                    // SHA-2-512 is the biggest we expect
                    Span <byte> stackDest = stackalloc byte[512 / 8];
                    ReadOnlyMemory <byte> digestInfoPrefix;

                    if (hashAlgorithmName == HashAlgorithmName.MD5)
                    {
                        digestInfoPrefix = s_digestInfoMD5;
                    }
                    else if (hashAlgorithmName == HashAlgorithmName.SHA1)
                    {
                        digestInfoPrefix = s_digestInfoSha1;
                    }
                    else if (hashAlgorithmName == HashAlgorithmName.SHA256)
                    {
                        digestInfoPrefix = s_digestInfoSha256;
                    }
                    else if (hashAlgorithmName == HashAlgorithmName.SHA384)
                    {
                        digestInfoPrefix = s_digestInfoSha384;
                    }
                    else if (hashAlgorithmName == HashAlgorithmName.SHA512)
                    {
                        digestInfoPrefix = s_digestInfoSha512;
                    }
                    else
                    {
                        Debug.Fail("Unknown digest algorithm");
                        throw new CryptographicException();
                    }

                    if (hasher.TryGetHashAndReset(stackDest, out int bytesWritten))
                    {
                        return new RsaPaddingProcessor(hashAlgorithmName, bytesWritten, digestInfoPrefix);
                    }

                    byte[] big = hasher.GetHashAndReset();
                    return new RsaPaddingProcessor(hashAlgorithmName, big.Length, digestInfoPrefix);
                }
            }));
        }
        internal static RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorithmName)
        {
            return(s_lookup.GetOrAdd(
                       hashAlgorithmName,
                       alg =>
            {
                using (IncrementalHash hasher = IncrementalHash.CreateHash(hashAlgorithmName))
                {
                    // SHA-2-512 is the biggest we expect
                    Span <byte> stackDest = stackalloc byte[512 / 8];

                    if (hasher.TryGetHashAndReset(stackDest, out int bytesWritten))
                    {
                        return new RsaPaddingProcessor(hashAlgorithmName, bytesWritten);
                    }

                    byte[] big = hasher.GetHashAndReset();
                    return new RsaPaddingProcessor(hashAlgorithmName, big.Length);
                }
            }));
        }
        internal static byte[] DeriveKeyFromHash(
            ECDiffieHellmanPublicKey otherPartyPublicKey,
            HashAlgorithmName hashAlgorithm,
            ReadOnlySpan <byte> secretPrepend,
            ReadOnlySpan <byte> secretAppend,
            DeriveSecretAgreement deriveSecretAgreement)
        {
            Debug.Assert(otherPartyPublicKey != null);
            Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));

            using (IncrementalHash hash = IncrementalHash.CreateHash(hashAlgorithm))
            {
                hash.AppendData(secretPrepend);

                byte[]? secretAgreement = deriveSecretAgreement(otherPartyPublicKey, hash);
                // We want the side effect, and it should not have returned the answer.
                Debug.Assert(secretAgreement == null);

                hash.AppendData(secretAppend);

                return(hash.GetHashAndReset());
            }
        }
 protected override byte[] HashFinal()
 {
     _running = false;
     return(_incrementalHash.GetHashAndReset());
 }
 protected override byte[] HashFinal() =>
 _incrementalHash.GetHashAndReset();
Example #6
0
 protected override byte[] HashFinal()
 {
     return(_incrementalHash.GetHashAndReset());
 }