ProofOfWorkBuilder { public ProofOfWork build(int magic, byte[] seed, int leading) { if (seed.length != 32 || leading < 1 || leading > 256) { throw new IllegalArgumentException(); } BitSet targetBitSet = new BitSet(256); targetBitSet.set(0, 256); targetBitSet.clear(0, (leading / 8) * 8); targetBitSet.clear((leading / 8) * 8 + (8 - leading % 8), (leading / 8) * 8 + 8); byte[] target = targetBitSet.toByteArray(); ByteBuffer buffer = ByteBuffer.allocate(32 + 4 + Long.BYTES); long nonce = 1; buffer.putInt(magic); buffer.put(seed); String targetHex = Bytes.toHexString(target); while (true) { buffer.position(32 + 4); buffer.putLong(nonce); String hashHex = Bytes.toHexString(Hash.hash256(buffer.array())); if (hashHex.compareTo(targetHex) < 0) { return new ProofOfWork(nonce, magic, seed, target); } nonce++; } }  ProofOfWork build(int magic, byte[] seed, int leading);  }
@Test public void test() throws ProofOfWorkException { int magic = 12345; byte[] seed = new byte[32]; ProofOfWork pow = new ProofOfWorkBuilder().build(magic, seed, 16); pow.validate(); }