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 static byte[] SignDataHash(byte [] Hash, string privateKey) { EllipticCurveDsa ellipticCurveDsa = new EllipticCurveDsa(CurveOID, EllipticCurveAlgorithm.EcDsaSha2Nistp256); byte[] privateKeyByte = Convert.FromBase64String(privateKey); ellipticCurveDsa.FromPrivateKey(privateKeyByte); byte[] signature = ellipticCurveDsa.SignHash(Hash); return(signature); }
public void ShouldSignAndVerifyMessageWithoutKey() { 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); byte[] message = new BigInteger("968236873715988614170569073515315707566766479517").ToByteArray(); byte[] signature = ellipticCurveDsa.SignMessage(message); bool isCorrect = ellipticCurveDsa.VerifyMessage(message, signature); bool isFake = ellipticCurveDsaFake.VerifyMessage(message, signature); Assert.True(isCorrect, "Signature does not match message"); Assert.False(isFake, "Fake Signature does not match message"); //Both values will not be true because no common keys were used in building the curves }
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)); }
public void ShouldSignAndVerifyMessage() { EllipticCurveAlgorithm curve = EllipticCurveAlgorithm.Create(EllipticCurveAlgorithm.EcDsaSha2Nistp256); var privateKey = curve.GetPrivateKey(); EllipticCurveDsa ellipticCurveDsa = new EllipticCurveDsa("1.2.840.10045.3.1.7", EllipticCurveAlgorithm.EcDsaSha2Nistp256); ellipticCurveDsa.FromPrivateKey(privateKey); EllipticCurveDsa ellipticCurveDsaFake = new EllipticCurveDsa("1.2.840.10045.3.1.7", EllipticCurveAlgorithm.EcDsaSha2Nistp256); ellipticCurveDsaFake.FromPrivateKey(privateKey); byte[] message = new BigInteger("968236873715988614170569073515315707566766479517").ToByteArray(); byte[] signature = ellipticCurveDsa.SignMessage(message); bool isCorrect = ellipticCurveDsa.VerifyMessage(message, signature); bool isFake = ellipticCurveDsaFake.VerifyMessage(message, signature); Assert.Equal(isCorrect, isFake); //Both values are true because the curves were built from the same private key }