Exemplo n.º 1
0
        private void InitializeBCryptHash(SecureString base64EncodedPassword, int base64EncodedPasswordLength, out IntPtr hashHandle)
        {
            IntPtr keyBytes       = IntPtr.Zero;
            uint   keyBytesLength = 0;

            try
            {
                Base64Helper.SecureStringToNativeBytes(base64EncodedPassword, base64EncodedPasswordLength, out keyBytes, out keyBytesLength);

                int status = NativeMethods.BCryptCreateHash(this.algorithmHandle,
                                                            out hashHandle,
                                                            IntPtr.Zero,
                                                            0,
                                                            keyBytes,
                                                            keyBytesLength,
                                                            0);
                if (status != 0)
                {
                    throw new Win32Exception(status, "BCryptCreateHash");
                }
            }
            finally
            {
                if (keyBytes != IntPtr.Zero)
                {
                    for (int n = 0; n < (int)keyBytesLength; n++)
                    {
                        Marshal.WriteByte(keyBytes, n, 0);
                    }

                    Marshal.FreeCoTaskMem(keyBytes);
                    keyBytes       = IntPtr.Zero;
                    keyBytesLength = 0;
                }
            }
        }