byte[] res = sig.encodeToDER();
public static MasternodeSignature signMessage(String strMessage, StringBuilder errorMessage, ECKey key) { //ECKey ecKey = ECKey.fromPublicOnly(key.getBytes()); try { byte dataToHash [] = (Utils.BITCOIN_SIGNED_MESSAGE_HEADER_BYTES+strMessage).getBytes(Charsets.UTF_8); ECKey.ECDSASignature signature = key.sign(Sha256Hash.twiceOf(dataToHash)); return new MasternodeSignature(signature.encodeToDER()); } catch (KeyCrypterException x) { } errorMessage.append("Sign failed"); return null; } public static boolean verifyMessage(PublicKey pubkey, MasternodeSignature vchSig, String strMessage, StringBuilder errorMessage)
public static String sign(ECKey key, String input) throws UnsupportedEncodingException { byte[] data = input.getBytes("UTF8"); Sha256Hash hash = Sha256Hash.of(data); ECDSASignature sig = key.sign(hash, null); byte[] bytes = sig.encodeToDER(); return bytesToHex(bytes); }
bos.write(coinbaseOutKey.sign(hash).encodeToDER()); bos.write(SigHash.SINGLE.value); byte[] signature = bos.toByteArray(); ByteArrayOutputStream bos = new UnsafeByteArrayOutputStream( 73); bos.write(coinbaseOutKey.sign(hash).encodeToDER()); bos.write(SigHash.SINGLE.value); byte[] signature = bos.toByteArray();
return NativeSecp256k1.verify(data, signature.encodeToDER(), pub); } catch (NativeSecp256k1Util.AssertFailException e) { log.error("Caught AssertFailException inside secp256k1", e);
return NativeSecp256k1.verify(data, signature.encodeToDER(), pub); } catch (NativeSecp256k1Util.AssertFailException e) { log.error("Caught AssertFailException inside secp256k1", e);
@Test public void testKeyPairRoundtrip() throws Exception { byte[] privkeyASN1 = HEX.decode( "3082011302010104205c0b98e524ad188ddef35dc6abba13c34a351a05409e5d285403718b93336a4aa081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a144034200042af7a2aafe8dafd7dc7f9cfb58ce09bda7dce28653ab229b98d1d3d759660c672dd0db18c8c2d76aa470448e876fc2089ab1354c01a6e72cefc50915f4a963ee"); ECKey decodedKey = ECKey.fromASN1(privkeyASN1); // Now re-encode and decode the ASN.1 to see if it is equivalent (it does not produce the exact same byte // sequence, some integers are padded now). ECKey roundtripKey = ECKey.fromPrivateAndPrecalculatedPublic(decodedKey.getPrivKey(), decodedKey.getPubKeyPoint()); for (ECKey key : new ECKey[] {decodedKey, roundtripKey}) { byte[] message = reverseBytes(HEX.decode( "11da3761e86431e4a54c176789e41f1651b324d240d599a7067bee23d328ec2a")); byte[] output = key.sign(Sha256Hash.wrap(message)).encodeToDER(); assertTrue(key.verify(message, output)); output = HEX.decode( "304502206faa2ebc614bf4a0b31f0ce4ed9012eb193302ec2bcaccc7ae8bb40577f47549022100c73a1a1acc209f3f860bf9b9f5e13e9433db6f8b7bd527a088a0e0cd0a4c83e9"); assertTrue(key.verify(message, output)); } // Try to sign with one key and verify with the other. byte[] message = reverseBytes(HEX.decode( "11da3761e86431e4a54c176789e41f1651b324d240d599a7067bee23d328ec2a")); assertTrue(roundtripKey.verify(message, decodedKey.sign(Sha256Hash.wrap(message)).encodeToDER())); assertTrue(decodedKey.verify(message, roundtripKey.sign(Sha256Hash.wrap(message)).encodeToDER())); // Verify bytewise equivalence of public keys (i.e. compression state is preserved) ECKey key = new ECKey(); ECKey key2 = ECKey.fromASN1(key.toASN1()); assertArrayEquals(key.getPubKey(), key2.getPubKey()); }
return NativeSecp256k1.verify(data, signature.encodeToDER(), pub); } catch (NativeSecp256k1Util.AssertFailException e) { log.error("Caught AssertFailException inside secp256k1", e);
return NativeSecp256k1.verify(data, signature.encodeToDER(), pub); } catch (NativeSecp256k1Util.AssertFailException e) { log.error("Caught AssertFailException inside secp256k1", e);
@Test public void testASN1Roundtrip() throws Exception { byte[] privkeyASN1 = HEX.decode( "3082011302010104205c0b98e524ad188ddef35dc6abba13c34a351a05409e5d285403718b93336a4aa081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a144034200042af7a2aafe8dafd7dc7f9cfb58ce09bda7dce28653ab229b98d1d3d759660c672dd0db18c8c2d76aa470448e876fc2089ab1354c01a6e72cefc50915f4a963ee"); ECKey decodedKey = ECKey.fromASN1(privkeyASN1); // Now re-encode and decode the ASN.1 to see if it is equivalent (it does not produce the exact same byte // sequence, some integers are padded now). ECKey roundtripKey = ECKey.fromASN1(decodedKey.toASN1()); assertArrayEquals(decodedKey.getPrivKeyBytes(), roundtripKey.getPrivKeyBytes()); for (ECKey key : new ECKey[] {decodedKey, roundtripKey}) { byte[] message = reverseBytes(HEX.decode( "11da3761e86431e4a54c176789e41f1651b324d240d599a7067bee23d328ec2a")); byte[] output = key.sign(Sha256Hash.wrap(message)).encodeToDER(); assertTrue(key.verify(message, output)); output = HEX.decode( "304502206faa2ebc614bf4a0b31f0ce4ed9012eb193302ec2bcaccc7ae8bb40577f47549022100c73a1a1acc209f3f860bf9b9f5e13e9433db6f8b7bd527a088a0e0cd0a4c83e9"); assertTrue(key.verify(message, output)); } // Try to sign with one key and verify with the other. byte[] message = reverseBytes(HEX.decode( "11da3761e86431e4a54c176789e41f1651b324d240d599a7067bee23d328ec2a")); assertTrue(roundtripKey.verify(message, decodedKey.sign(Sha256Hash.wrap(message)).encodeToDER())); assertTrue(decodedKey.verify(message, roundtripKey.sign(Sha256Hash.wrap(message)).encodeToDER())); }
@Test public void testCreatedSigAndPubkeyAreCanonical() throws Exception { // Tests that we will not generate non-canonical pubkeys or signatures // We dump failed data to error log because this test is not expected to be deterministic ECKey key = new ECKey(); if (!ECKey.isPubKeyCanonical(key.getPubKey())) { log.error(Utils.HEX.encode(key.getPubKey())); fail(); } byte[] hash = new byte[32]; new Random().nextBytes(hash); byte[] sigBytes = key.sign(Sha256Hash.wrap(hash)).encodeToDER(); byte[] encodedSig = Arrays.copyOf(sigBytes, sigBytes.length + 1); encodedSig[sigBytes.length] = Transaction.SigHash.ALL.byteValue(); if (!TransactionSignature.isEncodingCanonical(encodedSig)) { log.error(Utils.HEX.encode(sigBytes)); fail(); } }
@Test public void testSignatures() throws Exception { // Test that we can construct an ECKey from a private key (deriving the public from the private), then signing // a message with it. BigInteger privkey = new BigInteger(1, HEX.decode("180cb41c7c600be951b5d3d0a7334acc7506173875834f7a6c4c786a28fcbb19")); ECKey key = ECKey.fromPrivate(privkey); byte[] output = key.sign(Sha256Hash.ZERO_HASH).encodeToDER(); assertTrue(key.verify(Sha256Hash.ZERO_HASH.getBytes(), output)); // Test interop with a signature from elsewhere. byte[] sig = HEX.decode( "3046022100dffbc26774fc841bbe1c1362fd643609c6e42dcb274763476d87af2c0597e89e022100c59e3c13b96b316cae9fa0ab0260612c7a133a6fe2b3445b6bf80b3123bf274d"); assertTrue(key.verify(Sha256Hash.ZERO_HASH.getBytes(), sig)); }
public static MasternodeSignature signHash(Sha256Hash hash, ECKey key) { ECKey.ECDSASignature signature = key.sign(hash); return new MasternodeSignature(signature.encodeToDER()); }
@Test public void testSomething() { String msg = "hello world"; Sha256Hash hash = Sha256Hash.of(msg.getBytes()); ECKey signingKey = new ECKey(); ECKey verificationKey = ECKey.fromPublicOnly(signingKey.getPubKeyPoint()); ECKey.ECDSASignature sig = signingKey.sign(hash); boolean isVerified = verificationKey.verify(hash, sig); assertTrue("msg " + msg + " can not be signed with " + Arrays.toString(sig.encodeToDER()), isVerified); }
public static byte[] createSignature (ECKey pubkey, byte[] data) throws NoSuchProviderException, NoSuchAlgorithmException { return pubkey.sign(Sha256Hash.of(data)).encodeToDER(); }
@Override public Bytestring sign(Bytestring string) { ECKey.ECDSASignature ecdsaSignature = signingKey.sign(Sha256Hash.of(string.bytes)); return new Bytestring(ecdsaSignature.encodeToDER()); }