예제 #1
0
        /// <summary>
        ///     Computes the signature of a hash that was produced by the hash algorithm specified by "hashAlgorithm."
        /// </summary>
        public override byte[] SignHash(byte[] hash)
        {
            if (hash == null)
            {
                throw new ArgumentNullException("hash");
            }

            int estimatedSize;

            switch (KeySize)
            {
            case 256: estimatedSize = 64; break;

            case 384: estimatedSize = 96; break;

            case 521: estimatedSize = 132; break;

            default:
                // If we got here, the range of legal key sizes for ECDsaCng was expanded and someone didn't update this switch.
                // Since it isn't a fatal error to miscalculate the estimatedSize, don't throw an exception. Just truck along.
                estimatedSize = KeySize / 4;
                break;
            }

            unsafe
            {
                byte[] signature = CngAsymmetricAlgorithmCore.SignHash(Key, hash, AsymmetricPaddingMode.None, null, estimatedSize);
                return(signature);
            }
        }
예제 #2
0
        /// <summary>
        ///     Creates a new ECDsaCng object that will use the specified key. The key's
        ///     <see cref="CngKey.AlgorithmGroup" /> must be ECDsa. This constructor
        ///     creates a copy of the key. Hence, the caller can safely dispose of the
        ///     passed in key and continue using the ECDsaCng object.
        /// </summary>
        /// <param name="key">Key to use for ECDsa operations</param>
        /// <exception cref="ArgumentException">if <paramref name="key" /> is not an ECDsa key</exception>
        /// <exception cref="ArgumentNullException">if <paramref name="key" /> is null.</exception>
        public ECDsaCng(CngKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (key.AlgorithmGroup != CngAlgorithmGroup.ECDsa)
            {
                throw new ArgumentException(SR.Cryptography_ArgECDsaRequiresECDsaKey, "key");
            }

            Key = CngAsymmetricAlgorithmCore.Duplicate(key);
        }
예제 #3
0
파일: RSACng.cs 프로젝트: thiagodin/corefx
        /// <summary>
        ///     Creates a new RSACng object that will use the specified key. The key's
        ///     <see cref="CngKey.AlgorithmGroup" /> must be Rsa. This constructor
        ///     creates a copy of the key. Hence, the caller can safely dispose of the
        ///     passed in key and continue using the RSACng object.
        /// </summary>
        /// <param name="key">Key to use for RSA operations</param>
        /// <exception cref="ArgumentException">if <paramref name="key" /> is not an RSA key</exception>
        /// <exception cref="ArgumentNullException">if <paramref name="key" /> is null.</exception>
        public RSACng(CngKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (key.AlgorithmGroup != CngAlgorithmGroup.Rsa)
            {
                throw new ArgumentException(SR.Cryptography_ArgRSAaRequiresRSAKey, "key");
            }

            _legalKeySizesValue = s_legalKeySizes;
            Key = CngAsymmetricAlgorithmCore.Duplicate(key);
        }
예제 #4
0
        /// <summary>
        ///     Verifies that alleged signature of a hash is, in fact, a valid signature of that hash.
        /// </summary>
        public override bool VerifyHash(byte[] hash, byte[] signature)
        {
            if (hash == null)
            {
                throw new ArgumentNullException("hash");
            }
            if (signature == null)
            {
                throw new ArgumentNullException("signature");
            }

            unsafe
            {
                bool verified = CngAsymmetricAlgorithmCore.VerifyHash(Key, hash, signature, AsymmetricPaddingMode.None, null);
                return(verified);
            }
        }
예제 #5
0
        /// <summary>
        ///     Computes the signature of a hash that was produced by the hash algorithm specified by "hashAlgorithm."
        /// </summary>
        public override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
        {
            if (hash == null)
            {
                throw new ArgumentNullException("hash");
            }

            unsafe
            {
                byte[] signature = null;
                SignOrVerify(padding, hashAlgorithm, hash,
                             delegate(AsymmetricPaddingMode paddingMode, void *pPaddingInfo)
                {
                    int estimatedSize = KeySize / 8;
                    signature         = CngAsymmetricAlgorithmCore.SignHash(Key, hash, paddingMode, pPaddingInfo, estimatedSize);
                }
                             );
                return(signature);
            }
        }
예제 #6
0
        /// <summary>
        ///     Verifies that alleged signature of a hash is, in fact, a valid signature of that hash.
        /// </summary>
        public override bool VerifyHash(byte[] hash, byte[] signature, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding)
        {
            if (hash == null)
            {
                throw new ArgumentNullException("hash");
            }
            if (signature == null)
            {
                throw new ArgumentNullException("signature");
            }

            unsafe
            {
                bool verified = false;
                SignOrVerify(padding, hashAlgorithm, hash,
                             delegate(AsymmetricPaddingMode paddingMode, void *pPaddingInfo)
                {
                    verified = CngAsymmetricAlgorithmCore.VerifyHash(Key, hash, signature, paddingMode, pPaddingInfo);
                }
                             );
                return(verified);
            }
        }
예제 #7
0
 protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
 {
     return(CngAsymmetricAlgorithmCore.HashData(data, hashAlgorithm));
 }
예제 #8
0
 protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
 {
     return(CngAsymmetricAlgorithmCore.HashData(data, offset, count, hashAlgorithm));
 }