@Override protected byte[] doEncoding(final byte[] bytes) { if (bytes == null) { return null; } return Base64.encodeBase64(bytes); }
/** * 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 byte[] containing Base64 characters in their UTF-8 representation. * @since 1.4 */ public static byte[] encodeBase64URLSafe(final byte[] binaryData) { return encodeBase64(binaryData, false, true); }
/** * Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks * * @param binaryData * binary data to encode * @return Base64 characters chunked in 76 character blocks */ public static byte[] encodeBase64Chunked(final byte[] binaryData) { return encodeBase64(binaryData, true); }
/** * Encodes binary data using the base64 algorithm but does not chunk the output. * * @param binaryData * binary data to encode * @return byte[] containing Base64 characters in their UTF-8 representation. */ public static byte[] encodeBase64(final byte[] binaryData) { return encodeBase64(binaryData, false); }
private static String basicAuth(String userInfo) { if (userInfo != null) return "Basic "+new String(Base64.encodeBase64(userInfo.getBytes())); return null; }
private String base64(byte input[]) throws UnsupportedEncodingException { return new String(Base64.encodeBase64(input), ENCODING); }
/** * Convenience method to call {@link #authorization} with the HTTP basic authentication. * Cf. {@code BasicHeaderApiTokenAuthenticator}. */ public CLIConnectionFactory basicAuth(String userInfo) { return authorization("Basic " + new String(Base64.encodeBase64((userInfo).getBytes()))); }
public void writeKey(Key key) throws IOException { writeUTF(new String(Base64.encodeBase64(key.getEncoded()))); }
/** * 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)); }
/** * Gets the Base64 encoded public key that forms part of this instance's identity keypair. * @return the Base64 encoded public key * @since 2.16 */ @Nullable public String getIdentityPublicKey() { RSAPublicKey key = InstanceIdentityProvider.RSA.getPublicKey(); return key == null ? null : new String(Base64.encodeBase64(key.getEncoded()), Charset.forName("UTF-8")); }
/** * Gets base64-encoded public key. */ public String getEncodedPublicKey() { return new String(Base64.encodeBase64(getPublicKey().getEncoded())); } }
private static String signAndBase64Encode(String data, String key, SigningAlgorithm algorithm, Charset charset) throws AclException { try { byte[] signature = sign(data.getBytes(charset), key.getBytes(charset), algorithm); return new String(Base64.encodeBase64(signature), DEFAULT_CHARSET); } catch (Exception e) { String message = String.format(CAL_SIGNATURE_FAILED_MSG, CAL_SIGNATURE_FAILED, e.getMessage()); log.error(message, e); throw new AclException("CAL_SIGNATURE_FAILED", CAL_SIGNATURE_FAILED, message, e); } }
private static String signAndBase64Encode(byte[] data, String key, SigningAlgorithm algorithm, Charset charset) throws AclException { try { byte[] signature = sign(data, key.getBytes(charset), algorithm); return new String(Base64.encodeBase64(signature), DEFAULT_CHARSET); } catch (Exception e) { String message = String.format(CAL_SIGNATURE_FAILED_MSG, CAL_SIGNATURE_FAILED, e.getMessage()); log.error(message, e); throw new AclException("CAL_SIGNATURE_FAILED", CAL_SIGNATURE_FAILED, message, e); } }
@Test public void testCodec112() { // size calculation assumes always chunked final byte[] in = new byte[] { 0 }; final byte[] out = Base64.encodeBase64(in); Base64.encodeBase64(in, false, false, out.length); }
private void testEncodeOverMaxSize(final int maxSize) throws Exception { try { Base64.encodeBase64(Base64TestData.DECODED, true, false, maxSize); fail("Expected " + IllegalArgumentException.class.getName()); } catch (final IllegalArgumentException e) { // Expected } }
@Test public void testPairs() { assertEquals("AAA=", new String(Base64.encodeBase64(new byte[] { 0, 0 }))); for (int i = -128; i <= 127; i++) { final byte test[] = { (byte) i, (byte) i }; assertTrue(Arrays.equals(test, Base64.decodeBase64(Base64.encodeBase64(test)))); } }
@Test public void testObjectDecodeWithValidParameter() throws Exception { final String original = "Hello World!"; final Object o = Base64.encodeBase64(original.getBytes(CHARSET_UTF8)); final Base64 b64 = new Base64(); final Object oDecoded = b64.decode(o); final byte[] baDecoded = (byte[]) oDecoded; final String dest = new String(baDecoded); assertEquals("dest string does not equal original", original, dest); }
/** * 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)); }
@Test public void testEncodeDecodeRandom() { for (int i = 1; i < 5; i++) { final byte[] data = new byte[this.getRandom().nextInt(10000) + 1]; this.getRandom().nextBytes(data); final byte[] enc = Base64.encodeBase64(data); assertTrue(Base64.isBase64(enc)); final byte[] data2 = Base64.decodeBase64(enc); assertTrue(Arrays.equals(data, data2)); } }
@Test public void testEncodeDecodeSmall() { for (int i = 0; i < 12; i++) { final byte[] data = new byte[i]; this.getRandom().nextBytes(data); final byte[] enc = Base64.encodeBase64(data); assertTrue("\"" + new String(enc) + "\" is Base64 data.", Base64.isBase64(enc)); final byte[] data2 = Base64.decodeBase64(enc); assertTrue(toString(data) + " equals " + toString(data2), Arrays.equals(data, data2)); } }