public static bool VerifySignature(byte[] publickKey, byte [] signature, byte [] hashToVerify) { EllipticCurveDsa ellipticCurveDsa = new EllipticCurveDsa(CurveOID, EllipticCurveAlgorithm.EcDsaSha2Nistp256); ellipticCurveDsa.FromPublicKey(publickKey); // build the curve from the public key return(ellipticCurveDsa.VerifyHash(hashToVerify, signature)); }
public void ShouldSignAndVerifyDataHash() { EllipticCurveAlgorithm curve = EllipticCurveAlgorithm.Create(EllipticCurveAlgorithm.EcDsaSha2Nistp256); var privateKey = curve.GetPrivateKey(); EllipticCurveDsa ellipticCurveDsa = new EllipticCurveDsa("1.2.840.10045.3.1.7", EllipticCurveAlgorithm.EcDsaSha2Nistp256); EllipticCurveDsa ellipticCurveDsaFake = new EllipticCurveDsa("1.2.840.10045.3.1.7", EllipticCurveAlgorithm.EcDsaSha2Nistp256); ellipticCurveDsa.FromPrivateKey(privateKey); ellipticCurveDsaFake.FromPrivateKey(privateKey); SHA256 sHA256 = SHA256.Create(); byte[] hashBytes = sHA256.ComputeHash(Encoding.Default.GetBytes("Message to be hashed")); byte[] signature = ellipticCurveDsa.SignHash(hashBytes); bool isCorrect = ellipticCurveDsa.VerifyHash(hashBytes, signature); bool isFake = ellipticCurveDsaFake.VerifyHash(hashBytes, signature); Assert.Equal(isCorrect, isFake); }
public void ShouldVerifyTransactionUsingPublicKey() { EllipticCurveAlgorithm curve = EllipticCurveAlgorithm.Create(EllipticCurveAlgorithm.EcDsaSha2Nistp256); var privateKey = curve.GetPrivateKey(); var pubKey = curve.GetPublicKey(); //The first curve is created from the private key EllipticCurveDsa ellipticCurveDsa = new EllipticCurveDsa("1.2.840.10045.3.1.7", EllipticCurveAlgorithm.EcDsaSha2Nistp256); ellipticCurveDsa.FromPrivateKey(privateKey); //The second curve is created from the public key of the first curve and is used to verify the transaction //The public key can be easily shared which makes it easy for verification EllipticCurveDsa ellipticCurveDsaFake = new EllipticCurveDsa("1.2.840.10045.3.1.7", EllipticCurveAlgorithm.EcDsaSha2Nistp256); ellipticCurveDsaFake.FromPublicKey(pubKey); SHA256 sHA256 = SHA256.Create(); byte[] hashBytes = sHA256.ComputeHash(Encoding.Default.GetBytes("Message to be hashed")); byte[] signature = ellipticCurveDsa.SignHash(hashBytes); bool isCorrect = ellipticCurveDsa.VerifyHash(hashBytes, signature); bool isFake = ellipticCurveDsaFake.VerifyHash(hashBytes, signature); Assert.Equal(isCorrect, isFake); //Perform same operation using ChainUtility byte[] signature2 = ChainUtility.SignDataHash(hashBytes, Convert.ToBase64String(privateKey)); var newKeyPair = ChainUtility.GenerateNewKeyPair(); Assert.True(ChainUtility.VerifySignature(pubKey, signature2, hashBytes)); Assert.False(ChainUtility.VerifySignature(Convert.FromBase64String(newKeyPair.PublicKey), signature2, hashBytes)); }