예제 #1
0
        public void GetHashAlgorithmForSignatureFine()
        {
            X509AsymmetricSecurityKey k = new X509AsymmetricSecurityKey(cert);

            k.GetHashAlgorithmForSignature(SignedXml.XmlDsigRSASHA1Url);
            k.GetHashAlgorithmForSignature(SecurityAlgorithms.RsaSha256Signature);
        }
        public byte[] Sign(byte[] input)
        {
            X509Certificate2 x509Certificate2 = new X509Certificate2(
                Convert.FromBase64String(Keys.DefaultX509Data_2048),
                Keys.CertPassword,
                X509KeyStorageFlags.MachineKeySet);

            X509AsymmetricSecurityKey signSecurityKey = new X509AsymmetricSecurityKey(x509Certificate2);

            var algorithm = SecurityAlgorithms.RsaSha256Signature;

            var hash = signSecurityKey.GetHashAlgorithmForSignature(algorithm);

            var formatter = signSecurityKey.GetSignatureFormatter(algorithm);

            formatter.SetHashAlgorithm(hash.GetType().ToString());

            var sig = formatter.CreateSignature(hash.ComputeHash(input));

            return(sig);
        }
        public bool Verify(byte[] input, byte[] sig)
        {
            X509Certificate2 x509Certificate2 = new X509Certificate2(
                Convert.FromBase64String(Keys.DefaultX509Data_2048),
                Keys.CertPassword,
                X509KeyStorageFlags.MachineKeySet);

            var publicKey = x509Certificate2.Export(X509ContentType.Cert);

            X509Certificate2 cert = new X509Certificate2(publicKey);

            X509AsymmetricSecurityKey securityKey = new X509AsymmetricSecurityKey(cert);

            var algorithm = SecurityAlgorithms.RsaSha256Signature;

            var hash = securityKey.GetHashAlgorithmForSignature(algorithm);

            var deformatter = securityKey.GetSignatureDeformatter(algorithm);

            deformatter.SetHashAlgorithm(hash.GetType().ToString());

            return(deformatter.VerifySignature(hash.ComputeHash(input), sig));
        }