Exemplo n.º 1
0
        public async Task <bool> VerifyAsync(byte[] digest, byte[] signature, string algorithm, CancellationToken token = default(CancellationToken))
        {
            if (_csp == null)
            {
                throw new ObjectDisposedException(string.Format("RsaKey {0} is disposed", Kid));
            }

            if (digest == null)
            {
                throw new ArgumentNullException("digest");
            }

            if (signature == null)
            {
                throw new ArgumentNullException("signature");
            }

            if (algorithm == null)
            {
                algorithm = DefaultSignatureAlgorithm;
            }

            AsymmetricSignatureAlgorithm algo = AlgorithmResolver.Default[algorithm] as AsymmetricSignatureAlgorithm;

            if (algo == null)
            {
                throw new NotSupportedException("algorithm is not supported");
            }

            return(algo.VerifyHash(_csp, digest, signature));
        }
Exemplo n.º 2
0
        public async Task <Tuple <byte[], string> > SignAsync(byte[] digest, string algorithm, CancellationToken token = default(CancellationToken))
        {
            if (_csp == null)
            {
                throw new ObjectDisposedException(string.Format("RsaKey {0} is disposed", Kid));
            }

            if (algorithm == null)
            {
                algorithm = DefaultSignatureAlgorithm;
            }

            if (digest == null)
            {
                throw new ArgumentNullException("digest");
            }

            if (_csp.PublicOnly)
            {
                throw new NotSupportedException("Sign is not supported because no private key is available");
            }

            AsymmetricSignatureAlgorithm algo = AlgorithmResolver.Default[algorithm] as AsymmetricSignatureAlgorithm;

            if (algo == null)
            {
                throw new NotSupportedException("algorithm is not supported");
            }

            return(new Tuple <byte[], string>(algo.SignHash(_csp, digest), algorithm));
        }
Exemplo n.º 3
0
        public async Task <Tuple <byte[], string> > SignAsync(byte[] digest, string algorithm = null, CancellationToken token = default(CancellationToken))
        {
            if (_certificate == null)
            {
                throw new ObjectDisposedException(string.Format("Certificate {0} is disposed", Kid));
            }

            if (!_certificate.HasPrivateKey)
            {
                throw new NotSupportedException("Certificate does not have a private key");
            }

            if (digest == null)
            {
                throw new ArgumentNullException("digest");
            }

            if (algorithm == null)
            {
                algorithm = DefaultSignatureAlgorithm;
            }

            AsymmetricSignatureAlgorithm algo = AlgorithmResolver.Default[algorithm] as AsymmetricSignatureAlgorithm;

            if (algo == null)
            {
                throw new NotSupportedException("algorithm is not supported");
            }

            return(new Tuple <byte[], string>(algo.SignHash(_certificate.PrivateKey, digest), algorithm));
        }
Exemplo n.º 4
0
        public Task <bool> VerifyAsync(byte[] digest, byte[] signature, string algorithm, CancellationToken token = default(CancellationToken))
        {
            if (_csp == null)
            {
                throw new ObjectDisposedException(string.Format("RsaKey {0} is disposed", Kid));
            }

            if (digest == null)
            {
                throw new ArgumentNullException("digest");
            }

            if (signature == null)
            {
                throw new ArgumentNullException("signature");
            }

            if (algorithm == null)
            {
                algorithm = DefaultSignatureAlgorithm;
            }

            AsymmetricSignatureAlgorithm algo      = AlgorithmResolver.Default[algorithm] as AsymmetricSignatureAlgorithm;
            ISignatureTransform          transform = algo != null?algo.CreateSignatureTransform(_csp) : null;

            if (algo == null || transform == null)
            {
                throw new NotSupportedException("algorithm is not supported");
            }

            try
            {
                var result = transform.Verify(digest, signature);

                return(Task.FromResult(result));
            }
            catch (Exception ex)
            {
                return(TaskException.FromException <bool>(ex));
            }
        }
Exemplo n.º 5
0
        public Task <Tuple <byte[], string> > SignAsync(byte[] digest, string algorithm, CancellationToken token = default(CancellationToken))
        {
            if (_csp == null)
            {
                throw new ObjectDisposedException(string.Format("RsaKey {0} is disposed", Kid));
            }

            if (algorithm == null)
            {
                algorithm = DefaultSignatureAlgorithm;
            }

            if (digest == null)
            {
                throw new ArgumentNullException("digest");
            }

            // TODO: Not available via the RSA class
            //if ( _csp.PublicOnly )
            //    throw new NotSupportedException( "Sign is not supported because no private key is available" );

            AsymmetricSignatureAlgorithm algo      = AlgorithmResolver.Default[algorithm] as AsymmetricSignatureAlgorithm;
            ISignatureTransform          transform = algo != null?algo.CreateSignatureTransform(_csp) : null;

            if (algo == null || transform == null)
            {
                throw new NotSupportedException("algorithm is not supported");
            }

            try
            {
                var result = new Tuple <byte[], string>(transform.Sign(digest), algorithm);

                return(Task.FromResult(result));
            }
            catch (Exception ex)
            {
                return(TaskException.FromException <Tuple <byte[], string> >(ex));
            }
        }