/** * Decodes a base64 string into bytes. Newline characters found at block * boundaries will be ignored. * * @param encodedString * The string to decode. * @return The decoded byte array. */ public static byte[] decode(String encodedString) { return decode(encodedString.toCharArray()); }
/** * Encodes an entire byte array into a Base64 string, with optional newlines * after every 76 characters. * * @param bytes * The byte array to encode. * @param newlines * Indicates whether or not newlines are desired. * @return The encoded string. */ public static String encode(byte[] bytes, boolean newlines) { return encode(bytes, 0, bytes.length, newlines); }
/** * Encodes an entire chars array into a Base64 string, with optional * newlines after every 76 characters. * * @param chars * The characters array to encode. * @param newlines * Indicates whether or not newlines are desired. * @return The encoded string. */ public static String encode(char[] chars, boolean newlines) { return encode(/*IoUtils.*/toByteArray(chars), newlines); }
/** * Returns the byte value at a given position in a bytes array. * * @param data * The bytes array. * @param block * The block size. * @param off * The offset value. * @return The extracted byte. */ private final static int byteAt(byte[] data, int block, int off) { return unsign(data[(block * 3) + off]); }
/** * Converts a char array into a byte array using the default character set. * * @param chars * The source characters. * @return The result bytes. */ public static byte[] toByteArray(char[] chars) { /* IoUtilsからコピー */ return /*IoUtils.*/toByteArray(chars, java.nio.charset.Charset .defaultCharset().name()); }
/** * Encodes an entire chars array into a Base64 string, with optional * newlines after every 76 characters. * * @param chars * The characters array to encode. * @param charset * The character set to use for the character to byte conversion. * @param newlines * Indicates whether or not newlines are desired. * @return The encoded string. */ public static String encode(char[] chars, String charset, boolean newlines) { return encode(/*IoUtils.*/toByteArray(chars, charset), newlines); }
/** * Returns the byte value at a given position in a bytes array. * * @param data * The bytes array. * @param block * The block size. * @param off * The offset value. * @return The extracted byte. */ private final static int byteAt(byte[] data, int block, int off) { return unsign(data[(block * 3) + off]); }
/** * Encodes an entire byte array into a Base64 string, with optional newlines * after every 76 characters. * * @param bytes * The byte array to encode. * @param newlines * Indicates whether or not newlines are desired. * @return The encoded string. */ public static String encode(byte[] bytes, boolean newlines) { return encode(bytes, 0, bytes.length, newlines); }
/** * Decodes a base64 string into bytes. Newline characters found at block * boundaries will be ignored. * * @param encodedString * The string to decode. * @return The decoded byte array. */ public static byte[] decode(String encodedString) { return decode(encodedString.toCharArray()); }
/** * Encodes an entire chars array into a Base64 string, with optional * newlines after every 76 characters. * * @param chars * The characters array to encode. * @param newlines * Indicates whether or not newlines are desired. * @return The encoded string. */ public static String encode(char[] chars, boolean newlines) { return encode(IoUtils.toByteArray(chars), newlines); }
/** * Encrypts a content string. * * @param algo * The cryptographic algorithm. * @param base64Secret * The cryptographic secret, encoded as a Base64 string. * @param content * The content string to encrypt. * @return The encrypted bytes. * @throws GeneralSecurityException */ public static byte[] encrypt(String algo, String base64Secret, String content) throws GeneralSecurityException { return encrypt(algo, Base64.decode(base64Secret), content); }
/** * Encodes an entire chars array into a Base64 string, with optional * newlines after every 76 characters. * * @param chars * The characters array to encode. * @param charset * The character set to use for the character to byte conversion. * @param newlines * Indicates whether or not newlines are desired. * @return The encoded string. */ public static String encode(char[] chars, String charset, boolean newlines) { return encode(IoUtils.toByteArray(chars, charset), newlines); }
/** * Decrypts a bytes array. * * @param algo * The cryptographic algorithm. * @param base64Secret * The cryptographic secret key, encoded as a Base64 string. * @param encrypted * The encrypted bytes. * @return The decrypted content string. * @throws GeneralSecurityException */ public static String decrypt(String algo, String base64Secret, byte[] encrypted) throws GeneralSecurityException { return decrypt(algo, Base64.decode(base64Secret), encrypted); }
/** * Generates a nonce as recommended in section 3.2.1 of RFC-2617, but * without the ETag field. The format is: <code><pre> * Base64.encodeBytes(currentTimeMS + ":" * + md5String(currentTimeMS + ":" + secretKey)) * </pre></code> * * @param secretKey * a secret value known only to the creator of the nonce. It's * inserted into the nonce, and can be used later to validate the * nonce. */ public static String makeNonce(String secretKey) { final long currentTimeMS = System.currentTimeMillis(); return Base64.encode( (currentTimeMS + ":" + DigestUtils.toMd5(currentTimeMS + ":" + secretKey)).getBytes(), true); }
final byte[] encrypted = Base64.decode(cookieValue);
@Override public void formatResponse(ChallengeWriter cw, ChallengeResponse challenge, Request request, Series<Header> httpHeaders) { try { final CharArrayWriter credentials = new CharArrayWriter(); credentials.write("^@"); credentials.write(challenge.getIdentifier()); credentials.write("^@"); credentials.write(challenge.getSecret()); cw.append(Base64.encode(credentials.toCharArray(), "US-ASCII", false)); } catch (UnsupportedEncodingException e) { throw new RuntimeException( "Unsupported encoding, unable to encode credentials"); } catch (IOException e) { throw new RuntimeException( "Unexpected exception, unable to encode credentials", e); } }