Beispiel #1
0
        public void RSAPKCS1SHA256RoundTripTest()
        {
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
            {
                RSAPKCS1SHA256SignatureDescription sd = new RSAPKCS1SHA256SignatureDescription();

                using (HashAlgorithm digestAlgorithm = sd.CreateDigest())
                {
                    // Create some data to sign
                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                    byte[] data = new byte[1025];
                    rng.GetBytes(data);
                    byte[] hash = digestAlgorithm.ComputeHash(data);

                    // Sign the data
                    AsymmetricSignatureFormatter formatter = sd.CreateFormatter(rsa);
                    byte[] signature = formatter.CreateSignature(hash);
                    Assert.IsNotNull(signature, "Failed to create a signature");

                    // Verify the signature
                    AsymmetricSignatureDeformatter deformatter = sd.CreateDeformatter(rsa);
                    Assert.IsTrue(deformatter.VerifySignature(hash, signature), "Failed to verify signature");
                }
            }
        }
Beispiel #2
0
        public void RSAPKCS1SHA256SignatureCreateTest()
        {
            RSAPKCS1SHA256SignatureDescription sd = new RSAPKCS1SHA256SignatureDescription();

            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
            {
                // We should be using the PKCS1 signature deformatter
                AsymmetricSignatureDeformatter deformatter = sd.CreateDeformatter(rsa);
                Assert.IsInstanceOfType(deformatter, typeof(RSAPKCS1SignatureDeformatter));

                // We should be using the PKCS1 signature formatter
                AsymmetricSignatureFormatter formatter = sd.CreateFormatter(rsa);
                Assert.IsInstanceOfType(formatter, typeof(RSAPKCS1SignatureFormatter));
            }

            // We should be using SHA256Managed for hashing
            using (HashAlgorithm digestAlgorithm = sd.CreateDigest())
            {
                Assert.IsInstanceOfType(digestAlgorithm, typeof(SHA256Managed));
            }
        }