/** * Creates an instance using the provided algorithm type. * * @param algorithm to use * @param key the key to use * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. * @since 1.11 */ public HmacUtils(final String algorithm, final String key) { this(algorithm, StringUtils.getBytesUtf8(key)); }
/** * Returns the digest for the input data. * * @param valueToDigest the input to use, treated as UTF-8 * @return the digest as a byte[] * @since 1.11 */ public byte[] hmac(final String valueToDigest) { return mac.doFinal(StringUtils.getBytesUtf8(valueToDigest)); }
/** * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>. * * @param data * Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} * @return MD5 digest */ public static byte[] md5(final String data) { return md5(StringUtils.getBytesUtf8(data)); }
/** * Tests a given String to see if it contains only valid characters within the alphabet. * The method treats whitespace and PAD as valid. * * @param basen String to test * @return <code>true</code> if all characters in the String are valid characters in the alphabet or if * the String is empty; <code>false</code>, otherwise * @see #isInAlphabet(byte[], boolean) */ public boolean isInAlphabet(final String basen) { return isInAlphabet(StringUtils.getBytesUtf8(basen), true); }
/** * Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>. * * @param data * Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} * @return MD2 digest * @since 1.7 */ public static byte[] md2(final String data) { return md2(StringUtils.getBytesUtf8(data)); }
/** * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. * * @param data * Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} * @return SHA-1 digest */ public static byte[] sha1(final String data) { return sha1(StringUtils.getBytesUtf8(data)); }
/** * Decodes a String containing characters in the Base-N alphabet. * * @param pArray * A String containing Base-N character data * @return a byte array containing binary data */ public byte[] decode(final String pArray) { return decode(StringUtils.getBytesUtf8(pArray)); }
/** * Creates an instance using the provided algorithm type. * * @param algorithm to use * @param key the key to use * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. * @since 1.11 */ public HmacUtils(final HmacAlgorithms algorithm, final String key) { this(algorithm.getName(), StringUtils.getBytesUtf8(key)); }
/** * An MD5 hash converted to hex should always be 32 characters. */ @Test public void testMd5HexLengthForBytes() { String hashMe = "this is some string that is longer than 32 characters"; String hash = DigestUtils.md5Hex(getBytesUtf8(hashMe)); assertEquals(32, hash.length()); hashMe = "length < 32"; hash = DigestUtils.md5Hex(getBytesUtf8(hashMe)); assertEquals(32, hash.length()); }
/** * Tests Base64.encodeBase64(). */ @Test public void testChunkedEncodeMultipleOf76() { final byte[] expectedEncode = Base64.encodeBase64(Base64TestData.DECODED, true); // convert to "\r\n" so we're equal to the old openssl encoding test // stored // in Base64TestData.ENCODED_76_CHARS_PER_LINE: final String actualResult = Base64TestData.ENCODED_76_CHARS_PER_LINE.replaceAll("\n", "\r\n"); final byte[] actualEncode = StringUtils.getBytesUtf8(actualResult); assertTrue("chunkedEncodeMultipleOf76", Arrays.equals(expectedEncode, actualEncode)); }
private void testEncodeDecode(final String plainText) { final String encodedText = Base64.encodeBase64String(StringUtils.getBytesUtf8(plainText)); final String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText)); assertEquals(plainText, decodedText); }
@Test public void testEncodeHexByteArrayHelloWorldUpperCaseHex() { final byte[] b = StringUtils.getBytesUtf8("Hello World"); final String expected = "48656C6C6F20576F726C64"; char[] actual; actual = Hex.encodeHex(b); assertFalse(expected.equals(new String(actual))); actual = Hex.encodeHex(b, true); assertFalse(expected.equals(new String(actual))); actual = Hex.encodeHex(b, false); assertTrue(expected.equals(new String(actual))); }
@Test public void testEncodeHexByteArrayHelloWorldLowerCaseHex() { final byte[] b = StringUtils.getBytesUtf8("Hello World"); final String expected = "48656c6c6f20576f726c64"; char[] actual; actual = Hex.encodeHex(b); assertEquals(expected, new String(actual)); actual = Hex.encodeHex(b, true); assertEquals(expected, new String(actual)); actual = Hex.encodeHex(b, false); assertFalse(expected.equals(new String(actual))); }
private void testBase64InBuffer(final int startPasSize, final int endPadSize) { final String content = "Hello World"; String encodedContent; final byte[] bytesUtf8 = StringUtils.getBytesUtf8(content); byte[] buffer = ArrayUtils.addAll(bytesUtf8, new byte[endPadSize]); buffer = ArrayUtils.addAll(new byte[startPasSize], buffer); final byte[] encodedBytes = new Base64().encode(buffer, startPasSize, bytesUtf8.length); encodedContent = StringUtils.newStringUtf8(encodedBytes); assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent); }
@Test public void testGetBytesUtf8() throws UnsupportedEncodingException { final String charsetName = "UTF-8"; testGetBytesUnchecked(charsetName); final byte[] expected = STRING_FIXTURE.getBytes(charsetName); final byte[] actual = StringUtils.getBytesUtf8(STRING_FIXTURE); Assert.assertTrue(Arrays.equals(expected, actual)); }
/** * An MD2 hash converted to hex should always be 32 characters. */ @Test public void testMd2HexLength() { String hashMe = "this is some string that is longer than 32 characters"; String hash = DigestUtils.md2Hex(getBytesUtf8(hashMe)); assertEquals(32, hash.length()); hashMe = "length < 32"; hash = DigestUtils.md2Hex(getBytesUtf8(hashMe)); assertEquals(32, hash.length()); }
/** * An MD2 hash should always be a 16 element byte[]. */ @Test public void testMd2Length() { String hashMe = "this is some string that is longer than 16 characters"; byte[] hash = DigestUtils.md2(getBytesUtf8(hashMe)); assertEquals(16, hash.length); hashMe = "length < 16"; hash = DigestUtils.md2(getBytesUtf8(hashMe)); assertEquals(16, hash.length); }
@SuppressWarnings("deprecation") // deliberate tests of deprecated code @Test public void testShaHex() throws IOException { // Examples from FIPS 180-1 assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc")); assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex(getBytesUtf8("abc"))); assertEquals( "84983e441c3bd26ebaae4aa1f95129e5e54670f1", DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq")); assertEquals(DigestUtils.shaHex(testData), DigestUtils.shaHex(new ByteArrayInputStream(testData))); }
@Test public void testSha256() throws IOException { // Examples from FIPS 180-2 assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", DigestUtils.sha256Hex("abc")); assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", DigestUtils.sha256Hex(getBytesUtf8("abc"))); assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")); assertEquals(DigestUtils.sha256Hex(testData), DigestUtils.sha256Hex(new ByteArrayInputStream(testData))); }
@Test public void testSha1Hex() throws IOException { // Examples from FIPS 180-1 assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex("abc")); assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex(getBytesUtf8("abc"))); assertEquals( "84983e441c3bd26ebaae4aa1f95129e5e54670f1", DigestUtils.sha1Hex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq")); assertEquals(DigestUtils.sha1Hex(testData), DigestUtils.sha1Hex(new ByteArrayInputStream(testData))); }