@Override public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(Base64Utils.encodeToString(src)); }
@Override public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext cxt) { return Base64Utils.decodeFromString(json.getAsString()); } }
/** * Base64-decode the given byte array from an UTF-8 String. * @param src the encoded UTF-8 String * @return the original byte array */ public static byte[] decodeFromString(String src) { if (src.isEmpty()) { return new byte[0]; } return decode(src.getBytes(DEFAULT_CHARSET)); }
@Test public void encodeDecodeUrlSafe() { byte[] bytes = new byte[] { (byte) 0xfb, (byte) 0xf0 }; assertArrayEquals("-_A=".getBytes(), Base64Utils.encodeUrlSafe(bytes)); assertArrayEquals(bytes, Base64Utils.decodeUrlSafe(Base64Utils.encodeUrlSafe(bytes))); assertEquals("-_A=", Base64Utils.encodeToUrlSafeString(bytes)); assertArrayEquals(bytes, Base64Utils.decodeFromUrlSafeString(Base64Utils.encodeToUrlSafeString(bytes))); }
@Test public void encode() throws UnsupportedEncodingException { byte[] bytes = new byte[] {-0x4f, 0xa, -0x73, -0x4f, 0x64, -0x20, 0x75, 0x41, 0x5, -0x49, -0x57, -0x65, -0x19, 0x2e, 0x3f, -0x1b}; assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); bytes = "Hello World".getBytes("UTF-8"); assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); bytes = "Hello World\r\nSecond Line".getBytes("UTF-8"); assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); bytes = "Hello World\r\nSecond Line\r\n".getBytes("UTF-8"); assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); bytes = new byte[] { (byte) 0xfb, (byte) 0xf0 }; assertArrayEquals("+/A=".getBytes(), Base64Utils.encode(bytes)); assertArrayEquals(bytes, Base64Utils.decode(Base64Utils.encode(bytes))); assertArrayEquals("-_A=".getBytes(), Base64Utils.encodeUrlSafe(bytes)); assertArrayEquals(bytes, Base64Utils.decodeUrlSafe(Base64Utils.encodeUrlSafe(bytes))); }
@Test public void encodeToStringWithJdk8VsJaxb() throws UnsupportedEncodingException { byte[] bytes = new byte[] {-0x4f, 0xa, -0x73, -0x4f, 0x64, -0x20, 0x75, 0x41, 0x5, -0x49, -0x57, -0x65, -0x19, 0x2e, 0x3f, -0x1b}; assertEquals(Base64Utils.encodeToString(bytes), DatatypeConverter.printBase64Binary(bytes)); assertArrayEquals(bytes, Base64Utils.decodeFromString(Base64Utils.encodeToString(bytes))); assertArrayEquals(bytes, DatatypeConverter.parseBase64Binary(DatatypeConverter.printBase64Binary(bytes))); bytes = "Hello World".getBytes("UTF-8"); assertEquals(Base64Utils.encodeToString(bytes), DatatypeConverter.printBase64Binary(bytes)); assertArrayEquals(bytes, Base64Utils.decodeFromString(Base64Utils.encodeToString(bytes))); assertArrayEquals(bytes, DatatypeConverter.parseBase64Binary(DatatypeConverter.printBase64Binary(bytes))); bytes = "Hello World\r\nSecond Line".getBytes("UTF-8"); assertEquals(Base64Utils.encodeToString(bytes), DatatypeConverter.printBase64Binary(bytes)); assertArrayEquals(bytes, Base64Utils.decodeFromString(Base64Utils.encodeToString(bytes))); assertArrayEquals(bytes, DatatypeConverter.parseBase64Binary(DatatypeConverter.printBase64Binary(bytes))); bytes = "Hello World\r\nSecond Line\r\n".getBytes("UTF-8"); assertEquals(Base64Utils.encodeToString(bytes), DatatypeConverter.printBase64Binary(bytes)); assertArrayEquals(bytes, Base64Utils.decodeFromString(Base64Utils.encodeToString(bytes))); assertArrayEquals(bytes, DatatypeConverter.parseBase64Binary(DatatypeConverter.printBase64Binary(bytes))); }
/** * Base64-encode the given byte array to a String. * @param src the original byte array (may be {@code null}) * @return the encoded byte array as a UTF-8 String */ public static String encodeToString(byte[] src) { if (src.length == 0) { return ""; } return new String(encode(src), DEFAULT_CHARSET); }
/** * Generates a random 32 character token to be used in URLs. * * @return a token. */ public static String getRandomUrlToken() { SecureRandom sr = new SecureRandom(); byte[] tokenBytes = new byte[ URL_RANDOM_TOKEN_LENGTH ]; sr.nextBytes( tokenBytes ); return Base64Utils.encodeToUrlSafeString( tokenBytes ); } }
/** * Base64-encode the given byte array to a String. * @param src the original byte array (may be {@code null}) * @return the encoded byte array as a UTF-8 String */ public static String encodeToString(byte[] src) { if (src.length == 0) { return ""; } return new String(encode(src), DEFAULT_CHARSET); }
/** * Generate the connection id for the connection used by the rule's * connection factory. * @return the id. */ public String generateId() { UUID uuid = UUID.randomUUID(); ByteBuffer bb = ByteBuffer.wrap(new byte[SIXTEEN]); bb.putLong(uuid.getMostSignificantBits()) .putLong(uuid.getLeastSignificantBits()); return "SpringBrokerRunning." + Base64Utils.encodeToUrlSafeString(bb.array()).replaceAll("=", ""); }
protected String encode(String username, String password) { String token = Base64Utils.encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8)); return "Basic " + token; } }
@Override public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext cxt) { return Base64Utils.decodeFromString(json.getAsString()); } }
/** * Base64-decode the given byte array from an UTF-8 String. * @param src the encoded UTF-8 String * @return the original byte array */ public static byte[] decodeFromString(String src) { if (src.isEmpty()) { return new byte[0]; } return decode(src.getBytes(DEFAULT_CHARSET)); }
private void addAuthorizationToken(ConfigClientProperties configClientProperties, HttpHeaders httpHeaders, String username, String password) { String authorization = configClientProperties.getHeaders().get(AUTHORIZATION); if (password != null && authorization != null) { throw new IllegalStateException( "You must set either 'password' or 'authorization'"); } if (password != null) { byte[] token = Base64Utils.encode((username + ":" + password).getBytes()); httpHeaders.add("Authorization", "Basic " + new String(token)); } else if (authorization != null) { httpHeaders.add("Authorization", authorization); } }
@Override public String generateName() { UUID uuid = UUID.randomUUID(); ByteBuffer bb = ByteBuffer.wrap(new byte[16]); // NOSONAR - Magic # deprecated anyway bb.putLong(uuid.getMostSignificantBits()) .putLong(uuid.getLeastSignificantBits()); // Convert to base64 and remove trailing = return this.prefix + Base64Utils.encodeToUrlSafeString(bb.array()) .replaceAll("=", ""); }
@Override public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams) { if (username == null && password == null) { return; } String str = (username == null ? "" : username) + ":" + (password == null ? "" : password); headerParams.add(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString(str.getBytes(StandardCharsets.UTF_8))); } }
private Criteria stringBitmask(String operator, String bitmask) { Assert.hasText(bitmask, "Bitmask must not be null!"); target.criteria.put(operator, new Binary(Base64Utils.decodeFromString(bitmask))); return target; }
Base64Utils.decode(candidate.getBytes()); return candidate;
@Override public ClientMetadata mapRow(ResultSet rs, int rowNum) throws SQLException { ClientMetadata clientMetadata = new ClientMetadata(); int pos = 1; clientMetadata.setClientId(rs.getString("client_id")); clientMetadata.setIdentityZoneId(rs.getString("identity_zone_id")); clientMetadata.setShowOnHomePage(rs.getBoolean("show_on_home_page")); try { clientMetadata.setAppLaunchUrl(new URL(rs.getString("app_launch_url"))); } catch (MalformedURLException mue) { // it is safe to ignore this as client_metadata rows are always created from a ClientMetadata instance whose launch url property is strongly typed to URL } byte[] iconBytes = rs.getBytes("app_icon"); if(iconBytes != null) { clientMetadata.setAppIcon(new String(Base64Utils.encode(iconBytes))); } clientMetadata.setCreatedBy(rs.getString("created_by")); String json = rs.getString("additional_information"); if (hasText(json)) { Map<String,Object> additionalInformation = JsonUtils.readValue(json, new TypeReference<Map<String,Object>>() {}); clientMetadata.setClientName((String)additionalInformation.get(CLIENT_NAME)); } return clientMetadata; } }
@Override public String generateName() { UUID uuid = UUID.randomUUID(); ByteBuffer bb = ByteBuffer.wrap(new byte[SIXTEEN]); bb.putLong(uuid.getMostSignificantBits()) .putLong(uuid.getLeastSignificantBits()); // Convert to base64 and remove trailing = return this.prefix + Base64Utils.encodeToUrlSafeString(bb.array()) .replaceAll("=", ""); }