internal static extern ErrorCode BCryptCreateHash(SafeBCryptAlgorithmHandle hAlgorithm,
                                                   [Out] out SafeBCryptHashHandle phHash,
                                                   IntPtr pbHashObject,
                                                   int cbHashObject,
                                                   IntPtr pbSecret,
                                                   int cbSecret,
                                                   int dwFlags);
Beispiel #2
0
        public void Dispose()
        {
            Contract.Ensures(m_hashHandle == null || m_hashHandle.IsClosed);
            Contract.Ensures(m_algorithmHandle == null || m_algorithmHandle.IsClosed);

            if (m_hashHandle != null)
            {
                m_hashHandle.Dispose();
            }

            if (m_algorithmHandle != null)
            {
                m_algorithmHandle = null;
            }
        }
Beispiel #3
0
        public BCryptHashAlgorithm(CngAlgorithm algorithm, string implementation)
        {
            Contract.Requires(algorithm != null);
            Contract.Requires(!String.IsNullOrEmpty(implementation));
            Contract.Ensures(m_algorithmHandle != null && !m_algorithmHandle.IsInvalid && !m_algorithmHandle.IsClosed);
            Contract.Ensures(m_hashHandle != null && !m_hashHandle.IsInvalid && !m_hashHandle.IsClosed);

            if (_algorithmCache == null)
            {
                _algorithmCache = new BCryptAlgorithmHandleCache();
            }

            m_algorithmHandle = _algorithmCache.GetCachedAlgorithmHandle(algorithm.Algorithm, implementation);

            Initialize();
        }
Beispiel #4
0
        public SafeBCryptAlgorithmHandle GetCachedAlgorithmHandle(string algorithm, string implementation)
        {
            string handleKey = algorithm + implementation;
            SafeBCryptAlgorithmHandle algorithmHandle = null;

            if (m_algorithmHandles.ContainsKey(handleKey))
            {
                algorithmHandle = m_algorithmHandles[handleKey].Target as SafeBCryptAlgorithmHandle;
                if (algorithmHandle != null)
                {
                    return(algorithmHandle);
                }
            }

            algorithmHandle = BCryptNative.OpenAlgorithm(algorithm, implementation);
            m_algorithmHandles[handleKey] = new WeakReference(algorithmHandle);
            return(algorithmHandle);
        }
        internal static SafeBCryptAlgorithmHandle OpenAlgorithm(string algorithm, string implementation)
        {
            Contract.Requires(!String.IsNullOrEmpty(algorithm));
            Contract.Requires(!String.IsNullOrEmpty(implementation));
            Contract.Ensures(Contract.Result <SafeBCryptAlgorithmHandle>() != null &&
                             !Contract.Result <SafeBCryptAlgorithmHandle>().IsInvalid&&
                             !Contract.Result <SafeBCryptAlgorithmHandle>().IsClosed);

            SafeBCryptAlgorithmHandle algorithmHandle = null;
            ErrorCode error = UnsafeNativeMethods.BCryptOpenAlgorithmProvider(out algorithmHandle,
                                                                              algorithm,
                                                                              implementation,
                                                                              0);

            if (error != ErrorCode.Success)
            {
                throw new CryptographicException((int)error);
            }

            return(algorithmHandle);
        }
 internal static extern ErrorCode BCryptOpenAlgorithmProvider([Out] out SafeBCryptAlgorithmHandle phAlgorithm,
                                                              string pszAlgId,             // BCryptAlgorithm
                                                              string pszImplementation,    // ProviderNames
                                                              int dwFlags);
 internal static extern ErrorCode BCryptGetAlgorithmProperty(SafeBCryptAlgorithmHandle hObject,
                                                             string pszProperty,
                                                             [MarshalAs(UnmanagedType.LPArray), In, Out] byte[] pbOutput,
                                                             int cbOutput,
                                                             [In, Out] ref int pcbResult,
                                                             int flags);