private Sha3Hash(byte[] data, boolean hashed) { if (hashed) { this.data = data; } else { this.data = HashUtil.sha3(data); } }
/** * Calculates RIGTMOST160(SHA3(input)). This is used in address * calculations. * * * @param input - data * @return - 20 right bytes of the hash keccak of the data */ public static byte[] sha3omit12(byte[] input) { byte[] hash = sha3(input); return copyOfRange(hash, 12, hash.length); }
/** * Get the headerHash for signing. * * @return hash of header */ public byte[] getHashForSigning() { return HashUtil.sha3(this.toBinary()); }
public byte[] getHash() { ByteArrayOutputStream bao = new ByteArrayOutputStream(); try { bao.write(this.header.toBinary()); bao.write(this.signature); } catch (IOException e) { log.warn("getHash() ioException"); throw new NotValidateException(); } return HashUtil.sha3(bao.toByteArray()); }
private String approveKey(String sender, String spender) { byte[] approveKeyByteArray = ByteUtil.merge(sender.getBytes(), spender.getBytes()); byte[] approveKey = HashUtil.sha3(approveKeyByteArray); return Hex.toHexString(approveKey); }
/** * Verify the signature with plain data. * * @param data plain data for signed * @param signature signature * @return verification result */ public boolean verify(byte[] data, byte[] signature) { ECKey.ECDSASignature sig = new ECKey.ECDSASignature(signature); return key.verify(HashUtil.sha3(data), sig); }
/** * Sign the plain data. * * @param data plain data * @return signature as byte[65] */ public byte[] sign(byte[] data) { return key.sign(HashUtil.sha3(data)).toBinary(); }
public static byte[] calculatePubKey(byte[] data, byte[] signature, boolean hashed) { ECKey.ECDSASignature ecdsaSignature = new ECKey.ECDSASignature(signature); byte[] hashedData = hashed ? data : HashUtil.sha3(data); ECKey ecKeyPub; try { ecKeyPub = ECKey.signatureToKey(hashedData, ecdsaSignature); } catch (SignatureException e) { logger.debug("Invalid signature" + e.getMessage()); return null; } return ecKeyPub.getPubKey(); }
@Test public void test1() { byte[] result = sha3("horse".getBytes()); assertEquals("c87f65ff3f271bf5dc8643484f66b200109caffe4bf98c4cb393dc35740b28c0", Hex.toHexString(result)); result = sha3("cow".getBytes()); assertEquals("c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4", Hex.toHexString(result)); }
@Test public void testVerifySignature1() { ECKey key = ECKey.fromPublicOnly(pubKey); BigInteger r = new BigInteger("95350169487015575001444507567851457309689354890536757640816472151471942911739"); BigInteger s = new BigInteger("53263359985948886716508128220321578640585687230908938312863706887350789467339"); ECDSASignature sig = ECDSASignature.fromComponents(r.toByteArray(), s.toByteArray(), (byte) 28); assertTrue(key.verify(HashUtil.sha3(exampleMessage.getBytes()), sig)); }
@Test /* real block hash calc */ public void test8() { String blockRaw = "F885F8818080A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347940000000000000000000000000000000000000000A0BCDDD284BF396739C224DBA0411566C891C32115FEB998A3E2B4E61F3F35582AA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D4934783800000808080C0C0"; byte[] blockHashB = sha3(Hex.decode(blockRaw)); String blockHash = Hex.toHexString(blockHashB); log.debug(blockHash); }
/** * Verified via https://etherchain.org/verify/signature */ @Test public void testEthereumSignToHex() { ECKey key = ECKey.fromPrivate(privateKey); byte[] messageHash = HashUtil.sha3(exampleMessage.getBytes()); ECDSASignature signature = key.sign(messageHash); String output = signature.toHex(); log.debug("Signature\t: " + output + " (Hex, length: " + output.length() + ")"); assertEquals(signatureHex, output); }
@Test public void testEthereumSign() { ECKey key = ECKey.fromPrivate(privateKey); log.debug("Secret\t: " + Hex.toHexString(key.getPrivKeyBytes())); log.debug("Pubkey\t: " + Hex.toHexString(key.getPubKey())); log.debug("Data\t: " + exampleMessage); byte[] messageHash = HashUtil.sha3(exampleMessage.getBytes()); ECDSASignature signature = key.sign(messageHash); String output = signature.toBase64(); log.debug("Sign\t: " + output + " (Base64, length: " + output.length() + ")"); assertEquals(sigBase64, output); }
@Test public void test4() { byte[] cowBytes = sha3("cow".getBytes()); byte[] addr = ECKey.fromPrivate(cowBytes).getAddress(); assertEquals("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826", Hex.toHexString(addr).toUpperCase()); }
@Test public void test5() { byte[] horseBytes = sha3("horse".getBytes()); byte[] addr = ECKey.fromPrivate(horseBytes).getAddress(); assertEquals("13978AEE95F38490E9769C39B2773ED763D9CD5F", Hex.toHexString(addr).toUpperCase()); }
@Test public void testVerifySignature5() { ECKey key = ECKey.fromPrivate(privateKey); byte[] messageHash = HashUtil.sha3(exampleMessage.getBytes()); ECDSASignature signature = key.sign(messageHash); assertTrue(key.verify(messageHash, signature)); }
@Test public void testSignedMessageToKey() throws SignatureException { byte[] messageHash = HashUtil.sha3(exampleMessage.getBytes()); ECKey key = ECKey.signatureToKey(messageHash, sigBase64); assertNotNull(key); assertArrayEquals(pubKey, key.getPubKey()); }
@Test /* real tx hash calc */ public void test7() { String txRaw = "F89D80809400000000000000000000000000000000000000008609184E72A000822710B3606956330C0D630000003359366000530A0D630000003359602060005301356000533557604060005301600054630000000C5884336069571CA07F6EB94576346488C6253197BDE6A7E59DDC36F2773672C849402AA9C402C3C4A06D254E662BF7450DD8D835160CBB053463FED0B53F2CDD7F3EA8731919C8E8CC"; byte[] txHashB = sha3(Hex.decode(txRaw)); String txHash = Hex.toHexString(txHashB); assertEquals("4b7d9670a92bf120d5b43400543b69304a14d767cf836a7f6abff4edde092895", txHash); }
private void testProviderRoundTrip(Provider provider) { ECKey key = new ECKey(provider, secureRandom); String message = "The quick brown fox jumps over the lazy dog."; byte[] input = HashUtil.sha3(message.getBytes()); ECDSASignature sig = key.sign(input); assertTrue(sig.validateComponents()); assertTrue(key.verify(input, sig)); }
@Test public void testSignVerify() { ECKey key = ECKey.fromPrivate(privateKey); String message = "This is an example of a signed message."; byte[] input = HashUtil.sha3(message.getBytes()); ECDSASignature sig = key.sign(input); assertTrue(sig.validateComponents()); assertTrue(key.verify(input, sig)); }