Example #1
0
            public static unsafe int HashData(string hashAlgorithmId, ReadOnlySpan <byte> source, Span <byte> destination)
            {
                int hashSize; // in bytes

                // Try using a pseudo-handle if available.
                if (!s_useCompatOneShot)
                {
                    if (HashDataUsingPseudoHandle(hashAlgorithmId, source, destination, out hashSize))
                    {
                        return(hashSize);
                    }
                }

                // Pseudo-handle not available or a precondition check failed.
                // Fall back to a shared handle with no using or dispose.
                SafeBCryptAlgorithmHandle cachedAlgorithmHandle = BCryptAlgorithmCache.GetCachedBCryptAlgorithmHandle(
                    hashAlgorithmId,
                    BCryptOpenAlgorithmProviderFlags.None,
                    out hashSize);

                if (destination.Length < hashSize)
                {
                    throw new CryptographicException();
                }

                HashUpdateAndFinish(cachedAlgorithmHandle, hashSize, source, destination);

                return(hashSize);
            }
            public static unsafe int HashData(string hashAlgorithmId, ReadOnlySpan <byte> source, Span <byte> destination)
            {
                int hashSize; // in bytes

                // Try using a pseudo-handle if available.
                if (Interop.BCrypt.PseudoHandlesSupported)
                {
                    HashDataUsingPseudoHandle(hashAlgorithmId, source, destination, out hashSize);
                    return(hashSize);
                }
                else
                {
                    // Pseudo-handle not available. Fall back to a shared handle with no using or dispose.
                    SafeBCryptAlgorithmHandle cachedAlgorithmHandle = BCryptAlgorithmCache.GetCachedBCryptAlgorithmHandle(
                        hashAlgorithmId,
                        BCryptOpenAlgorithmProviderFlags.None,
                        out hashSize);

                    if (destination.Length < hashSize)
                    {
                        Debug.Fail("Caller should have checked length.");
                        throw new CryptographicException();
                    }

                    HashUpdateAndFinish(cachedAlgorithmHandle, hashSize, source, destination);

                    return(hashSize);
                }
            }
Example #3
0
            public static unsafe int HashData(string hashAlgorithmId, ReadOnlySpan <byte> source, Span <byte> destination)
            {
                // Shared handle, no using or dispose.
                SafeBCryptAlgorithmHandle cachedAlgorithmHandle = BCryptAlgorithmCache.GetCachedBCryptAlgorithmHandle(
                    hashAlgorithmId,
                    BCryptOpenAlgorithmProviderFlags.None);

                int hashSize;

                NTSTATUS ntStatus = Interop.BCrypt.BCryptGetProperty(
                    cachedAlgorithmHandle,
                    Interop.BCrypt.BCryptPropertyStrings.BCRYPT_HASH_LENGTH,
                    &hashSize,
                    sizeof(int),
                    out _,
                    0);

                if (ntStatus != NTSTATUS.STATUS_SUCCESS)
                {
                    throw Interop.BCrypt.CreateCryptographicException(ntStatus);
                }

                if (destination.Length < hashSize)
                {
                    throw new CryptographicException();
                }

                if (!s_useCompatOneShot)
                {
                    try
                    {
                        fixed(byte *pSource = source)
                        fixed(byte *pDestination = destination)
                        {
                            ntStatus = Interop.BCrypt.BCryptHash(cachedAlgorithmHandle, null, 0, pSource, source.Length, pDestination, hashSize);

                            if (ntStatus != NTSTATUS.STATUS_SUCCESS)
                            {
                                throw Interop.BCrypt.CreateCryptographicException(ntStatus);
                            }
                        }

                        return(hashSize);
                    }
                    catch (EntryPointNotFoundException)
                    {
                        s_useCompatOneShot = true;
                    }
                }

                Debug.Assert(s_useCompatOneShot);
                HashUpdateAndFinish(cachedAlgorithmHandle, hashSize, source, destination);

                return(hashSize);
            }