@Test public void testGetBytesIso8859_1() throws UnsupportedEncodingException { final String charsetName = "ISO-8859-1"; testGetBytesUnchecked(charsetName); final byte[] expected = STRING_FIXTURE.getBytes(charsetName); final byte[] actual = StringUtils.getBytesIso8859_1(STRING_FIXTURE); Assert.assertTrue(Arrays.equals(expected, actual)); }
/** * Tests if negative arguments to skip are handled correctly. * * @throws Throwable */ @Test(expected=IllegalArgumentException.class) public void testSkipWrongArgument() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); b32stream.skip(-10); b32stream.close(); } }
/** * Tests if negative arguments to skip are handled correctly. * * @throws Throwable */ @Test(expected=IllegalArgumentException.class) public void testSkipWrongArgument() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); b64stream.skip(-10); b64stream.close(); } }
/** * Tests skipping to the end of a stream. * * @throws Throwable */ @Test public void testSkipToEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); // due to CODEC-130, skip now skips correctly decoded characters rather than encoded assertEquals(3, b32stream.skip(3)); // End of stream reached assertEquals(-1, b32stream.read()); assertEquals(-1, b32stream.read()); b32stream.close(); }
/** * Tests skipping past the end of a stream. * * @throws Throwable */ @Test public void testSkipPastEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); // due to CODEC-130, skip now skips correctly decoded characters rather than encoded assertEquals(3, b32stream.skip(10)); // End of stream reached assertEquals(-1, b32stream.read()); assertEquals(-1, b32stream.read()); b32stream.close(); }
/** * Tests skipping number of characters larger than the internal buffer. * * @throws Throwable */ @Test public void testSkipBig() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); assertEquals(3, b32stream.skip(1024)); // End of stream reached assertEquals(-1, b32stream.read()); assertEquals(-1, b32stream.read()); b32stream.close(); }
/** * Tests skipping number of characters larger than the internal buffer. * * @throws Throwable */ @Test public void testSkipBig() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); assertEquals(6, b64stream.skip(Integer.MAX_VALUE)); // End of stream reached assertEquals(-1, b64stream.read()); assertEquals(-1, b64stream.read()); b64stream.close(); }
/** * Tests skipping past the end of a stream. * * @throws Throwable */ @Test public void testSkipPastEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); // due to CODEC-130, skip now skips correctly decoded characters rather than encoded assertEquals(6, b64stream.skip(10)); // End of stream reached assertEquals(-1, b64stream.read()); assertEquals(-1, b64stream.read()); b64stream.close(); }
/** * Tests skipping to the end of a stream. * * @throws Throwable */ @Test public void testSkipToEnd() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); // due to CODEC-130, skip now skips correctly decoded characters rather than encoded assertEquals(6, b64stream.skip(6)); // End of stream reached assertEquals(-1, b64stream.read()); assertEquals(-1, b64stream.read()); b64stream.close(); }
/** * Tests skipping as a noop * * @throws Throwable */ @Test public void testSkipNone() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); final byte[] actualBytes = new byte[6]; assertEquals(0, b32stream.skip(0)); b32stream.read(actualBytes, 0, actualBytes.length); assertArrayEquals(actualBytes, new byte[] { 102, 111, 111, 0, 0, 0 }); // End of stream reached assertEquals(-1, b32stream.read()); b32stream.close(); }
/** * Tests skipping as a noop * * @throws Throwable */ @Test public void testSkipNone() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); final byte[] actualBytes = new byte[6]; assertEquals(0, b64stream.skip(0)); b64stream.read(actualBytes, 0, actualBytes.length); assertArrayEquals(actualBytes, new byte[] { 0, 0, 0, (byte) 255, (byte) 255, (byte) 255 }); // End of stream reached assertEquals(-1, b64stream.read()); b64stream.close(); }
/** * Tests skipping past the end of a stream. * * @throws Throwable */ @Test public void testAvailable() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_B64)); final Base64InputStream b64stream = new Base64InputStream(ins); assertEquals(1, b64stream.available()); assertEquals(6, b64stream.skip(10)); // End of stream reached assertEquals(0, b64stream.available()); assertEquals(-1, b64stream.read()); assertEquals(-1, b64stream.read()); assertEquals(0, b64stream.available()); b64stream.close(); }
/** * Tests skipping past the end of a stream. * * @throws Throwable */ @Test public void testAvailable() throws Throwable { final InputStream ins = new ByteArrayInputStream(StringUtils.getBytesIso8859_1(ENCODED_FOO)); final Base32InputStream b32stream = new Base32InputStream(ins); assertEquals(1, b32stream.available()); assertEquals(3, b32stream.skip(10)); // End of stream reached assertEquals(0, b32stream.available()); assertEquals(-1, b32stream.read()); assertEquals(-1, b32stream.read()); assertEquals(0, b32stream.available()); b32stream.close(); }
/** * Recovers the ECDSA Public key from a message hash and signature * @param messageHash a 32-character string in Latin-1 encoding representing the 32-byte message hash of the signed message * @param v The recovery id, in the range 27-34, to use to recover the correct public key * @param r The r component of the message signature, as a 32-character Latin-1 string * @param s The s component of the message signature, as a 32-character Latin-1 string * @return Output String (64 characters) in Latin-1 encoding representing the public key recovered upon success. Returns * the empty string if key recovery fails due to invalid input. * */ public static StringToken ecdsaRecover(StringToken messageHash, IntToken v, StringToken r, StringToken s, TermContext context) { byte[] hashBytes = StringUtils.getBytesIso8859_1(messageHash.stringValue()); byte vByte = v.bigIntegerValue().byteValueExact(); byte[] rBytes = StringUtils.getBytesIso8859_1(r.stringValue()); byte[] sBytes = StringUtils.getBytesIso8859_1(s.stringValue()); try { ECDSARecover key = ECDSARecover.signatureToKey(hashBytes, rBytes, sBytes, vByte); return StringToken.of(Arrays.copyOfRange(key.getPubKey(), 1, 65)); } catch (SignatureException | IllegalArgumentException e) { return StringToken.of(""); } }
/** * Finds the keccak256 digest of the input. * * @param inputString - The String is expected to be formed such that each character in the string * represents a Latin-1 encoded byte. * @return Output String (256 characters) such that each character represents an encoded Hex Value. */ public static StringToken keccak256(StringToken inputString, TermContext context) { byte[] bytes = StringUtils.getBytesIso8859_1(inputString.stringValue()); Keccak.Digest256 keccakEngine = new Keccak.Digest256(); byte[] digest = keccakEngine.digest(bytes); String digestString = Hex.toHexString(digest); return StringToken.of(digestString); }
/** * Finds the SHA3 digest of the input. * * @param inputString - The String is expected to be formed such that each character in the string * represents a Latin-1 encoded byte. * @return Output String (256 characters) such that each character represents an encoded Hex Value. */ public static StringToken sha3256(StringToken inputString, TermContext context) { byte[] bytes = StringUtils.getBytesIso8859_1(inputString.stringValue()); SHA3.Digest256 sha3engine = new SHA3.Digest256(); byte[] digest = sha3engine.digest(bytes); String digestString = Hex.toHexString(digest); return StringToken.of(digestString); }
/** * Finds the SHA256 digest of the input. * * @param inputString - The String is expected to be formed such that each character in the string * represents a Latin-1 encoded byte. * @return Output String (256 characters) such that each character represents an encoded Hex Value. */ public static StringToken sha256(StringToken inputString, TermContext context) { byte[] bytes = StringUtils.getBytesIso8859_1(inputString.stringValue()); SHA256.Digest sha2engine = new SHA256.Digest(); byte[] digest = sha2engine.digest(bytes); String digestString = Hex.toHexString(digest); return StringToken.of(digestString); }
/** * Finds the RIPEMD160 digest of the input. * * @param inputString - The String is expected to be formed such that each character in the string * represents a Latin-1 encoded byte. * @return Output String (256 characters) such that each character represents an encoded Hex Value. */ public static StringToken ripemd160(StringToken inputString, TermContext context) { byte[] bytes = StringUtils.getBytesIso8859_1(inputString.stringValue()); RIPEMD160.Digest ripemd160engine = new RIPEMD160.Digest(); byte[] digest = ripemd160engine.digest(bytes); String digestString = Hex.toHexString(digest); return StringToken.of(digestString); }