public void HashCore(byte[] array, int ibStart, int cbSize) { Contract.Assert(m_hashHandle != null); if (array == null) { throw new ArgumentNullException("array"); } if (ibStart < 0 || ibStart > array.Length - cbSize) { throw new ArgumentOutOfRangeException("ibStart"); } if (cbSize < 0 || cbSize > array.Length) { throw new ArgumentOutOfRangeException("cbSize"); } byte[] hashData = new byte[cbSize]; Buffer.BlockCopy(array, ibStart, hashData, 0, cbSize); BCryptNative.ErrorCode error = BCryptNative.UnsafeNativeMethods.BCryptHashData(m_hashHandle, hashData, hashData.Length, 0); if (error != BCryptNative.ErrorCode.Success) { throw new CryptographicException((int)error); } }
private static bool HasProperty(SafeBCryptKeyHandle cryptHandle, string propertyName) { Debug.Assert(!cryptHandle.IsInvalid); unsafe { int numBytesNeeded; BCryptNative.ErrorCode errorCode = BCryptNative.UnsafeNativeMethods.BCryptGetProperty(cryptHandle, propertyName, null, 0, out numBytesNeeded, 0); return(errorCode == BCryptNative.ErrorCode.Success && numBytesNeeded > 0); } }
public byte[] HashFinal() { byte[] pbInput = new byte[BCryptNative.GetInt32Property <SafeBCryptHashHandle>(this.m_hashHandle, "HashDigestLength")]; BCryptNative.ErrorCode code = BCryptNative.UnsafeNativeMethods.BCryptFinishHash(this.m_hashHandle, pbInput, pbInput.Length, 0); if (code != BCryptNative.ErrorCode.Success) { throw new CryptographicException((int)code); } return(pbInput); }
public byte[] HashFinal() { Contract.Ensures(Contract.Result <byte[]>() != null); Contract.Assert(m_hashHandle != null); int hashSize = BCryptNative.GetInt32Property(m_hashHandle, BCryptNative.HashPropertyName.HashLength); byte[] hashValue = new byte[hashSize]; BCryptNative.ErrorCode error = BCryptNative.UnsafeNativeMethods.BCryptFinishHash(m_hashHandle, hashValue, hashValue.Length, 0); if (error != BCryptNative.ErrorCode.Success) { throw new CryptographicException((int)error); } return(hashValue); }
public void HashCore(byte[] array, int ibStart, int cbSize) { if (array == null) { throw new ArgumentNullException("array"); } if ((ibStart < 0) || (ibStart > (array.Length - cbSize))) { throw new ArgumentOutOfRangeException("ibStart"); } if ((cbSize < 0) || (cbSize > array.Length)) { throw new ArgumentOutOfRangeException("cbSize"); } byte[] dst = new byte[cbSize]; Buffer.BlockCopy(array, ibStart, dst, 0, cbSize); BCryptNative.ErrorCode code = BCryptNative.UnsafeNativeMethods.BCryptHashData(this.m_hashHandle, dst, dst.Length, 0); if (code != BCryptNative.ErrorCode.Success) { throw new CryptographicException((int)code); } }
private static byte[] GetProperty(SafeBCryptKeyHandle cryptHandle, string propertyName) { Debug.Assert(!cryptHandle.IsInvalid); unsafe { int numBytesNeeded; BCryptNative.ErrorCode errorCode = BCryptNative.UnsafeNativeMethods.BCryptGetProperty(cryptHandle, propertyName, null, 0, out numBytesNeeded, 0); if (errorCode != BCryptNative.ErrorCode.Success) { return(null); } byte[] propertyValue = new byte[numBytesNeeded]; errorCode = BCryptNative.UnsafeNativeMethods.BCryptGetProperty(cryptHandle, propertyName, propertyValue, propertyValue.Length, out numBytesNeeded, 0); if (errorCode != BCryptNative.ErrorCode.Success) { return(null); } Array.Resize(ref propertyValue, numBytesNeeded); return(propertyValue); } }
public void Initialize() { SafeBCryptHashHandle phHash = null; IntPtr zero = IntPtr.Zero; RuntimeHelpers.PrepareConstrainedRegions(); try { int cb = BCryptNative.GetInt32Property <SafeBCryptAlgorithmHandle>(this.m_algorithmHandle, "ObjectLength"); RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { zero = Marshal.AllocCoTaskMem(cb); } BCryptNative.ErrorCode code = BCryptNative.UnsafeNativeMethods.BCryptCreateHash(this.m_algorithmHandle, out phHash, zero, cb, IntPtr.Zero, 0, 0); if (code != BCryptNative.ErrorCode.Success) { throw new CryptographicException((int)code); } phHash.HashObject = zero; } finally { if ((zero != IntPtr.Zero) && ((phHash == null) || (phHash.HashObject == IntPtr.Zero))) { Marshal.FreeCoTaskMem(zero); } } 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_algorithmHandle != null); // Try to create a new hash algorithm to use SafeBCryptHashHandle newHashAlgorithm = null; IntPtr hashObjectBuffer = IntPtr.Zero; // Creating a BCRYPT_HASH_HANDLE requires providing a buffer to hold the hash object in, which // is tied to the lifetime of the hash handle. Wrap this in a CER so we can tie the lifetimes together // safely. try { int hashObjectSize = BCryptNative.GetInt32Property(m_algorithmHandle, BCryptNative.ObjectPropertyName.ObjectLength); Debug.Assert(hashObjectSize > 0, "hashObjectSize > 0"); // Allocate in a CER because we could fail between the alloc and the assignment try { } finally { hashObjectBuffer = Marshal.AllocCoTaskMem(hashObjectSize); } BCryptNative.ErrorCode error = BCryptNative.UnsafeNativeMethods.BCryptCreateHash(m_algorithmHandle, out newHashAlgorithm, hashObjectBuffer, hashObjectSize, IntPtr.Zero, 0, 0); if (error != BCryptNative.ErrorCode.Success) { throw new CryptographicException((int)error); } } finally { // Make sure we've successfully transfered ownership of the hash object buffer to the safe handle if (hashObjectBuffer != IntPtr.Zero) { // If we created the safe handle, it needs to own the buffer and free it in release if (newHashAlgorithm != null) { newHashAlgorithm.HashObject = hashObjectBuffer; } else { Marshal.FreeCoTaskMem(hashObjectBuffer); } } } // If we could create it, dispose of any old hash handle we had and replace it with the new one if (m_hashHandle != null) { m_hashHandle.Dispose(); } m_hashHandle = newHashAlgorithm; }