internal static extern ErrorCode BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm, [Out] out SafeBCryptHashHandle hHash, IntPtr pbHashObject, // byte * int cbHashObject, [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbSecret, int cbSecret, int dwFlags);
internal static SafeBCryptHashHandle CreateHash(SafeBCryptAlgorithmHandle algorithm, byte[] secret) { Debug.Assert(algorithm != null, "algorithm != null"); Debug.Assert(!algorithm.IsClosed && !algorithm.IsInvalid, "!algorithm.IsClosed && !algorithm.IsInvalid"); IntPtr hashObject = IntPtr.Zero; SafeBCryptHashHandle hash = null; RuntimeHelpers.PrepareConstrainedRegions(); try { // Figure out how big of a buffer is needed for the hash object and allocate it int hashObjectSize = GetInt32Property(algorithm, ObjectPropertyName.ObjectLength); RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { hashObject = Marshal.AllocCoTaskMem(hashObjectSize); } // Create the hash object ErrorCode error = UnsafeNativeMethods.BCryptCreateHash(algorithm, out hash, hashObject, hashObjectSize, secret, secret != null ? secret.Length : 0, 0); if (error != ErrorCode.Success) { throw new CryptographicException((int)error); } // Transfer ownership of the buffer to the safe handle hash.DataBuffer = hashObject; return(hash); } finally { // If the safe hash handle never took ownership of the data buffer, free it now. if (hashObject != IntPtr.Zero) { if (hash == null || hash.DataBuffer == IntPtr.Zero) { Marshal.FreeCoTaskMem(hashObject); } } } }
internal static void HashData(SafeBCryptHashHandle hash, byte[] data) { Debug.Assert(hash != null, "hash != null"); Debug.Assert(!hash.IsClosed && !hash.IsInvalid, "!hash.IsClosed && !hash.IsInvalid"); Debug.Assert(data != null, "data != null"); ErrorCode error = UnsafeNativeMethods.BCryptHashData(hash, data, data.Length, 0); if (error != ErrorCode.Success) { throw new CryptographicException((int)error); } }
public override void Initialize() { Debug.Assert(m_algorithm != null, "m_algorithm != null"); base.Initialize(); // If we have a previously used hash handle, we can clean it up now if (m_hash != null) { m_hash.Dispose(); } m_hash = BCryptNative.CreateHash(m_algorithm, KeyValue); // We're allowed to reset the key at this point State = 0; }
internal static byte[] FinishHash(SafeBCryptHashHandle hash) { Debug.Assert(hash != null, "hash != null"); Debug.Assert(!hash.IsClosed && !hash.IsInvalid, "!hash.IsClosed && !hash.IsInvalid"); int hashSize = GetInt32Property(hash, HashPropertyName.HashLength); byte[] result = new byte[hashSize]; ErrorCode error = UnsafeNativeMethods.BCryptFinishHash(hash, result, result.Length, 0); if (error != ErrorCode.Success) { throw new CryptographicException((int)error); } return(result); }
internal static extern ErrorCode BCryptSetHashProperty(SafeBCryptHashHandle hObject, [MarshalAs(UnmanagedType.LPWStr)] string pszProperty, [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbInput, int cbInput, int dwFlags);
internal static extern ErrorCode BCryptHashData(SafeBCryptHashHandle hHash, [In, MarshalAs(UnmanagedType.LPArray)] byte[] pbInput, int cbInput, int dwFlags);
internal static extern ErrorCode BCryptGetHashProperty(SafeBCryptHashHandle hObject, [MarshalAs(UnmanagedType.LPWStr)] string pszProperty, [In, Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbOutput, int cbOutput, [In, Out] ref int pcbResult, int flags);
internal static extern ErrorCode BCryptFinishHash(SafeBCryptHashHandle hHash, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbOutput, int cbOutput, int dwFlags);