/** Use {@link #of(byte[])} instead: this old name is ambiguous. */ @Deprecated public static Sha256Hash create(byte[] contents) { return of(contents); }
/** Use {@link #of(byte[])} instead: this old name is ambiguous. */ @Deprecated public static Sha256Hash create(byte[] contents) { return of(contents); }
/** Use {@link #of(byte[])} instead: this old name is ambiguous. */ @Deprecated public static Sha256Hash create(byte[] contents) { return of(contents); }
/** Use {@link #of(byte[])} instead: this old name is ambiguous. */ @Deprecated public static Sha256Hash create(byte[] contents) { return of(contents); }
/** * Creates a new instance containing the calculated (one-time) hash of the given file's contents. * * The file contents are read fully into memory, so this method should only be used with small files. * * @param file the file on which the hash value is calculated * @return a new instance containing the calculated (one-time) hash * @throws IOException if an error occurs while reading the file */ public static Sha256Hash of(File file) throws IOException { FileInputStream in = new FileInputStream(file); try { return of(ByteStreams.toByteArray(in)); } finally { in.close(); } }
/** * Creates a new instance containing the calculated (one-time) hash of the given file's contents. * * The file contents are read fully into memory, so this method should only be used with small files. * * @param file the file on which the hash value is calculated * @return a new instance containing the calculated (one-time) hash * @throws IOException if an error occurs while reading the file */ public static Sha256Hash of(File file) throws IOException { FileInputStream in = new FileInputStream(file); try { return of(ByteStreams.toByteArray(in)); } finally { in.close(); } }
/** * Creates a new instance containing the calculated (one-time) hash of the given file's contents. * * The file contents are read fully into memory, so this method should only be used with small files. * * @param file the file on which the hash value is calculated * @return a new instance containing the calculated (one-time) hash * @throws IOException if an error occurs while reading the file */ public static Sha256Hash of(File file) throws IOException { FileInputStream in = new FileInputStream(file); try { return of(ByteStreams.toByteArray(in)); } finally { in.close(); } }
/** * Creates a new instance containing the calculated (one-time) hash of the given file's contents. * * The file contents are read fully into memory, so this method should only be used with small files. * * @param file the file on which the hash value is calculated * @return a new instance containing the calculated (one-time) hash * @throws IOException if an error occurs while reading the file */ public static Sha256Hash of(File file) throws IOException { FileInputStream in = new FileInputStream(file); try { return of(ByteStreams.toByteArray(in)); } finally { in.close(); } }
public Sha256Hash getHash () { if (hash != null) { return hash; } //TODO for now we take the two anchor hashes, this works until we have multiple anchors.. List<byte[]> list = new ArrayList<>(); list.add(keyServer.getPubKey()); list.add(keyClient.getPubKey()); list.sort(UnsignedBytes.lexicographicalComparator()); ByteBuffer byteBuffer = ByteBuffer.allocate(list.get(0).length + list.get(1).length); byteBuffer.put(list.get(0)); byteBuffer.put(list.get(1)); hash = Sha256Hash.of(byteBuffer.array()); return hash; }
public static byte[] createSignature (ECKey pubkey, byte[] data) throws NoSuchProviderException, NoSuchAlgorithmException { return pubkey.sign(Sha256Hash.of(data)).encodeToDER(); }
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); }
@Override public boolean verify(Bytestring payload, Bytestring signature) { ECKey.ECDSASignature ecdsaSignature; ecdsaSignature = ECKey.ECDSASignature.decodeFromDER(signature.bytes); return ECKey.verify(Sha256Hash.of(payload.bytes).getBytes(),ecdsaSignature,vKey); }
@Override public Bytestring sign(Bytestring string) { ECKey.ECDSASignature ecdsaSignature = signingKey.sign(Sha256Hash.of(string.bytes)); return new Bytestring(ecdsaSignature.encodeToDER()); }
@Before public void before() { wallet = createMock(Wallet.class); ecKey = createMock(ECKey.class); maxValue = Coin.COIN; serverHash = Sha256Hash.of("serverId".getBytes()); connection = createMock(IPaymentChannelClient.ClientConnection.class); clientVersionCapture = new Capture<>(); }
@Override public void bitcoinSerializeToStream(OutputStream stream) throws IOException { // Add some hashes. addItem(new InventoryItem(InventoryItem.Type.Transaction, Sha256Hash.of(new byte[]{1}))); addItem(new InventoryItem(InventoryItem.Type.Transaction, Sha256Hash.of(new byte[]{2}))); addItem(new InventoryItem(InventoryItem.Type.Transaction, Sha256Hash.of(new byte[]{3}))); // Write out a copy that's truncated in the middle. ByteArrayOutputStream bos = new ByteArrayOutputStream(); super.bitcoinSerializeToStream(bos); byte[] bits = bos.toByteArray(); bits = Arrays.copyOf(bits, bits.length / 2); stream.write(bits); } }.bitcoinSerialize(), out);
@Test public void autosaveImmediate() throws Exception { // Test that the wallet will save itself automatically when it changes. File f = File.createTempFile("bitcoinj-unit-test", null); Sha256Hash hash1 = Sha256Hash.of(f); // Start with zero delay and ensure the wallet file changes after adding a key. wallet.autosaveToFile(f, 0, TimeUnit.SECONDS, null); ECKey key = wallet.freshReceiveKey(); Sha256Hash hash2 = Sha256Hash.of(f); assertFalse("Wallet not saved after generating fresh key", hash1.equals(hash2)); // File has changed. Transaction t1 = createFakeTx(PARAMS, valueOf(5, 0), key); if (wallet.isPendingTransactionRelevant(t1)) wallet.receivePending(t1, null); Sha256Hash hash3 = Sha256Hash.of(f); assertFalse("Wallet not saved after receivePending", hash2.equals(hash3)); // File has changed again. }
@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); }
@Test public void testJavaSerialization() throws Exception { ECKey key = new ECKey(); UTXO utxo = new UTXO(Sha256Hash.of(new byte[]{1,2,3}), 1, Coin.COIN, 10, true, ScriptBuilder.createOutputScript(key)); ByteArrayOutputStream os = new ByteArrayOutputStream(); new ObjectOutputStream(os).writeObject(utxo); UTXO utxoCopy = (UTXO) new ObjectInputStream( new ByteArrayInputStream(os.toByteArray())).readObject(); assertEquals(utxo, utxoCopy); assertEquals(utxo.getValue(), utxoCopy.getValue()); assertEquals(utxo.getHeight(), utxoCopy.getHeight()); assertEquals(utxo.isCoinbase(), utxoCopy.isCoinbase()); assertEquals(utxo.getScript(), utxoCopy.getScript()); } }
@Test public void keyRecovery() throws Exception { ECKey key = new ECKey(); String message = "Hello World!"; Sha256Hash hash = Sha256Hash.of(message.getBytes()); ECKey.ECDSASignature sig = key.sign(hash); key = ECKey.fromPublicOnly(key.getPubKeyPoint()); boolean found = false; for (int i = 0; i < 4; i++) { ECKey key2 = ECKey.recoverFromSignature(i, sig, hash, true); checkNotNull(key2); if (key.equals(key2)) { found = true; break; } } assertTrue(found); }
@Test public void keyRecoveryWithEncryptedKey() throws Exception { ECKey unencryptedKey = new ECKey(); KeyParameter aesKey = keyCrypter.deriveKey(PASSWORD1); ECKey encryptedKey = unencryptedKey.encrypt(keyCrypter, aesKey); String message = "Goodbye Jupiter!"; Sha256Hash hash = Sha256Hash.of(message.getBytes()); ECKey.ECDSASignature sig = encryptedKey.sign(hash, aesKey); unencryptedKey = ECKey.fromPublicOnly(unencryptedKey.getPubKeyPoint()); boolean found = false; for (int i = 0; i < 4; i++) { ECKey key2 = ECKey.recoverFromSignature(i, sig, hash, true); checkNotNull(key2); if (unencryptedKey.equals(key2)) { found = true; break; } } assertTrue(found); }