/** * 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); }
/** * Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature * * @param pArray a byte array containing base64 character data * @return A BigInteger * @since 1.4 */ public static BigInteger decodeInteger(final byte[] pArray) { return new BigInteger(1, decodeBase64(pArray)); }
public static String base64Encode(byte[] data, boolean urlSafe) { return new Base64(0, Base64.CHUNK_SEPARATOR, urlSafe).encodeToString(data); }
/** * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the * method treats whitespace as valid. * * @param arrayOctet byte array to test * @return {@code true} if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; * {@code false}, otherwise * @since 1.5 */ public static boolean isBase64(final byte[] arrayOctet) { for (int i = 0; i < arrayOctet.length; i++) { if (!isBase64(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) { return false; } } return true; }
/** * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks. * * @param binaryData Array containing binary data to encode. * @param isChunked if {@code true} this encoder will chunk the base64 output into 76 character blocks * @param urlSafe if {@code true} this encoder will emit - and _ instead of the usual + and / characters. * <b>Note: no padding is added when encoding using the URL-safe alphabet.</b> * @param maxResultSize The maximum result size to accept. * @return Base64-encoded data. * @throws IllegalArgumentException Thrown when the input array needs an output array bigger than maxResultSize * @since 1.4 */ public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize) { if (binaryData == null || binaryData.length == 0) { return binaryData; } // Create this so can use the super-class method // Also ensures that the same roundings are performed by the ctor and the code final Base64 b64 = isChunked ? new Base64(urlSafe) : new Base64(0, CHUNK_SEPARATOR, urlSafe); final long len = b64.getEncodedLength(binaryData); if (len > maxResultSize) { throw new IllegalArgumentException("Input array too big, the output array would be bigger (" + len + ") than the specified maximum size of " + maxResultSize); } return b64.encode(binaryData); }
/** * Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature * * @param bigInt a BigInteger * @return A byte array containing base64 character data * @throws NullPointerException if null is passed in * @since 1.4 */ public static byte[] encodeInteger(final BigInteger bigInt) { if (bigInt == null) { throw new NullPointerException("encodeInteger called with null parameter"); } return encodeBase64(toIntegerBytes(bigInt), false); }
public static String fingerprint(byte[] data) { return new Base64(-1, new byte[0], true).encodeAsString(DigestUtils.md5(data)).toUpperCase(); }
/** * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the * method treats whitespace as valid. * * @param arrayOctet byte array to test * @return {@code true} if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; * {@code false}, otherwise * @deprecated 1.5 Use {@link #isBase64(byte[])}, will be removed in 2.0. */ @Deprecated public static boolean isArrayByteBase64(final byte[] arrayOctet) { return isBase64(arrayOctet); }
final byte[] buffer = ensureBufferSize(decodeSize, context); final byte b = in[inPos++]; if (b == PAD) { final byte[] buffer = ensureBufferSize(decodeSize, context);
if (containsAlphabetOrPad(lineSeparator)) { final String sep = StringUtils.utf8(lineSeparator); throw new IllegalArgumentException("lineSeparator must not contain base64 characters: [" + sep + "]");
public static String base64Encode(byte[] data) { return new Base64(0, Base64.CHUNK_SEPARATOR).encodeToString(data); }
/** * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks. * * @param binaryData Array containing binary data to encode. * @param isChunked if {@code true} this encoder will chunk the base64 output into 76 character blocks * @param urlSafe if {@code true} this encoder will emit - and _ instead of the usual + and / characters. * <b>Note: no padding is added when encoding using the URL-safe alphabet.</b> * @param maxResultSize The maximum result size to accept. * @return Base64-encoded data. * @throws IllegalArgumentException Thrown when the input array needs an output array bigger than maxResultSize * @since 1.4 */ public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize) { if (binaryData == null || binaryData.length == 0) { return binaryData; } // Create this so can use the super-class method // Also ensures that the same roundings are performed by the ctor and the code final Base64 b64 = isChunked ? new Base64(urlSafe) : new Base64(0, CHUNK_SEPARATOR, urlSafe); final long len = b64.getEncodedLength(binaryData); if (len > maxResultSize) { throw new IllegalArgumentException("Input array too big, the output array would be bigger (" + len + ") than the specified maximum size of " + maxResultSize); } return b64.encode(binaryData); }
/** * Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature * * @param bigInt a BigInteger * @return A byte array containing base64 character data * @throws NullPointerException if null is passed in * @since 1.4 */ public static byte[] encodeInteger(final BigInteger bigInt) { if (bigInt == null) { throw new NullPointerException("encodeInteger called with null parameter"); } return encodeBase64(toIntegerBytes(bigInt), false); }
/** * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the * method treats whitespace as valid. * * @param arrayOctet byte array to test * @return {@code true} if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; * {@code false}, otherwise * @since 1.5 */ public static boolean isBase64(final byte[] arrayOctet) { for (int i = 0; i < arrayOctet.length; i++) { if (!isBase64(arrayOctet[i]) && !isWhiteSpace(arrayOctet[i])) { return false; } } return true; }
/** * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the * method treats whitespace as valid. * * @param arrayOctet byte array to test * @return {@code true} if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; * {@code false}, otherwise * @deprecated 1.5 Use {@link #isBase64(byte[])}, will be removed in 2.0. */ @Deprecated public static boolean isArrayByteBase64(final byte[] arrayOctet) { return isBase64(arrayOctet); }
final byte[] buffer = ensureBufferSize(decodeSize, context); final byte b = in[inPos++]; if (b == PAD) { final byte[] buffer = ensureBufferSize(decodeSize, context);
if (containsAlphabetOrPad(lineSeparator)) { final String sep = StringUtils.utf8(lineSeparator); throw new IllegalArgumentException("lineSeparator must not contain base64 characters: [" + sep + "]");
/** * 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); }