static String encodeBool(Bool value) { byte[] rawValue = new byte[MAX_BYTE_LENGTH]; if (value.getValue()) { rawValue[rawValue.length - 1] = 1; } return Numeric.toHexStringNoPrefix(rawValue); }
public static String toHexStringWithPrefixSafe(BigInteger value) { String result = toHexStringNoPrefix(value); if (result.length() < 2) { result = Strings.zeros(1) + result; } return HEX_PREFIX + result; }
static String encodeBytes(BytesType bytesType) { byte[] value = bytesType.getValue(); int length = value.length; int mod = length % MAX_BYTE_LENGTH; byte[] dest; if (mod != 0) { int padding = MAX_BYTE_LENGTH - mod; dest = new byte[length + padding]; System.arraycopy(value, 0, dest, 0, length); } else { dest = value; } return Numeric.toHexStringNoPrefix(dest); }
private static String toHexStringZeroPadded(BigInteger value, int size, boolean withPrefix) { String result = toHexStringNoPrefix(value); int length = result.length(); if (length > size) { throw new UnsupportedOperationException( "Value " + result + "is larger then length " + size); } else if (value.signum() < 0) { throw new UnsupportedOperationException("Value cannot be negative"); } if (length < size) { result = Strings.zeros(size - length) + result; } if (withPrefix) { return HEX_PREFIX + result; } else { return result; } }
static String encodeNumeric(NumericType numericType) { byte[] rawValue = toByteArray(numericType); byte paddingValue = getPaddingValue(numericType); byte[] paddedRawValue = new byte[MAX_BYTE_LENGTH]; if (paddingValue != 0) { for (int i = 0; i < paddedRawValue.length; i++) { paddedRawValue[i] = paddingValue; } } System.arraycopy( rawValue, 0, paddedRawValue, MAX_BYTE_LENGTH - rawValue.length, rawValue.length); return Numeric.toHexStringNoPrefix(paddedRawValue); }
@Test public void testToHexStringNoPrefix() { assertThat(Numeric.toHexStringNoPrefix(BigInteger.TEN), is("a")); }
Numeric.toHexStringNoPrefix( Numeric.toBytesPadded( new BigInteger(Long.toString(offset)), MAX_BYTE_LENGTH
private static WalletFile createWalletFile( ECKeyPair ecKeyPair, byte[] cipherText, byte[] iv, byte[] salt, byte[] mac, int n, int p) { WalletFile walletFile = new WalletFile(); walletFile.setAddress(Keys.getAddress(ecKeyPair)); WalletFile.Crypto crypto = new WalletFile.Crypto(); crypto.setCipher(CIPHER); crypto.setCiphertext(Numeric.toHexStringNoPrefix(cipherText)); WalletFile.CipherParams cipherParams = new WalletFile.CipherParams(); cipherParams.setIv(Numeric.toHexStringNoPrefix(iv)); crypto.setCipherparams(cipherParams); crypto.setKdf(SCRYPT); WalletFile.ScryptKdfParams kdfParams = new WalletFile.ScryptKdfParams(); kdfParams.setDklen(DKLEN); kdfParams.setN(n); kdfParams.setP(p); kdfParams.setR(R); kdfParams.setSalt(Numeric.toHexStringNoPrefix(salt)); crypto.setKdfparams(kdfParams); crypto.setMac(Numeric.toHexStringNoPrefix(mac)); walletFile.setCrypto(crypto); walletFile.setId(UUID.randomUUID().toString()); walletFile.setVersion(CURRENT_VERSION); return walletFile; }
@Test public void testGetAddressZeroPadded() { byte[] address = Keys.getAddress( Numeric.toBytesPadded(BigInteger.valueOf(0x1234), Keys.PUBLIC_KEY_SIZE)); String expected = Numeric.toHexStringNoPrefix(address); String value = "1234"; assertThat(Keys.getAddress("0x" + Strings.zeros(Keys.PUBLIC_KEY_LENGTH_IN_HEX - value.length()) + value), equalTo(expected)); }
@Test public void testGetAddressSmallPublicKey() { byte[] address = Keys.getAddress( Numeric.toBytesPadded(BigInteger.valueOf(0x1234), Keys.PUBLIC_KEY_SIZE)); String expected = Numeric.toHexStringNoPrefix(address); assertThat(Keys.getAddress("0x1234"), equalTo(expected)); }
@Test public void testDecryptScrypt() throws Exception { WalletFile walletFile = load(SCRYPT); ECKeyPair ecKeyPair = Wallet.decrypt(PASSWORD, walletFile); assertThat(Numeric.toHexStringNoPrefix(ecKeyPair.getPrivateKey()), is(SECRET)); }
@Test public void testDecryptAes128Ctr() throws Exception { WalletFile walletFile = load(AES_128_CTR); ECKeyPair ecKeyPair = Wallet.decrypt(PASSWORD, walletFile); assertThat(Numeric.toHexStringNoPrefix(ecKeyPair.getPrivateKey()), is(SECRET)); }
static String encodeBool(Bool value) { byte[] rawValue = new byte[MAX_BYTE_LENGTH]; if (value.getValue()) { rawValue[rawValue.length - 1] = 1; } return Numeric.toHexStringNoPrefix(rawValue); }
static String encodeBytes(BytesType bytesType) { byte[] value = bytesType.getValue(); int length = value.length; int mod = length % MAX_BYTE_LENGTH; byte[] dest; if (mod != 0) { int padding = MAX_BYTE_LENGTH - mod; dest = new byte[length + padding]; System.arraycopy(value, 0, dest, 0, length); } else { dest = value; } return Numeric.toHexStringNoPrefix(dest); }
private static Boolean validateChecksumEthereumAddress(String address) { address = address.replace("0x", ""); String hash = Numeric.toHexStringNoPrefix(Hash.sha3(address.toLowerCase().getBytes())); for (int i = 0; i < 40; i++) { if (Character.isLetter(address.charAt(i))) { // each uppercase letter should correlate with a first bit of 1 in the hash // char with the same index, and each lowercase letter with a 0 bit int charInt = Integer.parseInt(Character.toString(hash.charAt(i)), 16); if ((Character.isUpperCase(address.charAt(i)) && charInt <= 7) || (Character.isLowerCase(address.charAt(i)) && charInt > 7)) { return false; } } } return true; } }
private static String toHexStringZeroPadded(BigInteger value, int size, boolean withPrefix) { String result = toHexStringNoPrefix(value); int length = result.length(); if (length > size) { throw new UnsupportedOperationException( "Value " + result + "is larger then length " + size); } else if (value.signum() < 0) { throw new UnsupportedOperationException("Value cannot be negative"); } if (length < size) { result = Strings.zeros(size - length) + result; } if (withPrefix) { return HEX_PREFIX + result; } else { return result; } }
static String encodeNumeric(NumericType numericType) { byte[] rawValue = toByteArray(numericType); byte paddingValue = getPaddingValue(numericType); byte[] paddedRawValue = new byte[MAX_BYTE_LENGTH]; if (paddingValue != 0) { for (int i = 0; i < paddedRawValue.length; i++) { paddedRawValue[i] = paddingValue; } } System.arraycopy( rawValue, 0, paddedRawValue, MAX_BYTE_LENGTH - rawValue.length, rawValue.length); return Numeric.toHexStringNoPrefix(paddedRawValue); }
Numeric.toHexStringNoPrefix( Numeric.toBytesPadded( new BigInteger(Long.toString(offset)), MAX_BYTE_LENGTH