Base64 { public static String encode(byte[] bytes) { return encodeBytes(bytes); } private  Base64(); static String encode(byte[] bytes); static String encode(String content); static String encode(String content, int options); static String encode(String content, String charset); static String encode(String content, Charset charset); static String encodeObject(java.io.Serializable serializableObject); static String encodeObject(java.io.Serializable serializableObject, int options); static String encodeBytes(byte[] source); static String encodeBytes(byte[] source, int options); static String encodeBytes(byte[] source, int off, int len); static String encodeBytes(byte[] source, int off, int len, int options); static byte[] decode(byte[] source, int off, int len); static byte[] decode(String s); static Object decodeToObject(String encodedObject); static boolean encodeToFile(byte[] dataToEncode, String filename); static boolean decodeToFile(String dataToDecode, String filename); static byte[] decodeFromFile(String filename); static String encodeFromFile(String filename); final static int NO_OPTIONS; final static int ENCODE; final static int DECODE; final static int GZIP; final static int DONT_BREAK_LINES; }
@Test public void testEncode() { long ts = new Date().getTime(); long random = new Random(ts).nextInt(); String clearToken = String.format("%d:%d:%s:%s", ts, random, PASSWORD, USERNAME); byte[] hashedToken; try { hashedToken = MessageDigest.getInstance("MD5").digest(clearToken.getBytes()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Cannot compute token", e); } String formerToken = org.nuxeo.client.util.Base64.encode(hashedToken); String token = ENCODER.encodeToString(hashedToken); assertEquals(token, formerToken); }
@Test public void testEncodeDoesNotBreakLine() { String info = USERNAME + ":" + PASSWORD; String formerToken = "Basic " + org.nuxeo.client.util.Base64.encode(info, org.nuxeo.client.util.Base64.DONT_BREAK_LINES); String token = "Basic " + ENCODER.encodeToString(info.getBytes(UTF_8)); assertEquals(token, formerToken); }