public void Initialize() { SafeCapiHashHandle phHash = null; if (!CapiNative.UnsafeNativeMethods.CryptCreateHash(this.m_cspHandle, this.m_algorithmId, SafeCapiKeyHandle.InvalidHandle, 0, out phHash)) { int hr = Marshal.GetLastWin32Error(); if (hr == -2146893816) { throw new PlatformNotSupportedException(System.SR.GetString("Cryptography_PlatformNotSupported")); } throw new CryptographicException(hr); } if (this.m_hashHandle != null) { this.m_hashHandle.Dispose(); } this.m_hashHandle = phHash; }
public void 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; RuntimeHelpers.PrepareConstrainedRegions(); 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.GetString(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 Debug.Assert(newHashAlgorithm != null, "newHashAlgorithm != null"); if (m_hashHandle != null) { m_hashHandle.Dispose(); } m_hashHandle = newHashAlgorithm; }