Beispiel #1
0
        protected virtual bool TryHashData(ReadOnlySpan <byte> data, Span <byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
        {
            // If this is an algorithm that we ship, then we can use the hash one-shot.
            if (this is IRuntimeAlgorithm)
            {
                return(HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten));
            }

            // If this is not our algorithm implementation, for compatibility purposes we need to
            // call out to the HashData virtual.
            byte[] result;
            // Use ArrayPool.Shared instead of CryptoPool because the array is passed out.
            byte[] array = ArrayPool <byte> .Shared.Rent(data.Length);

            try
            {
                data.CopyTo(array);
                result = HashData(array, 0, data.Length, hashAlgorithm);
            }
            finally
            {
                Array.Clear(array, 0, data.Length);
                ArrayPool <byte> .Shared.Return(array);
            }

            if (destination.Length >= result.Length)
            {
                new ReadOnlySpan <byte>(result).CopyTo(destination);
                bytesWritten = result.Length;
                return(true);
            }

            bytesWritten = 0;
            return(false);
        }
Beispiel #2
0
        protected override bool TryHashData(ReadOnlySpan <byte> data, Span <byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
        {
            if (hashAlgorithm != HashAlgorithmName.SHA1)
            {
                throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name);
            }

            return(HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten));
        }
Beispiel #3
0
 protected override bool TryHashData(ReadOnlySpan <byte> data, Span <byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
 HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten);