public static byte[] base64Decode(String base64) { return getMimeDecoder().decode(base64.getBytes(US_ASCII)); } }
private static byte[] base64Decode(String base64) { return getMimeDecoder().decode(base64.getBytes(US_ASCII)); } }
private static byte[] loadSharedSecret(File file) { try { return Base64.getMimeDecoder().decode(readAllBytes(file.toPath())); } catch (IOException | IllegalArgumentException e) { throw new RuntimeException("Failed to load shared secret file: " + file, e); } } }
/** * Constructs a Base64JavaUtilCodec. * <p> * The implementation will use a basic encoder and a MIME decoder by default. * </p> * * @since 1.4.11 */ public Base64JavaUtilCodec() { this(Base64.getEncoder(), Base64.getMimeDecoder()); }
private static Optional<Consumer<JwtBuilder>> setupJwtSigner(File file, String password) { if (file == null) { return Optional.empty(); } try { PrivateKey key = PemReader.loadPrivateKey(file, Optional.ofNullable(password)); if (!(key instanceof RSAPrivateKey)) { throw new IOException("Only RSA private keys are supported"); } return Optional.of(jwt -> jwt.signWith(SignatureAlgorithm.RS256, key)); } catch (IOException e) { throw new RuntimeException("Failed to load key file: " + file, e); } catch (GeneralSecurityException ignored) { } try { byte[] base64Key = readAllBytes(file.toPath()); byte[] key = Base64.getMimeDecoder().decode(base64Key); return Optional.of(jwt -> jwt.signWith(SignatureAlgorithm.HS256, key)); } catch (IOException | IllegalArgumentException e) { throw new RuntimeException("Failed to load key file: " + file, e); } } }
private static byte[] readPrivateKeyBytes(InputStream in) { String content; try { content = readContent(in); } catch (IOException e) { throw new PkiException("Failed to read key input stream", e); } finally { safeClose(in); } Matcher m = KEY_PATTERN.matcher(content); if (!m.find()) { throw new PkiException("Could not find a PKCS#8 private key in input stream"); } byte[] base64 = m.group(1).getBytes(StandardCharsets.US_ASCII); return Base64.getMimeDecoder().decode(base64); }
private static byte[] readPublicKeyBytes(InputStream in) { String content; try { content = readContent(in); } catch (IOException e) { throw new PkiException("Failed to read key input stream", e); } finally { safeClose(in); } Matcher m = PUBLIC_KEY_PATTERN.matcher(content); if (!m.find()) { throw new PkiException("Could not find a X509 public key in input stream"); } byte[] base64 = m.group(1).getBytes(StandardCharsets.US_ASCII); return Base64.getMimeDecoder().decode(base64); }
@SuppressWarnings("deprecation") private static TypeSerializer<?> javaDeserialize(String base64) throws IOException { byte[] bytes = Base64.getMimeDecoder().decode(base64); DataInputDeserializer in = new DataInputDeserializer(bytes); return TypeSerializerSerializationUtil.tryReadSerializer(in, Thread.currentThread().getContextClassLoader()); }
/** * Decodes, inflates and deserializes the specified base64-encoded message to an {@link XMLObject}. */ static XMLObject fromDeflatedBase64(String base64Encoded) { requireNonNull(base64Encoded, "base64Encoded"); final byte[] base64decoded; try { base64decoded = Base64.getMimeDecoder().decode(base64Encoded); } catch (IllegalArgumentException e) { throw new SamlException("failed to decode a deflated base64 string", e); } final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); try (InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(bytesOut, new Inflater(true))) { inflaterOutputStream.write(base64decoded); } catch (IOException e) { throw new SamlException("failed to inflate a SAML message", e); } return SamlMessageUtil.deserialize(bytesOut.toByteArray()); }
private static LoadedKey loadKeyFile(File file) { if (!file.canRead()) { throw new SignatureException("Unknown signing key ID"); } // try to load the key as a PEM encoded public key try { return new LoadedKey(PemReader.loadPublicKey(file)); } catch (Exception ignored) { } // try to load the key as a base64 encoded HMAC key try { String base64Key = asCharSource(file, US_ASCII).read(); byte[] rawKey = getMimeDecoder().decode(base64Key.getBytes(US_ASCII)); return new LoadedKey(rawKey); } catch (IOException ignored) { } throw new SignatureException("Unknown signing key id"); }
byte[] der = Base64.getMimeDecoder().decode(base64); try { certs.add((X509Certificate) cf.generateCertificate(new ByteArrayInputStream(der)));
@BeforeClass public void setup() throws Exception { Logging.initialize(); URL resource = getClass().getClassLoader().getResource("33.privateKey"); assertNotNull(resource, "key directory not found"); File keyDir = new File(resource.getFile()).getAbsoluteFile().getParentFile(); defaultKey = getMimeDecoder().decode(asCharSource(new File(keyDir, "default-key.key"), US_ASCII).read().getBytes(US_ASCII)); hmac222 = getMimeDecoder().decode(asCharSource(new File(keyDir, "222.key"), US_ASCII).read().getBytes(US_ASCII)); privateKey33 = PemReader.loadPrivateKey(new File(keyDir, "33.privateKey"), Optional.empty()); server = new TestingPrestoServer( true, ImmutableMap.<String, String>builder() .put("http-server.authentication.type", "JWT") .put("http.authentication.jwt.key-file", new File(keyDir, "${KID}.key").toString()) .put("http-server.https.enabled", "true") .put("http-server.https.keystore.path", getResource("localhost.keystore").getPath()) .put("http-server.https.keystore.key", "changeit") .build(), null, null, new SqlParserOptions(), ImmutableList.of()); server.installPlugin(new TpchPlugin()); server.createCatalog(TEST_CATALOG, "tpch"); waitForNodeRefresh(server); }
final byte[] decodedSignature = Base64.getMimeDecoder().decode(signature); if (!XMLSigningUtil.verifyWithURI(validationCredential, sigAlg, decodedSignature, input)) { throw new SamlException("failed to validate a signature");
/** * Converts an {@link AggregatedHttpMessage} which is received from the remote entity to * a {@link SAMLObject}. */ static <T extends SAMLObject> MessageContext<T> toSamlObject(AggregatedHttpMessage msg, String name) { final SamlParameters parameters = new SamlParameters(msg); final byte[] decoded; try { decoded = Base64.getMimeDecoder().decode(parameters.getFirstValue(name)); } catch (IllegalArgumentException e) { throw new SamlException("failed to decode a base64 string of the parameter: " + name, e); } @SuppressWarnings("unchecked") final T message = (T) deserialize(decoded); final MessageContext<T> messageContext = new MessageContext<>(); messageContext.setMessage(message); final String relayState = parameters.getFirstValueOrNull(RELAY_STATE); if (relayState != null) { final SAMLBindingContext context = messageContext.getSubcontext(SAMLBindingContext.class, true); assert context != null; context.setRelayState(relayState); } return messageContext; }
@SneakyThrows private byte[] decodePem(byte[] bytes, String... headers) { String pem = new String(bytes, StandardCharsets.UTF_8); for (String header : headers) { pem = pem.replace(header, ""); } return Base64.getMimeDecoder().decode(pem); }
/** * Get the clipboard text. * * @return Either the text, which is stored in the clipboard or an empty string if the clipboard is empty */ default String getClipboardText() { byte[] base64decodedBytes = Base64 .getMimeDecoder() .decode(getClipboard(ClipboardContentType.PLAINTEXT)); return new String(base64decodedBytes, StandardCharsets.UTF_8); } }
/** * Get an image from the clipboard. * * @return the actual image instance. * @throws IOException If the returned image cannot be decoded or if the clipboard is empty. */ default BufferedImage getClipboardImage() throws IOException { final byte[] base64decodedBytes = Base64 .getMimeDecoder() .decode(getClipboard(ClipboardContentType.IMAGE)); return ImageIO.read(new ByteArrayInputStream(base64decodedBytes)); }
/** * Get an URL from the clipboard. * * @return the actual URL instance. * @throws MalformedURLException if the URL in the clipboard is not valid or if the clipboard is empty. */ default URL getClipboardUrl() throws MalformedURLException { final byte[] base64decodedBytes = Base64 .getMimeDecoder() .decode(getClipboard(ClipboardContentType.URL)); return new URL(new String(base64decodedBytes, StandardCharsets.UTF_8)); } }
@Override public ServletInputStream getInputStream() throws IOException { if (request.getBody() == null) { return new AwsServletInputStream(new NullInputStream(0, false, false)); } byte[] bodyBytes = null; if (request.isBase64Encoded()) { bodyBytes = Base64.getMimeDecoder().decode(request.getBody()); } else { String encoding = getCharacterEncoding(); if (encoding == null) { encoding = StandardCharsets.ISO_8859_1.name(); } try { bodyBytes = request.getBody().getBytes(encoding); } catch (Exception e) { log.error("Could not read request with character encoding: " + SecurityUtils.crlf(encoding), e); bodyBytes = request.getBody().getBytes(StandardCharsets.ISO_8859_1.name()); } } ByteArrayInputStream requestBodyStream = new ByteArrayInputStream(bodyBytes); return new AwsServletInputStream(requestBodyStream); }
/** * Constructs a Base64JavaUtilCodec. * <p> * The implementation will use a basic encoder and a MIME decoder by default. * </p> * * @since 1.4.11 */ public Base64JavaUtilCodec() { this(Base64.getEncoder(), Base64.getMimeDecoder()); }