示例#1
0
        internal static Platform.HashAlgorithm GetHashAlgorithm(AsymmetricAlgorithm algorithm)
#endif
        {
            var hashAlgorithm = AsymmetricKeyAlgorithmProviderFactory.GetHashAlgorithmEnum(algorithm);

            return(HashAlgorithmProvider.CreateHashAlgorithm(hashAlgorithm));
        }
        /// <inheritdoc />
        protected internal override byte[] SignHash(byte[] data)
        {
            var hashAlgorithm = AsymmetricKeyAlgorithmProviderFactory.GetHashAlgorithmEnum(this.Algorithm);
            var formatter     = this.GetSignatureFormatter();

            formatter.SetHashAlgorithm(hashAlgorithm.ToString());
            return(formatter.CreateSignature(data));
        }
        /// <inheritdoc />
        protected internal override bool VerifySignature(byte[] data, byte[] signature)
        {
            var hashAlgorithm = AsymmetricKeyAlgorithmProviderFactory.GetHashAlgorithmEnum(this.Algorithm);

            using (var hash = this.GetHashAlgorithm())
            {
                var deformatter = this.GetSignatureDeformatter();
                deformatter.SetHashAlgorithm(hashAlgorithm.ToString());
                return(deformatter.VerifySignature(hash.ComputeHash(data), signature));
            }
        }
 /// <inheritdoc />
 protected internal override bool VerifyHash(byte[] data, byte[] signature)
 {
     try
     {
         var deformatter   = this.GetSignatureDeformatter();
         var hashAlgorithm = AsymmetricKeyAlgorithmProviderFactory.GetHashAlgorithmEnum(this.Algorithm);
         deformatter.SetHashAlgorithm(hashAlgorithm.ToString());
         return(deformatter.VerifySignature(data, signature));
     }
     catch (CryptographicException)
     {
         return(false);
     }
 }
示例#5
0
        /// <summary>
        /// Gets the OID (or name) for a given hash algorithm.
        /// </summary>
        /// <param name="algorithm">The algorithm.</param>
        /// <returns>A non-empty string.</returns>
        internal static string GetHashAlgorithmOID(AsymmetricAlgorithm algorithm)
        {
            string algorithmName = HashAlgorithmProviderFactory.GetHashAlgorithmName(AsymmetricKeyAlgorithmProviderFactory.GetHashAlgorithmEnum(algorithm));

#if SILVERLIGHT
            // Windows Phone 8.0 and Silverlight both are missing the
            // CryptoConfig type. But that's ok since both platforms
            // accept the algorithm name directly as well as the OID
            // which we can't easily get to.
            return(algorithmName);
#else
            // Mono requires the OID, so we get it when we can.
            return(Platform.CryptoConfig.MapNameToOID(algorithmName));
#endif
        }
        /// <summary>
        /// Gets the string to pass to <see cref="Signature.GetInstance(string)"/>
        /// for a given algorithm.
        /// </summary>
        /// <param name="algorithm">The algorithm.</param>
        /// <returns>A non-empty string.</returns>
        /// <exception cref="System.NotSupportedException">Thrown if the algorithm is not supported.</exception>
        private static string GetSignatureName(AsymmetricAlgorithm algorithm)
        {
            string hashName = HashAlgorithmProviderFactory.GetHashAlgorithmName(AsymmetricKeyAlgorithmProviderFactory.GetHashAlgorithmEnum(algorithm));

            switch (algorithm)
            {
            case AsymmetricAlgorithm.RsaSignPkcs1Sha1:
            case AsymmetricAlgorithm.RsaSignPkcs1Sha256:
            case AsymmetricAlgorithm.RsaSignPkcs1Sha384:
            case AsymmetricAlgorithm.RsaSignPkcs1Sha512:
                return(hashName + "withRSA");

            case AsymmetricAlgorithm.RsaSignPssSha1:
            case AsymmetricAlgorithm.RsaSignPssSha256:
            case AsymmetricAlgorithm.RsaSignPssSha384:
            case AsymmetricAlgorithm.RsaSignPssSha512:
            default:
                throw new NotSupportedException();
            }
        }
示例#7
0
        /// <summary>
        /// Creates a hash algorithm instance that is appropriate for the given algorithm.T
        /// </summary>
        /// <returns>The hash algorithm.</returns>
        private System.Security.Cryptography.HashAlgorithm GetHashAlgorithm()
        {
            var hashAlgorithm = AsymmetricKeyAlgorithmProviderFactory.GetHashAlgorithmEnum(this.Algorithm);

            return(HashAlgorithmProvider.CreateHashAlgorithm(hashAlgorithm));
        }