/** * @return the byte array that hex represents. This is meant for test use and should be rewritten if used in * production code. */ public static byte[] fromHex(String hex) { if (hex.length() % 2 != 0) throw new IllegalArgumentException("expected even number of characters"); byte[] result = new byte[hex.length() / 2]; for (int i = 0; i < result.length; i++) { result[i] = (byte) (hexValue(hex.charAt(i * 2)) * 16); result[i] += hexValue(hex.charAt(i * 2 + 1)); } return result; }
private void writeCoordinateComponent(OutputStream out, byte[] x) throws IOException { int offset = Bytes.numberOfLeadingZeros(x); int length = x.length - offset; Encode.int16(length, out); out.write(x, offset, length); }
/** * @return true if a < b, where the first [size] bytes are used as the numbers to check. */ public static boolean lt(byte[] a, byte[] b, int size) { for (int i = 0; i < size; i++) { if (a[i] != b[i]) { return lt(a[i], b[i]); } } return false; }
public static byte[] varInt(long value) { ByteBuffer buffer = ByteBuffer.allocate(9); varInt(value, buffer); buffer.flip(); return Bytes.truncate(buffer.array(), buffer.limit()); }
private static byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException { BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now()); LOG.debug("TTL: " + TTL + "s"); BigInteger numerator = TWO.pow(64); BigInteger powLength = BigInteger.valueOf(object.getPayloadBytesWithoutNonce().length + extraBytes); BigInteger denominator = BigInteger.valueOf(nonceTrialsPerByte).multiply(powLength.add(powLength.multiply(TTL).divide(BigInteger.valueOf(2).pow(16)))); return Bytes.expand(numerator.divide(denominator).toByteArray(), 8); }
/** * @return true if a < b, where the first [size] bytes are used as the numbers to check. */ public static boolean lt(byte[] a, byte[] b, int size) { for (int i = 0; i < size; i++) { if (a[i] != b[i]) { return lt(a[i], b[i]); } } return false; }
public PrivateKey(long version, long stream, String passphrase, long nonceTrialsPerByte, long extraBytes, Pubkey.Feature... features) { try { // FIXME: this is most definitely wrong this.privateSigningKey = Bytes.truncate(Security.sha512(passphrase.getBytes("UTF-8"), new byte[]{0}), 32); this.privateEncryptionKey = Bytes.truncate(Security.sha512(passphrase.getBytes("UTF-8"), new byte[]{1}), 32); this.pubkey = Security.createPubkey(version, stream, privateSigningKey, privateEncryptionKey, nonceTrialsPerByte, extraBytes, features); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
@Override public byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) { if (nonceTrialsPerByte == 0) nonceTrialsPerByte = NETWORK_NONCE_TRIALS_PER_BYTE; if (extraBytes == 0) extraBytes = NETWORK_EXTRA_BYTES; BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now()); BigInteger powLength = BigInteger.valueOf(object.getPayloadBytesWithoutNonce().length + extraBytes); BigInteger denominator = BigInteger.valueOf(nonceTrialsPerByte) .multiply( powLength.add( powLength.multiply(TTL).divide(TWO_POW_16) ) ); return Bytes.expand(TWO_POW_64.divide(denominator).toByteArray(), 8); }
@Override public byte[] calculateNonce(byte[] initialHash, byte[] target) { byte[] nonce = new byte[8]; MessageDigest mda; try { mda = MessageDigest.getInstance("SHA-512"); } catch (Exception e) { throw new RuntimeException(e); } do { inc(nonce); mda.update(nonce); mda.update(initialHash); } while (Bytes.lt(target, mda.digest(mda.digest()), 8)); return nonce; } }
/** * @return true if a < b. */ public static boolean lt(byte[] a, byte[] b) { byte[] max = (a.length > b.length ? a : b); byte[] min = (max == a ? b : a); int diff = max.length - min.length; for (int i = 0; i < max.length - min.length; i++) { if (max[i] != 0) return a != max; } for (int i = diff; i < max.length; i++) { if (max[i] != min[i - diff]) { return lt(max[i], min[i - diff]) == (a == max); } } return false; }
public InventoryVector getInventoryVector() { return new InventoryVector(Bytes.truncate(Security.doubleSha512(nonce, getPayloadBytesWithoutNonce()), 32)); }
private void writeCoordinateComponent(ByteBuffer buffer, byte[] x) { int offset = Bytes.numberOfLeadingZeros(x); int length = x.length - offset; Encode.int16(length, buffer); buffer.put(x, offset, length); }
public BitmessageAddress(String address) { try { this.address = address; byte[] bytes = Base58.decode(address.substring(3)); ByteArrayInputStream in = new ByteArrayInputStream(bytes); AccessCounter counter = new AccessCounter(); this.version = varInt(in, counter); this.stream = varInt(in, counter); this.ripe = Bytes.expand(bytes(in, bytes.length - counter.length() - 4), 20); // test checksum byte[] checksum = Security.doubleSha512(bytes, bytes.length - 4); byte[] expectedChecksum = bytes(in, 4); for (int i = 0; i < 4; i++) { if (expectedChecksum[i] != checksum[i]) throw new IllegalArgumentException("Checksum of address failed"); } if (version < 4) { checksum = Security.sha512(Arrays.copyOfRange(bytes, 0, counter.length()), ripe); this.tag = null; this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } else { checksum = Security.doubleSha512(Arrays.copyOfRange(bytes, 0, counter.length()), ripe); this.tag = Arrays.copyOfRange(checksum, 32, 64); this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } } catch (IOException e) { throw new RuntimeException(e); } }
/** * @return the byte array that hex represents. This is meant for test use and should be rewritten if used in * production code. */ public static byte[] fromHex(String hex) { if (hex.length() % 2 != 0) throw new IllegalArgumentException("expected even number of characters"); byte[] result = new byte[hex.length() / 2]; for (int i = 0; i < result.length; i++) { result[i] = (byte) (hexValue(hex.charAt(i * 2)) * 16); result[i] += hexValue(hex.charAt(i * 2 + 1)); } return result; }
/** * @return true if a < b. */ public static boolean lt(byte[] a, byte[] b) { byte[] max = (a.length > b.length ? a : b); byte[] min = (max == a ? b : a); int diff = max.length - min.length; for (int i = 0; i < max.length - min.length; i++) { if (max[i] != 0) return a != max; } for (int i = diff; i < max.length; i++) { if (max[i] != min[i - diff]) { return lt(max[i], min[i - diff]) == (a == max); } } return false; }
public InventoryVector getInventoryVector() { return new InventoryVector( Bytes.truncate(cryptography().doubleSha512(nonce, getPayloadBytesWithoutNonce()), 32) ); }
private void writeCoordinateComponent(OutputStream out, ECFieldElement coord) throws IOException { byte[] x = coord.getEncoded(); int offset = Bytes.numberOfLeadingZeros(x); int length = x.length - offset; Encode.int16(length, out); out.write(x, offset, length); }
public BitmessageAddress(String address) { try { this.address = address; byte[] bytes = Base58.decode(address.substring(3)); ByteArrayInputStream in = new ByteArrayInputStream(bytes); AccessCounter counter = new AccessCounter(); this.version = varInt(in, counter); this.stream = varInt(in, counter); this.ripe = Bytes.expand(bytes(in, bytes.length - counter.length() - 4), 20); // test checksum byte[] checksum = cryptography().doubleSha512(bytes, bytes.length - 4); byte[] expectedChecksum = bytes(in, 4); for (int i = 0; i < 4; i++) { if (expectedChecksum[i] != checksum[i]) throw new IllegalArgumentException("Checksum of address failed"); } if (version < 4) { checksum = cryptography().sha512(Arrays.copyOfRange(bytes, 0, counter.length()), ripe); this.tag = null; this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } else { checksum = cryptography().doubleSha512(Arrays.copyOfRange(bytes, 0, counter.length()), ripe); this.tag = Arrays.copyOfRange(checksum, 32, 64); this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } } catch (IOException e) { throw new ApplicationException(e); } }