/** * Encodes a byte array into Base64 notation. * <p> * Example options:<pre> * GZIP: gzip-compresses object before encoding it. * DO_BREAK_LINES: break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * <p> * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or * <p> * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DO_BREAK_LINES )</code> * <p>As of v 2.3, if there is an error with the GZIP stream, * the method will throw an java.io.IOException. <b>This is new to v2.3!</b> * In earlier versions, it just returned a null value, but * in retrospect that's a pretty poor way to handle it.</p> * * @param source The data to convert * @param options Specified options * @return The Base64-encoded data as a String * @throws java.io.IOException if there is an error * @throws NullPointerException if source array is null * @see Base64#GZIP * @see Base64#DO_BREAK_LINES * @since 2.0 */ public static String encodeBytes(byte[] source, int options) throws java.io.IOException { return encodeBytes(source, 0, source.length, options); } // end encodeBytes
public static String encode(byte[] bytes) { String s = Base64.encodeBytes(bytes); s = s.split("=")[0]; // Remove any trailing '='s s = s.replace('+', '-'); // 62nd char of encoding s = s.replace('/', '_'); // 63rd char of encoding return s; }
/** * Encodes a byte array into Base64 notation. * Does not GZip-compress data. * * @param source The data to convert * @return The data in Base64-encoded form * @throws NullPointerException if source array is null * @since 1.4 */ public static String encodeBytes(byte[] source) { // Since we're not going to have the GZIP encoding turned on, // we're not going to have an java.io.IOException thrown, so // we should not force the user to have to catch it. String encoded = null; try { encoded = encodeBytes(source, 0, source.length, NO_OPTIONS); } catch (java.io.IOException ex) { assert false : ex.getMessage(); } // end catch assert encoded != null; return encoded; } // end encodeBytes
public static String createHeader(String username, String password) { StringBuffer buf = new StringBuffer(username); buf.append(':').append(password); return "Basic " + Base64.encodeBytes(buf.toString().getBytes(StandardCharsets.UTF_8)); }
try encoded = encodeBytes(source, off, len, NO_OPTIONS);
private String calculateEncodedHash(byte[] body, String hashAlgorithm) throws SignatureException { byte[] bodyHash = hash(body, hashAlgorithm); return Base64.encodeBytes(bodyHash); }
@Override public void writeTo(SignedOutput out, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException { try { byte[] encoded = PKCS7SignatureWriter.sign(providers, out); os.write(Base64.encodeBytes(encoded).getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { throw new WriterException(e); } }
String base64Signature = Base64.encodeBytes(signed); dosetaHeader += base64Signature;
encrypted.close(); byte[] bytes = baos.toByteArray(); String str = Base64.encodeBytes(bytes, Base64.DO_BREAK_LINES); os.write(str.getBytes());
public static String encode(String input) throws Exception { String result = Base64.encodeBytes(input.getBytes("utf-8")); return result; }
public static String encode(String input) throws Exception { String result = Base64.encodeBytes(input.getBytes("utf-8")); return result; }
public static String encode(byte[] bytes) { String s = Base64.encodeBytes(bytes); s = s.split("=")[0]; // Remove any trailing '='s s = s.replace('+', '-'); // 62nd char of encoding s = s.replace('/', '_'); // 63rd char of encoding return s; }
public static String encodeId(String id) throws UnsupportedEncodingException { String key64 = Base64.encodeBytes(id.getBytes()); return URLEncoder.encode(key64, "UTF-8"); }
public static String createHeader(String username, String password) { StringBuffer buf = new StringBuffer(username); buf.append(':').append(password); return "Basic " + Base64.encodeBytes(buf.toString().getBytes(StandardCharsets.UTF_8)); }
public static String createHeader(String username, String password) { StringBuffer buf = new StringBuffer(username); buf.append(':').append(password); return "Basic " + Base64.encodeBytes(buf.toString().getBytes(StandardCharsets.UTF_8)); }
private String hash(String value) { MessageDigest digest = null; try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } byte[] bytes = digest.digest(value.getBytes()); return Base64.encodeBytes(bytes); }
private String serializeKey(Key key) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(128); DataOutputStream out = new DataOutputStream(baos); key.write(out); out.close(); String key64 = Base64.encodeBytes(baos.toByteArray()); return URLEncoder.encode(key64, "UTF-8"); }
@Override public void filter(ClientRequestContext requestContext) throws IOException { String pair = username + ":" + password; String authHeader = "Basic " + new String(Base64.encodeBytes(pair.getBytes())); requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, authHeader); }
public String create(StoredUser user) throws Exception { String password = user.getCredentials().remove("password"); MessageDigest digest = MessageDigest.getInstance("MD5"); String hashPassword = Base64.encodeBytes(digest.digest(password.getBytes("UTF-8"))); user.getCredentials().clear(); user.getCredentials().put("password-hash", hashPassword); if (user.getId() == null) { String id = UUID.randomUUID().toString(); user.setId(id); } cache.put("/users/" + user.getId(), user, -1L, TimeUnit.MILLISECONDS); return user.getId(); }
@PUT @Consumes("application/json") @Produces("application/json") @Path("{id}") public void update(@PathParam("id") String id, StoredUser user) throws Exception { StoredUser stored = getStoredUser(id); if (stored == null) throw new NotFoundException(); if (user.getName() != null) stored.setName(user.getName()); if (user.getEnabled() != null) stored.setEnabled(user.getEnabled()); if (user.getEmail() != null) stored.setEmail((user.getEmail())); if (user.getCredentials() != null && user.getCredentials().containsKey("password")) { String password = user.getCredentials().remove("password"); MessageDigest digest = MessageDigest.getInstance("MD5"); String hashPassword = Base64.encodeBytes(digest.digest(password.getBytes("UTF-8"))); stored.getCredentials().put("password", hashPassword); } cache.put("/users/" + id, stored, -1, TimeUnit.MILLISECONDS); }