Beispiel #1
0
        public override bool TrySign(ReadOnlySpan <byte> data, Span <byte> destination, out int bytesWritten)
        {
            Debug.Assert(!_disposed);
            if (data.IsEmpty)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.data);
            }

            var rsa = _rsaPool.Get();

            try
            {
#if SUPPORT_SPAN_CRYPTO
                Span <byte> hash = stackalloc byte[Sha2.HashSizeStackallocThreshold].Slice(0, _sha.HashSize);
                _sha.ComputeHash(data, hash);
                return(rsa.TrySignHash(hash, destination, _hashAlgorithm, _signaturePadding, out bytesWritten));
#else
                byte[] hash = new byte[_sha.HashSize];
                _sha.ComputeHash(data, hash);
                var result = rsa.SignHash(hash, _hashAlgorithm, _signaturePadding);
                bytesWritten = result.Length;
                result.CopyTo(destination);
                return(true);
#endif
            }
            finally
            {
                _rsaPool.Return(rsa);
            }
        }
        /// <inheritsdoc />
        public override bool Verify(ReadOnlySpan <byte> data, ReadOnlySpan <byte> signature)
        {
            Debug.Assert(!_disposed);
            if (data.IsEmpty)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.data);
            }

            if (signature.IsEmpty)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.signature);
            }

            var ecdsa = _ecdsaPool.Get();

#if SUPPORT_SPAN_CRYPTO
            Span <byte> hash = stackalloc byte[Sha2.BlockSizeStackallocThreshold].Slice(0, _sha.HashSize);
            _sha.ComputeHash(data, hash);
            return(ecdsa.VerifyHash(hash, signature));
#else
            byte[] hash = new byte[_sha.HashSize];
            _sha.ComputeHash(data, hash);
            return(ecdsa.VerifyHash(hash, signature.ToArray()));
#endif
        }
Beispiel #3
0
        public override bool Verify(ReadOnlySpan <byte> data, ReadOnlySpan <byte> signature)
        {
            Debug.Assert(!_disposed);
            if (data.IsEmpty)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.data);
            }

            if (signature.IsEmpty)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.signature);
            }

            var rsa = _rsaPool.Get();

            try
            {
#if SUPPORT_SPAN_CRYPTO
                Span <byte> hash = stackalloc byte[_sha.HashSize];
                _sha.ComputeHash(data, hash);
                return(rsa.VerifyHash(hash, signature, _hashAlgorithm, _signaturePadding));
#else
                byte[] hash = new byte[_sha.HashSize];
                _sha.ComputeHash(data, hash);
                return(rsa.VerifyHash(hash, signature.ToArray(), _hashAlgorithm, _signaturePadding));
#endif
            }
            finally
            {
                _rsaPool.Return(rsa);
            }
        }
Beispiel #4
0
        /// <inheritsdoc />
        public override bool TrySign(ReadOnlySpan <byte> data, Span <byte> destination, out int bytesWritten)
        {
            Debug.Assert(!_disposed);
            if (data.IsEmpty)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.data);
            }

            var ecdsa = _ecdsaPool.Get();

#if SUPPORT_SPAN_CRYPTO
            Span <byte> hash = stackalloc byte[_sha.HashSize];
            _sha.ComputeHash(data, hash);
            return(ecdsa.TrySignHash(hash, destination, out bytesWritten));
#else
            byte[] hash = new byte[_sha.HashSize];
            _sha.ComputeHash(data, hash);
            var result = ecdsa.SignHash(hash);
            bytesWritten = result.Length;
            result.CopyTo(destination);
            return(true);
#endif
        }
Beispiel #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HmacSha2"/> class.
        /// </summary>
        /// <param name="sha2"></param>
        /// <param name="key"></param>
        public HmacSha2(Sha2 sha2, ReadOnlySpan <byte> key)
        {
            Debug.Assert(sha2 != null);

            Sha2 = sha2;
            int blockSize = sha2.BlockSize;

            _keys = ArrayPool <byte> .Shared.Rent(blockSize * 2);

            _innerPadKey = new ReadOnlyMemory <byte>(_keys, 0, blockSize);
            _outerPadKey = new ReadOnlyMemory <byte>(_keys, blockSize, blockSize);
            if (key.Length > blockSize)
            {
                Span <byte> keyPrime = stackalloc byte[sha2.HashSize];
                Sha2.ComputeHash(key, default, keyPrime, default);
Beispiel #6
0
 public static void ComputeHash(this Sha2 sha2, ReadOnlySpan <byte> source, Span <byte> destination)
 {
     sha2.ComputeHash(source, default, destination, default);