/** * 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 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))); }
/** * 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)); }
Base64Utils.decode(candidate.getBytes()); return candidate;
@Override public ClientMetadata update(ClientMetadata resource, String zoneId) { logger.debug("Updating metadata for client: " + resource.getClientId()); updateClientNameIfNotEmpty(resource, IdentityZoneHolder.get().getId()); int updated = template.update(CLIENT_METADATA_UPDATE, ps -> { int pos = 1; ps.setBoolean(pos++, resource.isShowOnHomePage()); URL appLaunchUrl = resource.getAppLaunchUrl(); ps.setString(pos++, appLaunchUrl == null ? null : appLaunchUrl.toString()); String appIcon = resource.getAppIcon(); if (appIcon != null) { byte[] decodedAppIcon = Base64Utils.decode(appIcon.getBytes()); ps.setBinaryStream(pos++, new ByteArrayInputStream(decodedAppIcon), decodedAppIcon.length); } else { ps.setBinaryStream(pos++, new ByteArrayInputStream(new byte[]{}), 0); } ps.setString(pos++, resource.getClientId()); ps.setString(pos++, zoneId); }); ClientMetadata resultingClientMetadata = retrieve(resource.getClientId(), zoneId); if (updated > 1) { throw new IncorrectResultSizeDataAccessException(1); } return resultingClientMetadata; }
private PublicKey decodePublicKey() throws Exception { InputStream stream = new ClassPathResource("META-INF/keys/sftp_rsa.pub").getInputStream(); byte[] keyBytes = StreamUtils.copyToByteArray(stream); // strip any newline chars while (keyBytes[keyBytes.length - 1] == 0x0a || keyBytes[keyBytes.length - 1] == 0x0d) { keyBytes = Arrays.copyOf(keyBytes, keyBytes.length - 1); } byte[] decodeBuffer = Base64Utils.decode(keyBytes); ByteBuffer bb = ByteBuffer.wrap(decodeBuffer); int len = bb.getInt(); byte[] type = new byte[len]; bb.get(type); if ("ssh-rsa".equals(new String(type))) { BigInteger e = decodeBigInt(bb); BigInteger m = decodeBigInt(bb); RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e); return KeyFactory.getInstance("RSA").generatePublic(spec); } else { throw new IllegalArgumentException("Only supports RSA"); } }
private PublicKey decodePublicKey(String key) throws Exception { InputStream stream = new ClassPathResource(key).getInputStream(); byte[] keyBytes = FileCopyUtils.copyToByteArray(stream); // strip any newline chars while (keyBytes[keyBytes.length - 1] == 0x0a || keyBytes[keyBytes.length - 1] == 0x0d) { keyBytes = Arrays.copyOf(keyBytes, keyBytes.length - 1); } byte[] decodeBuffer = Base64Utils.decode(keyBytes); ByteBuffer bb = ByteBuffer.wrap(decodeBuffer); int len = bb.getInt(); byte[] type = new byte[len]; bb.get(type); if ("ssh-rsa".equals(new String(type))) { BigInteger e = decodeBigInt(bb); BigInteger m = decodeBigInt(bb); RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e); return KeyFactory.getInstance("RSA").generatePublic(spec); } else { throw new IllegalArgumentException("Only supports RSA"); } }
/** * 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 static byte[] base64Decode(String string) { try { ByteBuffer bytes = UTF8.newEncoder().encode(CharBuffer.wrap(string)); byte[] bytesCopy = new byte[bytes.limit()]; System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit()); return Base64Utils.decode(bytesCopy); } catch (CharacterCodingException e) { throw new RuntimeException(e); } }
@Override public String decrypt(String encryptedText) { Assert.state(this.privateKey != null, "Private key must be provided for decryption"); return new String(decrypt(Base64Utils.decode(encryptedText .getBytes(this.defaultCharset))), this.charset); }
@Override public String decrypt(String encryptedText) { Assert.state(this.privateKey != null, "Private key must be provided for decryption"); return new String(decrypt(Base64Utils.decode(encryptedText .getBytes(this.defaultCharset))), this.charset); }
public static Optional<Credentials> readFrom(HttpServletRequest request) { String authenticationHeader = request.getHeader("Authorization"); if (!StringUtils.isEmpty(authenticationHeader)) { String credentials = authenticationHeader.substring(6, authenticationHeader.length()); String[] decodedCredentialParts = new String(Base64Utils.decode(credentials.getBytes())).split(":"); String username = decodedCredentialParts.length >= 1 ? decodedCredentialParts[0] : ""; String password = decodedCredentialParts.length >= 2 ? decodedCredentialParts[1] : ""; return Optional.of(new Credentials(username, password)); } else { return Optional.empty(); } } }
/** * Read username and password from the request's {@code Authorization} header and create a {@code Credentials} * object. Requires authorization header to be base64 encoded. * * @param request incoming http request * @return {@code Optional} with parsed {@code Credentials} if {@code Authorization} header and credentials * are present, {@code Optional.empty} otherwise. */ public static Optional<Credentials> readFrom(HttpServletRequest request) { String authorizationHeader = request.getHeader("Authorization"); if (!StringUtils.isEmpty(authorizationHeader)) { String credentials = authorizationHeader.substring(6, authorizationHeader.length()); String[] decodedCredentialParts = new String(Base64Utils.decode(credentials.getBytes())).split(":", 2); if (!decodedCredentialParts[0].isEmpty() && !decodedCredentialParts[1].isEmpty()) { return Optional.of(new Credentials(decodedCredentialParts[0], decodedCredentialParts[1])); } } return Optional.empty(); } }
encodedProperties = new String(Base64Utils.decode(parts[1].getBytes())); } catch (IllegalArgumentException e) { throw new ServiceBrokerInvalidOriginatingIdentityException("Error decoding JSON properties from "
encodedProperties = new String(Base64Utils.decode(parts[1].getBytes())); } catch (IllegalArgumentException e) { throw new ServiceBrokerInvalidOriginatingIdentityException("Error decoding JSON properties from "