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
        private static void Extract(HashAlgorithmName hashAlgorithmName, int hashLength, ReadOnlySpan <byte> ikm, ReadOnlySpan <byte> salt, Span <byte> prk)
        {
            Debug.Assert(HashLength(hashAlgorithmName) == hashLength);
            int written = HashOneShotHelpers.MacData(hashAlgorithmName, salt, ikm, prk);

            Debug.Assert(written == prk.Length, $"Bytes written is {written} bytes which does not match output length ({prk.Length} bytes)");
        }
        /// <summary>
        /// Verifies the signature of a hash value.
        /// </summary>
        public bool VerifyData(byte[] buffer, object halg, byte[] signature)
        {
            int calgHash = CapiHelper.ObjToHashAlgId(halg);
            HashAlgorithmName hashAlgorithmName = CapiHelper.AlgIdToHashAlgorithmName(calgHash);

            byte[] hashVal = HashOneShotHelpers.HashData(hashAlgorithmName, buffer);
            return(VerifyHash(hashVal, calgHash, signature));
        }
        /// <summary>
        /// Computes the hash value of a subset of the specified byte array using the
        /// specified hash algorithm, and signs the resulting hash value.
        /// </summary>
        /// <param name="inputStream">The input data for which to compute the hash</param>
        /// <param name="halg">The hash algorithm to use to create the hash value. </param>
        /// <returns>The RSA signature for the specified data.</returns>
        public byte[] SignData(Stream inputStream, object halg)
        {
            int calgHash = CapiHelper.ObjToHashAlgId(halg);
            HashAlgorithmName hashAlgorithmName = CapiHelper.AlgIdToHashAlgorithmName(calgHash);

            byte[] hashVal = HashOneShotHelpers.HashData(hashAlgorithmName, inputStream);
            return(SignHash(hashVal, calgHash));
        }
        /// <summary>
        /// Computes the hash value of a subset of the specified byte array using the
        /// specified hash algorithm, and signs the resulting hash value.
        /// </summary>
        /// <param name="buffer">The input data for which to compute the hash</param>
        /// <param name="offset">The offset into the array from which to begin using data</param>
        /// <param name="count">The number of bytes in the array to use as data. </param>
        /// <param name="halg">The hash algorithm to use to create the hash value. </param>
        /// <returns>The RSA signature for the specified data.</returns>
        public byte[] SignData(byte[] buffer, int offset, int count, object halg)
        {
            int calgHash = CapiHelper.ObjToHashAlgId(halg);
            HashAlgorithmName hashAlgorithmName = CapiHelper.AlgIdToHashAlgorithmName(calgHash);

            byte[] hashVal = HashOneShotHelpers.HashData(hashAlgorithmName, new ReadOnlySpan <byte>(buffer, offset, count));
            return(SignHash(hashVal, calgHash));
        }
Beispiel #6
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 #7
0
        protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
        {
            if (hashAlgorithm != HashAlgorithmName.SHA1)
            {
                throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name);
            }

            return(HashOneShotHelpers.HashData(hashAlgorithm, data));
        }
Beispiel #8
0
        protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
        {
            if (hashAlgorithm != HashAlgorithmName.SHA1)
            {
                throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name);
            }

            return(HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan <byte>(data, offset, count)));
        }
Beispiel #9
0
            protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
            {
                // we're sealed and the base should have checked this already
                Debug.Assert(data != null);
                Debug.Assert(offset >= 0 && offset <= data.Length);
                Debug.Assert(count >= 0 && count <= data.Length);
                Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));

                return(HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan <byte>(data, offset, count)));
            }
Beispiel #10
0
            protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
            {
                if (hashAlgorithm != HashAlgorithmName.SHA1)
                {
                    // Matching DSACryptoServiceProvider's "I only understand SHA-1/FIPS 186-2" exception
                    throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name);
                }

                return(HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan <byte>(data, offset, count)));
            }
Beispiel #11
0
 protected virtual byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
 HashOneShotHelpers.HashData(hashAlgorithm, data);
Beispiel #12
0
 protected virtual byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
 HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan <byte>(data, offset, count));
Beispiel #13
0
 protected override bool TryHashData(ReadOnlySpan <byte> data, Span <byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
 HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten);