示例#1
0
            protected sealed override bool ReleaseHandle()
            {
                IntPtr localHandle = handle;

                handle = IntPtr.Zero;
                int status = MD5PInvokeHelper.BCryptDestroyHash(localHandle);

                return(status == MD5PInvokeHelper.StatusSuccess);
            }
示例#2
0
            protected sealed override bool ReleaseHandle()
            {
                IntPtr localHandle = handle;

                handle = IntPtr.Zero;
                int status = MD5PInvokeHelper.BCryptCloseAlgorithmProvider(localHandle, 0);

                return(status == MD5PInvokeHelper.StatusSuccess);
            }
示例#3
0
        internal static byte[] CalculateHash(byte[] inputBuffer)
        {
            byte[] hashResult;
            int    status;
            SafeBCryptHashHandle hashHandle = null;

            status = MD5PInvokeHelper.BCryptCreateHash(MD5PInvokeHelper.MD5AlgorithmProvider, out hashHandle, IntPtr.Zero, 0, null, 0, 0);
            if (status != MD5PInvokeHelper.StatusSuccess)
            {
                DisposeMD5Handles();
                throw new CryptographicException(status);
            }

            using (hashHandle)
            {
                int sizeOfHashSize;
                int hashSize;
                unsafe
                {
                    status = MD5PInvokeHelper.BCryptGetProperty(hashHandle, MD5PInvokeHelper.BCryptHashLength, &hashSize, sizeof(int), out sizeOfHashSize, 0);
                    if (status != MD5PInvokeHelper.StatusSuccess)
                    {
                        DisposeMD5Handles();
                        throw new CryptographicException(status);
                    }
                }

                unsafe
                {
                    fixed(byte *inputBytePointer = inputBuffer)
                    {
                        status = MD5PInvokeHelper.BCryptHashData(hashHandle, inputBytePointer, inputBuffer.Length, 0);
                        if (status != MD5PInvokeHelper.StatusSuccess)
                        {
                            DisposeMD5Handles();
                            throw new CryptographicException(status);
                        }
                    }
                }

                hashResult = new byte[hashSize];
                status     = MD5PInvokeHelper.BCryptFinishHash(hashHandle, hashResult, hashResult.Length, 0);
                if (status != MD5PInvokeHelper.StatusSuccess)
                {
                    DisposeMD5Handles();
                    throw new CryptographicException(status);
                }
            }

            // hashHandle has been disposed by the end of the "using" block.
            hashHandle = null;

            return(hashResult);
        }