Refine search
private String[] getAuthHeaderTokens(HttpServletRequest request, String authType) throws HttpAuthenticationException { String authHeaderBase64 = getAuthHeader(request, authType); String authHeaderString = StringUtils.newStringUtf8( Base64.decodeBase64(authHeaderBase64.getBytes())); String[] creds = authHeaderString.split(":"); return creds; }
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); }
/** * Check if the Double Metaphone values of two <code>String</code> values * are equal, optionally using the alternate value. * * @param value1 The left-hand side of the encoded {@link String#equals(Object)}. * @param value2 The right-hand side of the encoded {@link String#equals(Object)}. * @param alternate use the alternate value if <code>true</code>. * @return <code>true</code> if the encoded <code>String</code>s are equal; * <code>false</code> otherwise. */ public boolean isDoubleMetaphoneEqual(final String value1, final String value2, final boolean alternate) { return StringUtils.equals(doubleMetaphone(value1, alternate), doubleMetaphone(value2, alternate)); }
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); }
/** * Another test for the CODEC-101 bug: In commons-codec-1.4 this test shows InputStreamReader explicitly hating an * InputStream.read(byte[]) return of 0: * * java.io.IOException: Underlying input stream returned zero bytes at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268) at * sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) at * java.io.InputStreamReader.read(InputStreamReader.java:167) at java.io.BufferedReader.fill(BufferedReader.java:136) at * java.io.BufferedReader.readLine(BufferedReader.java:299) at java.io.BufferedReader.readLine(BufferedReader.java:362) at * org.apache.commons.codec.binary.Base64InputStreamTest.testInputStreamReader(Base64InputStreamTest.java:75) * * But in commons-codec-1.5 it's fixed. :-) * * @throws Exception * for some failure scenarios. */ @Test public void testInputStreamReader() throws Exception { final byte[] codec101 = StringUtils.getBytesUtf8(Base64TestData.CODEC_101_MULTIPLE_OF_3); final ByteArrayInputStream bais = new ByteArrayInputStream(codec101); final Base64InputStream in = new Base64InputStream(bais); final InputStreamReader isr = new InputStreamReader(in); final BufferedReader br = new BufferedReader(isr); final String line = br.readLine(); assertNotNull("Codec101: InputStreamReader works!", line); br.close(); }
/** * Test the Base64InputStream implementation against the special NPE inducing input identified in the CODEC-98 bug. * * @throws Exception * for some failure scenarios. */ @Test public void testCodec98NPE() throws Exception { final byte[] codec98 = StringUtils.getBytesUtf8(Base64TestData.CODEC_98_NPE); final ByteArrayInputStream data = new ByteArrayInputStream(codec98); final Base64InputStream stream = new Base64InputStream(data); // This line causes an NPE in commons-codec-1.4.jar: final byte[] decodedBytes = Base64TestData.streamToBytes(stream, new byte[1024]); final String decoded = StringUtils.newStringUtf8(decodedBytes); assertEquals("codec-98 NPE Base64InputStream", Base64TestData.CODEC_98_NPE_DECODED, decoded); }
private InputStream streamDataUrl(String dataUrl) { int dataIndex = dataUrl.indexOf(','); String data = dataUrl.substring(dataIndex + 1); byte[] bytes; if (dataUrl.substring(0, dataIndex).endsWith(";base64")) { bytes = Base64.decodeBase64(data); } else { try { bytes = URLCodec.decodeUrl(StringUtils.getBytesUsAscii(data)); } catch (DecoderException e) { throw new IllegalArgumentException("Invalid data URL: \"" + dataUrl + "\".", e); } } return new ByteArrayInputStream(bytes); }
@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))); }
@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))); }
@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 testSha384() throws IOException { // Examples from FIPS 180-2 assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + "8086072ba1e7cc2358baeca134c825a7", DigestUtils.sha384Hex("abc")); assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + "8086072ba1e7cc2358baeca134c825a7", DigestUtils.sha384Hex(getBytesUtf8("abc"))); assertEquals("09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" + "fcc7c71a557e2db966c3e9fa91746039", DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")); assertEquals(DigestUtils.sha384Hex(testData), DigestUtils.sha384Hex(new ByteArrayInputStream(testData))); }
/** * Tests RFC 4648 section 10 test vectors. * <ul> * <li>BASE64("") = ""</li> * <li>BASE64("f") = "Zg=="</li> * <li>BASE64("fo") = "Zm8="</li> * <li>BASE64("foo") = "Zm9v"</li> * <li>BASE64("foob") = "Zm9vYg=="</li> * <li>BASE64("fooba") = "Zm9vYmE="</li> * <li>BASE64("foobar") = "Zm9vYmFy"</li> * </ul> * * @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/ * html/rfc4648</a> */ @Test public void testRfc4648Section10DecodeWithCrLf() { final String CRLF = StringUtils.newStringUsAscii(Base64.CHUNK_SEPARATOR); assertEquals("", StringUtils.newStringUsAscii(Base64.decodeBase64("" + CRLF))); assertEquals("f", StringUtils.newStringUsAscii(Base64.decodeBase64("Zg==" + CRLF))); assertEquals("fo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm8=" + CRLF))); assertEquals("foo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9v" + CRLF))); assertEquals("foob", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYg==" + CRLF))); assertEquals("fooba", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmE=" + CRLF))); assertEquals("foobar", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmFy" + CRLF))); }
/** * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The * url-safe variation emits - and _ instead of + and / characters. * <b>Note: no padding is added.</b> * @param binaryData * binary data to encode * @return String containing Base64 characters * @since 1.4 */ public static String encodeBase64URLSafeString(final byte[] binaryData) { return StringUtils.newStringUsAscii(encodeBase64(binaryData, false, true)); }
/** * Test our decode with pad character in the middle. (Our current * implementation: halt decode and return what we've got so far). * * The point of this test is not to say * "this is the correct way to decode base64." The point is simply to keep * us aware of the current logic since 1.4 so we don't accidentally break it * without realizing. * * Note for historians. The 1.3 logic would decode to: * "Hello World\u0000Hello World" -- null in the middle --- and 1.4 * unwittingly changed it to current logic. */ @Test public void testDecodeWithInnerPad() { final String content = "SGVsbG8gV29ybGQ=SGVsbG8gV29ybGQ="; final byte[] result = Base64.decodeBase64(content); final byte[] shouldBe = StringUtils.getBytesUtf8("Hello World"); assertTrue("decode should halt at pad (=)", Arrays.equals(result, shouldBe)); }
@Test public void testByteToStringVariations() throws DecoderException { final Base64 base64 = new Base64(0); final byte[] b1 = StringUtils.getBytesUtf8("Hello World"); final byte[] b2 = new byte[0]; final byte[] b3 = null; final byte[] b4 = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090"); // for // url-safe // tests assertEquals("byteToString Hello World", "SGVsbG8gV29ybGQ=", base64.encodeToString(b1)); assertEquals("byteToString static Hello World", "SGVsbG8gV29ybGQ=", Base64.encodeBase64String(b1)); assertEquals("byteToString \"\"", "", base64.encodeToString(b2)); assertEquals("byteToString static \"\"", "", Base64.encodeBase64String(b2)); assertEquals("byteToString null", null, base64.encodeToString(b3)); assertEquals("byteToString static null", null, Base64.encodeBase64String(b3)); assertEquals("byteToString UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", base64.encodeToString(b4)); assertEquals("byteToString static UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", Base64.encodeBase64String(b4)); assertEquals("byteToString static-url-safe UUID", "K_fMJwH-Q5e0nr7tWsxwkA", Base64.encodeBase64URLSafeString(b4)); }
private String Bytes2Base64(byte[] bytes, String format) { StringBuilder sb = new StringBuilder(); if (format != null) { sb.append("data:image/").append(format).append(";base64,"); } else { sb.append("data:image/png;base64,"); } sb.append(StringUtils.newStringUtf8(Base64.encodeBase64(bytes, false))); return sb.toString(); } }
/** * 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)); }
/** * Tests a lineSeparator much bigger than DEFAULT_BUFFER_SIZE. * * @see "<a href='http://mail-archives.apache.org/mod_mbox/commons-dev/201202.mbox/%3C4F3C85D7.5060706@snafu.de%3E'>dev@commons.apache.org</a>" */ @Test @Ignore public void testHugeLineSeparator() { final int BaseNCodec_DEFAULT_BUFFER_SIZE = 8192; final int Base64_BYTES_PER_ENCODED_BLOCK = 4; final byte[] baLineSeparator = new byte[BaseNCodec_DEFAULT_BUFFER_SIZE * 4 - 3]; final Base64 b64 = new Base64(Base64_BYTES_PER_ENCODED_BLOCK, baLineSeparator); final String strOriginal = "Hello World"; final String strDecoded = new String(b64.decode(b64.encode(StringUtils.getBytesUtf8(strOriginal)))); assertEquals("testDEFAULT_BUFFER_SIZE", strOriginal, strDecoded); }
@Test public void testConstructor_Int_ByteArray_Boolean() { final Base64 base64 = new Base64(65, new byte[] { '\t' }, false); final byte[] encoded = base64.encode(Base64TestData.DECODED); String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE; expectedResult = expectedResult.replace('\n', '\t'); final String result = StringUtils.newStringUtf8(encoded); assertEquals("new Base64(65, \\t, false)", expectedResult, result); }
/** * 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()); }