/** * 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); }
/** * 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)); }
/** * 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)); }
@Override protected byte[] doDecoding(final byte[] bytes) { if (bytes == null) { return null; } return Base64.decodeBase64(bytes); }
/** * 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); }
/** * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. * The returned array will be double the length of the passed array, as it takes two characters to represent any * given byte. * * @param data * a byte[] to convert to Hex characters * @return A char[] containing lower-case hexadecimal characters */ public static char[] encodeHex(final byte[] data) { return encodeHex(data, true); }
/** * Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the * method treats whitespace as valid. * * @param base64 * String to test * @return <code>true</code> if all characters in the String are valid characters in the Base64 alphabet or if * the String is empty; <code>false</code>, otherwise * @since 1.5 */ public static boolean isBase64(final String base64) { return isBase64(StringUtils.getBytesUtf8(base64)); }
/** * Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet. * Uses UTF8 encoding. * * @param pArray * a byte array containing binary data * @return A String containing only Base-N character data */ public String encodeToString(final byte[] pArray) { return StringUtils.newStringUtf8(encode(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 String algorithm, final String key) { this(algorithm, StringUtils.getBytesUtf8(key)); }
/** * Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the * original provided OutputStream. * * @param out * OutputStream to wrap. * @param doEncode * true if we should encode all data written to us, false if we should decode. */ public Base32OutputStream(final OutputStream out, final boolean doEncode) { super(out, new Base32(false), doEncode); }
/** * Decodes a byte array where each byte represents an ASCII '0' or '1'. * * @param ascii * each byte represents an ASCII '0' or '1' * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument * @see org.apache.commons.codec.Decoder#decode(Object) */ @Override public byte[] decode(final byte[] ascii) { return fromAscii(ascii); }
/** * Flushes this output stream and forces any buffered output bytes to be written out to the stream. * * @throws IOException * if an I/O error occurs. */ @Override public void flush() throws IOException { flush(true); }
private void testBase32EmptyInputStream(final int chuckSize) throws Exception { final byte[] emptyEncoded = new byte[0]; final byte[] emptyDecoded = new byte[0]; testByteByByte(emptyEncoded, emptyDecoded, chuckSize, CRLF); testByChunk(emptyEncoded, emptyDecoded, chuckSize, CRLF); }
private void testBase64EmptyOutputStream(final int chunkSize) throws Exception { final byte[] emptyEncoded = new byte[0]; final byte[] emptyDecoded = new byte[0]; testByteByByte(emptyEncoded, emptyDecoded, chunkSize, CRLF); testByChunk(emptyEncoded, emptyDecoded, chunkSize, CRLF); }
private void testBase32EmptyOutputStream(final int chunkSize) throws Exception { final byte[] emptyEncoded = new byte[0]; final byte[] emptyDecoded = new byte[0]; testByteByByte(emptyEncoded, emptyDecoded, chunkSize, CRLF); testByChunk(emptyEncoded, emptyDecoded, chunkSize, CRLF); }
@Override protected byte[] doEncoding(final byte[] bytes) { if (bytes == null) { return null; } return Base64.encodeBase64(bytes); }
/** * Encodes binary data using the base64 algorithm but does not chunk the output. * * NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to * single-line non-chunking (commons-codec-1.5). * * @param binaryData * binary data to encode * @return String containing Base64 characters. * @since 1.4 (NOTE: 1.4 chunked the output, whereas 1.5 does not). */ public static String encodeBase64String(final byte[] binaryData) { return StringUtils.newStringUsAscii(encodeBase64(binaryData, false)); }
/** * Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet. * Uses UTF8 encoding. * * @param pArray a byte array containing binary data * @return String containing only character data in the appropriate alphabet. * @since 1.5 * This is a duplicate of {@link #encodeToString(byte[])}; it was merged during refactoring. */ public String encodeAsString(final byte[] pArray){ return StringUtils.newStringUtf8(encode(pArray)); }
/** * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original * provided InputStream. * * @param in * InputStream to wrap. * @param doEncode * true if we should encode all data read from us, false if we should decode. */ public Base32InputStream(final InputStream in, final boolean doEncode) { super(in, new Base32(false), doEncode); }
/** * 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); }