/// <summary> /// Reset the hash algorithm to begin hashing a new set of data /// </summary> public SafeCapiHashHandle Initialize() { Contract.Ensures(m_hashHandle != null && !m_hashHandle.IsInvalid && !m_hashHandle.IsClosed); Contract.Assert(m_cspHandle != null); // Try to create a new hash algorithm to use SafeCapiHashHandle?newHashAlgorithm = null; #if !NET_COREAPP_50 RuntimeHelpers.PrepareConstrainedRegions(); #endif try { if (!CapiNative.UnsafeNativeMethods.CryptCreateHash(m_cspHandle, m_algorithmId, SafeCapiKeyHandle.InvalidHandle, 0, out newHashAlgorithm)) { // BadAlgorithmId means that this CSP does not support the specified algorithm, which means // that we're on a platform that does not support the given hash function. int error = Marshal.GetLastWin32Error(); if (error == (int)CapiNative.ErrorCode.BadAlgorithmId) { throw new PlatformNotSupportedException(SR.Cryptography_PlatformNotSupported); } else { throw new CryptographicException(error); } } } finally { if (newHashAlgorithm != null && !newHashAlgorithm.IsInvalid) { newHashAlgorithm.SetParentCsp(m_cspHandle); } } // If we created a new algorithm, dispose of the old one and use the new one Contract.Assert(newHashAlgorithm != null, "newHashAlgorithm != null"); if (m_hashHandle != null) { m_hashHandle.Dispose(); } m_hashHandle = newHashAlgorithm; return(m_hashHandle); }
public void Dispose() { m_hashHandle?.Dispose(); m_cspHandle?.Dispose(); }