public void WhenAlgorithmIsSupported_ReturnsExpectedAlgorithm(string algorithm, string expectedType)
        {
            var actual = HashAlgorithmFactory.Create(new HashAlgorithmName(algorithm));

            actual.Should().NotBeNull();
            actual.GetType().Name.Should().Be(expectedType);
        }
        public byte[] ComputeHash(string contentToSign)
        {
            var inputBytes = Encoding.UTF8.GetBytes(contentToSign);

            using (var hasher = HashAlgorithmFactory.Create(HashAlgorithm)) {
                var hashedData = hasher.ComputeHash(inputBytes);
                return(_rsa.SignHash(hashedData, HashAlgorithm, RSASignaturePadding.Pkcs1));
            }
        }
        public bool VerifySignature(string contentToSign, byte[] signature)
        {
            if (contentToSign == null)
            {
                throw new ArgumentNullException(nameof(contentToSign));
            }
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }

            var signedBytes = Encoding.UTF8.GetBytes(contentToSign);

            using (var hasher = HashAlgorithmFactory.Create(HashAlgorithm)) {
                var hashedData = hasher.ComputeHash(signedBytes);
                return(_rsa.VerifyHash(hashedData, signature, HashAlgorithm, RSASignaturePadding.Pkcs1));
            }
        }
        public void WhenAlgorithmIsNotSupported_ThrowsNotSupportedException()
        {
            Action act = () => HashAlgorithmFactory.Create(new HashAlgorithmName("unsupported"));

            act.Should().Throw <NotSupportedException>();
        }
        public void WhenAlgorithmIsEmpty_ThrowsArgumentException()
        {
            Action act = () => HashAlgorithmFactory.Create(new HashAlgorithmName());

            act.Should().Throw <ArgumentException>();
        }
 /// <summary>
 ///     Creates a new <see cref="ECDsaSignatureAlgorithm" />.
 /// </summary>
 /// <param name="hashAlgorithm">The name of the hash algorithm to use.</param>
 /// <param name="ecdsa">The Elliptic Curve Digital Signature Algorithm.</param>
 public ECDsaSignatureAlgorithm(HashAlgorithmName hashAlgorithm, ECDsa ecdsa)
 {
     HashAlgorithm = hashAlgorithm;
     _ecdsa        = ecdsa ?? throw new ArgumentNullException(nameof(ecdsa));
     _hasherPool   = PoolProvider.Create(new PooledHashAlgorithmPolicy(() => HashAlgorithmFactory.Create(HashAlgorithm)));
 }