/** * Calculates the SHA-256 hash of the given byte range. * * @param input the array containing the bytes to hash * @param offset the offset within the array of the bytes to hash * @param length the number of bytes to hash * @return the hash (in big-endian order) */ public static byte[] hash(byte[] input, int offset, int length) { MessageDigest digest = newDigest(); digest.update(input, offset, length); return digest.digest(); }
/** * Calculates the SHA-256 hash of the given byte range. * * @param input the array containing the bytes to hash * @param offset the offset within the array of the bytes to hash * @param length the number of bytes to hash * @return the hash (in big-endian order) */ public static byte[] hash(byte[] input, int offset, int length) { MessageDigest digest = newDigest(); digest.update(input, offset, length); return digest.digest(); }
/** * Calculates the SHA-256 hash of the given byte range. * * @param input the array containing the bytes to hash * @param offset the offset within the array of the bytes to hash * @param length the number of bytes to hash * @return the hash (in big-endian order) */ public static byte[] hash(byte[] input, int offset, int length) { MessageDigest digest = newDigest(); digest.update(input, offset, length); return digest.digest(); }
/** * Calculates the hash of hash on the given byte ranges. This is equivalent to * concatenating the two ranges and then passing the result to {@link #hashTwice(byte[])}. */ public static byte[] hashTwice(byte[] input1, int offset1, int length1, byte[] input2, int offset2, int length2) { MessageDigest digest = newDigest(); digest.update(input1, offset1, length1); digest.update(input2, offset2, length2); return digest.digest(digest.digest()); }
/** * Calculates the SHA-256 hash of the given byte range. * * @param input the array containing the bytes to hash * @param offset the offset within the array of the bytes to hash * @param length the number of bytes to hash * @return the hash (in big-endian order) */ public static byte[] hash(byte[] input, int offset, int length) { MessageDigest digest = newDigest(); digest.update(input, offset, length); return digest.digest(); }
/** * Calculates the SHA-256 hash of the given byte range, * and then hashes the resulting hash again. * * @param input the array containing the bytes to hash * @param offset the offset within the array of the bytes to hash * @param length the number of bytes to hash * @return the double-hash (in big-endian order) */ public static byte[] hashTwice(byte[] input, int offset, int length) { MessageDigest digest = newDigest(); digest.update(input, offset, length); return digest.digest(digest.digest()); }
/** * Calculates the hash of hash on the given byte ranges. This is equivalent to * concatenating the two ranges and then passing the result to {@link #hashTwice(byte[])}. */ public static byte[] hashTwice(byte[] input1, int offset1, int length1, byte[] input2, int offset2, int length2) { MessageDigest digest = newDigest(); digest.update(input1, offset1, length1); digest.update(input2, offset2, length2); return digest.digest(digest.digest()); }
/** * Calculates the hash of hash on the given byte ranges. This is equivalent to * concatenating the two ranges and then passing the result to {@link #hashTwice(byte[])}. */ public static byte[] hashTwice(byte[] input1, int offset1, int length1, byte[] input2, int offset2, int length2) { MessageDigest digest = newDigest(); digest.update(input1, offset1, length1); digest.update(input2, offset2, length2); return digest.digest(digest.digest()); }
/** * Calculates the SHA-256 hash of the given byte range, * and then hashes the resulting hash again. * * @param input the array containing the bytes to hash * @param offset the offset within the array of the bytes to hash * @param length the number of bytes to hash * @return the double-hash (in big-endian order) */ public static byte[] hashTwice(byte[] input, int offset, int length) { MessageDigest digest = newDigest(); digest.update(input, offset, length); return digest.digest(digest.digest()); }
/** * Calculates the hash of hash on the given byte ranges. This is equivalent to * concatenating the two ranges and then passing the result to {@link #hashTwice(byte[])}. */ public static byte[] hashTwice(byte[] input1, int offset1, int length1, byte[] input2, int offset2, int length2) { MessageDigest digest = newDigest(); digest.update(input1, offset1, length1); digest.update(input2, offset2, length2); return digest.digest(digest.digest()); }
/** * Calculates the SHA-256 hash of the given byte range, * and then hashes the resulting hash again. * * @param input the array containing the bytes to hash * @param offset the offset within the array of the bytes to hash * @param length the number of bytes to hash * @return the double-hash (in big-endian order) */ public static byte[] hashTwice(byte[] input, int offset, int length) { MessageDigest digest = newDigest(); digest.update(input, offset, length); return digest.digest(digest.digest()); }
/** * Calculates the SHA-256 hash of the given byte range, * and then hashes the resulting hash again. * * @param input the array containing the bytes to hash * @param offset the offset within the array of the bytes to hash * @param length the number of bytes to hash * @return the double-hash (in big-endian order) */ public static byte[] hashTwice(byte[] input, int offset, int length) { MessageDigest digest = newDigest(); digest.update(input, offset, length); return digest.digest(digest.digest()); }
private void initializeFromWords(ArrayList<String> wordList, String wordListDigest) throws IllegalArgumentException { if (wordList.size() != 2048) throw new IllegalArgumentException("input wordlist did not contain 2048 words"); this.wordList = wordList; // If a wordListDigest is supplied check to make sure it matches. if (wordListDigest != null) { MessageDigest md = Sha256Hash.newDigest(); for (String word : wordList) md.update(word.getBytes()); byte[] digest = md.digest(); String hexdigest = HEX.encode(digest); if (!hexdigest.equals(wordListDigest)) throw new IllegalArgumentException("wordlist digest mismatch"); } }
/** * Creates an MnemonicCode object, initializing with words read from the supplied input stream. If a wordListDigest * is supplied the digest of the words will be checked. */ public MnemonicCode(InputStream wordstream, String wordListDigest) throws IOException, IllegalArgumentException { BufferedReader br = new BufferedReader(new InputStreamReader(wordstream, "UTF-8")); this.wordList = new ArrayList<String>(2048); MessageDigest md = Sha256Hash.newDigest(); String word; while ((word = br.readLine()) != null) { md.update(word.getBytes()); this.wordList.add(word); } br.close(); if (this.wordList.size() != 2048) throw new IllegalArgumentException("input stream did not contain 2048 words"); // If a wordListDigest is supplied check to make sure it matches. if (wordListDigest != null) { byte[] digest = md.digest(); String hexdigest = HEX.encode(digest); if (!hexdigest.equals(wordListDigest)) throw new IllegalArgumentException("wordlist digest mismatch"); } }
/** * Creates an MnemonicCode object, initializing with words read from the supplied input stream. If a wordListDigest * is supplied the digest of the words will be checked. */ public MnemonicCode(InputStream wordstream, String wordListDigest) throws IOException, IllegalArgumentException { BufferedReader br = new BufferedReader(new InputStreamReader(wordstream, "UTF-8")); this.wordList = new ArrayList<String>(2048); MessageDigest md = Sha256Hash.newDigest(); String word; while ((word = br.readLine()) != null) { md.update(word.getBytes()); this.wordList.add(word); } br.close(); if (this.wordList.size() != 2048) throw new IllegalArgumentException("input stream did not contain 2048 words"); // If a wordListDigest is supplied check to make sure it matches. if (wordListDigest != null) { byte[] digest = md.digest(); String hexdigest = HEX.encode(digest); if (!hexdigest.equals(wordListDigest)) throw new IllegalArgumentException("wordlist digest mismatch"); } }
/** * Creates an MnemonicCode object, initializing with words read from the supplied input stream. If a wordListDigest * is supplied the digest of the words will be checked. */ public MnemonicCode(InputStream wordstream, String wordListDigest) throws IOException, IllegalArgumentException { BufferedReader br = new BufferedReader(new InputStreamReader(wordstream, "UTF-8")); this.wordList = new ArrayList<>(2048); MessageDigest md = Sha256Hash.newDigest(); String word; while ((word = br.readLine()) != null) { md.update(word.getBytes()); this.wordList.add(word); } br.close(); if (this.wordList.size() != 2048) throw new IllegalArgumentException("input stream did not contain 2048 words"); // If a wordListDigest is supplied check to make sure it matches. if (wordListDigest != null) { byte[] digest = md.digest(); String hexdigest = HEX.encode(digest); if (!hexdigest.equals(wordListDigest)) throw new IllegalArgumentException("wordlist digest mismatch"); } }
private static void writeBinaryCheckpoints(TreeMap<Integer, StoredBlock> checkpoints, File file) throws Exception { final FileOutputStream fileOutputStream = new FileOutputStream(file, false); MessageDigest digest = Sha256Hash.newDigest(); final DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest); digestOutputStream.on(false); final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream); dataOutputStream.writeBytes("CHECKPOINTS 1"); dataOutputStream.writeInt(0); // Number of signatures to read. Do this later. digestOutputStream.on(true); dataOutputStream.writeInt(checkpoints.size()); ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE); for (StoredBlock block : checkpoints.values()) { block.serializeCompact(buffer); dataOutputStream.write(buffer.array()); buffer.position(0); } dataOutputStream.close(); Sha256Hash checkpointsHash = Sha256Hash.wrap(digest.digest()); System.out.println("Hash of checkpoints data is " + checkpointsHash); digestOutputStream.close(); fileOutputStream.close(); System.out.println("Checkpoints written to '" + file.getCanonicalPath() + "'."); }
/** * Calculate the merkle branch root based on the supplied hashes and the given leaf hash. * Used to verify that the given leaf and root are part of the same tree. */ public Sha256Hash calculateMerkleRoot(final Sha256Hash leaf) { byte[] target = leaf.getReversedBytes(); long mask = index; MessageDigest digest = Sha256Hash.newDigest(); for (Sha256Hash hash: hashes) { digest.reset(); if ((mask & 1) == 0) { // 0 means it goes on the right digest.update(target); digest.update(hash.getReversedBytes()); } else { digest.update(hash.getReversedBytes()); digest.update(target); } // Double-digest the values target = digest.digest(); digest.reset(); target = digest.digest(target); mask >>= 1; } return Sha256Hash.wrapReversed(target); }
private Sha256Hash readBinary(InputStream inputStream) throws IOException { DataInputStream dis = null; try { MessageDigest digest = Sha256Hash.newDigest(); DigestInputStream digestInputStream = new DigestInputStream(inputStream, digest); dis = new DataInputStream(digestInputStream);
private Sha256Hash readBinary(InputStream inputStream) throws IOException { DataInputStream dis = null; try { MessageDigest digest = Sha256Hash.newDigest(); DigestInputStream digestInputStream = new DigestInputStream(inputStream, digest); dis = new DataInputStream(digestInputStream);