Пример #1
0
        public void Open(string algorithm)
        {
            Close();

            int status = BCryptDll.BCryptOpenAlgorithmProvider(
                out IntPtr hAlgorithm,
                algorithm,
                BCryptDll.MS_PRIMITIVE_PROVIDER,
                0);

            if (status != 0)
            {
                throw new Exception($"Error: Status = {status}");
            }
            _hAlgorithm = hAlgorithm;

            byte[] length = new byte[4];
            status = BCryptDll.BCryptGetProperty(
                _hAlgorithm,
                BCryptDll.BCRYPT_HASH_LENGTH,
                length,
                out uint pcbResult,
                0);
            if (status != 0 || pcbResult != 4)
            {
                throw new Exception($"Error: Status = {status}");
            }

            _hashLength = BitConverter.ToInt32(length, 0);
        }
Пример #2
0
        public static byte[] GenerateRandomNumber(int length)
        {
            var buffer = new byte[length];
            int status = BCryptDll.BCryptGenRandom(IntPtr.Zero, buffer, BCryptDll.BCRYPT_USE_SYSTEM_PREFERRED_RNG);

            if (status != 0)
            {
                throw new Exception($"Error: Status = {status}");
            }
            return(buffer);
        }
Пример #3
0
        public void Close()
        {
            if (_hAlgorithm == IntPtr.Zero)
            {
                return;
            }

            int status = BCryptDll.BCryptCloseAlgorithmProvider(_hAlgorithm, 0);

            if (status != 0)
            {
                throw new Exception($"Error: Status = {status}");
            }
        }
Пример #4
0
        public byte[] Hash(byte[] input, byte[] secret)
        {
            byte[] output = new byte[_hashLength];
            int    status = BCryptDll.BCryptHash(
                _hAlgorithm,
                secret,
                input,
                output
                );

            if (status != 0)
            {
                throw new Exception($"Error: Status = {status}");
            }
            return(output);
        }