System.Security.Cryptography.IncrementalHash.GetHashAndReset C# (CSharp) Method

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[]
        public byte[] GetHashAndReset()
        {
            if (_disposed)
                throw new ObjectDisposedException(typeof(IncrementalHash).Name);

            Debug.Assert(_hash != null);

            if (_resetPending)
            {
                // No point in setting _resetPending to false, we're about to set it to true.
                _hash.Initialize();
            }

            _hash.TransformFinalBlock(Array.Empty<byte>(), 0, 0);

            byte[] hashValue = _hash.Hash;
            _resetPending = true;

            return hashValue;
        }

Usage Example

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);
                }
            }));
        }
All Usage Examples Of System.Security.Cryptography.IncrementalHash::GetHashAndReset