Esempio n. 1
0
        public void TestSignAndVerifyHash()
        {
            byte[] data = new byte[100];
            new Random().NextBytes(data);

            foreach (HashAlgorithm ha in new HashAlgorithm[] { MD5.Create(), SHA1.Create(), SHA256.Create(), SHA384.Create(), SHA512.Create() })
            {
                Hash hash = Hash.FromBytes(ha.ComputeHash(data));
                Assert.AreEqual(CryptoConfig.MapNameToOID(ha.GetType().FullName), hash.AlgorithmOID);

                using (RSAPrivateKey key = new RSAPrivateKey())
                {
                    byte[] sig = key.SignHash(hash);

                    using (RSAPublicKey pub = key.PublicKey)
                    {
                        Assert.IsTrue(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))));
                        data[0] = (byte)~data[0];
                        Assert.IsFalse(pub.VerifyHash(sig, Hash.FromBytes(ha.ComputeHash(data))));
                    }
                }
            }
        }