public static extern bool CryptEncrypt(SafeCapiKeyHandle hKey, SafeCapiHashHandle hHash, [MarshalAs(UnmanagedType.Bool)] bool Final, int dwFlags, IntPtr pbData, // BYTE * [In, Out] ref int pdwDataLen, int dwBufLen);
internal static byte[] GetHashParameter(SafeCapiHashHandle hashHandle, CapiNative.HashParameter parameter) { Contract.Requires(hashHandle != null); Contract.Requires(CapiNative.HashParameter.AlgorithmId <= parameter && parameter <= CapiNative.HashParameter.HashSize); Contract.Ensures(Contract.Result <byte[]>() != null); // // Determine the maximum size of the parameter and retrieve it // int parameterSize = 0; if (!CapiNative.UnsafeNativeMethods.CryptGetHashParam(hashHandle, parameter, null, ref parameterSize, 0)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } Debug.Assert(0 < parameterSize, "Invalid parameter size returned"); byte[] parameterValue = new byte[parameterSize]; if (!CapiNative.UnsafeNativeMethods.CryptGetHashParam(hashHandle, parameter, parameterValue, ref parameterSize, 0)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } // CAPI may have asked for a larger buffer than it used, so only copy the used bytes if (parameterSize != parameterValue.Length) { byte[] realValue = new byte[parameterSize]; Buffer.BlockCopy(parameterValue, 0, realValue, 0, parameterSize); parameterValue = realValue; } return(parameterValue); }
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; }
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; }
internal static byte[] GetHashParameter(SafeCapiHashHandle hashHandle, HashParameter parameter) { int pdwDataLen = 0; if (!UnsafeNativeMethods.CryptGetHashParam(hashHandle, parameter, null, ref pdwDataLen, 0)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } byte[] pbData = new byte[pdwDataLen]; if (!UnsafeNativeMethods.CryptGetHashParam(hashHandle, parameter, pbData, ref pdwDataLen, 0)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (pdwDataLen != pbData.Length) { byte[] dst = new byte[pdwDataLen]; Buffer.BlockCopy(pbData, 0, dst, 0, pdwDataLen); pbData = dst; } return(pbData); }
public static extern bool CryptHashData(SafeCapiHashHandle hHash, [MarshalAs(UnmanagedType.LPArray)] byte[] pbData, int dwDataLen, int dwFlags);
public static extern bool CryptGetHashParam(SafeCapiHashHandle hHash, HashParameter dwParam, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbData, [In, Out] ref int pdwDataLen, int dwFlags);
public static extern bool CryptCreateHash(SafeCspHandle hProv, AlgorithmId Algid, SafeCapiKeyHandle hKey, int dwFlags, [Out] out SafeCapiHashHandle phHash);
public static extern unsafe bool CryptHashData(SafeCapiHashHandle hHash, byte *pbData, int dwDataLen, int dwFlags);