Example usage for java.util Base64 getEncoder

List of usage examples for java.util Base64 getEncoder

Introduction

In this page you can find the example usage for java.util Base64 getEncoder.

Prototype

public static Encoder getEncoder() 

Source Link

Document

Returns a Encoder that encodes using the Basic type base64 encoding scheme.

Usage

From source file:it.infn.mw.iam.util.ssh.RSAPublicKeyUtils.java

private static String buildSHA256Fingerprint(String key) throws InvalidSshKeyException {

    String fingerprint = null;//from  w  ww .j  a  v  a2 s  . c  o m

    try {

        byte[] decodedKey = Base64.getDecoder().decode(key);
        byte[] digest = MessageDigest.getInstance(MessageDigestAlgorithms.SHA_256).digest(decodedKey);
        fingerprint = Base64.getEncoder().encodeToString(digest);

    } catch (Exception e) {

        throw new InvalidSshKeyException("Error during fingerprint generation: RSA key is not base64 encoded",
                e);
    }

    return fingerprint;
}

From source file:org.neo4j.ogm.typeconversion.ByteArrayWrapperBase64Converter.java

@Override
public String toGraphProperty(Byte[] value) {
    if (value == null)
        return null;
    return Base64.getEncoder().encodeToString(ArrayUtils.toPrimitive(value));
}

From source file:cloudlens.parser.FileReader.java

public static InputStream fetchFile(String urlString) {
    try {/*from   www  .  ja v a  2s . co m*/
        InputStream inputStream;
        URL url;
        if (urlString.startsWith("local:")) {
            final String path = urlString.replaceFirst("local:", "");
            inputStream = Files.newInputStream(Paths.get(path));
        } else if (urlString.startsWith("file:")) {
            url = new URL(urlString);
            inputStream = Files.newInputStream(Paths.get(url.toURI()));
        } else if (urlString.startsWith("http:") || urlString.startsWith("https:")) {
            url = new URL(urlString);
            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            final Matcher matcher = Pattern.compile("//([^@]+)@").matcher(urlString);
            if (matcher.find()) {
                final String encoding = Base64.getEncoder().encodeToString(matcher.group(1).getBytes());
                conn.setRequestProperty("Authorization", "Basic " + encoding);
            }
            conn.setRequestMethod("GET");
            inputStream = conn.getInputStream();
        } else {
            throw new CLException("supported protocols are: http, https, file, and local.");
        }
        return inputStream;
    } catch (IOException | URISyntaxException e) {
        throw new CLException(e.getMessage());
    }
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static String encryptUsingRSA(String plainText, String publicKeyContent)
        throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, IOException,
        BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
    Cipher encryptCipher = Cipher.getInstance("RSA");
    encryptCipher.init(Cipher.ENCRYPT_MODE, getRSAPublicKeyFrom(publicKeyContent));
    return Base64.getEncoder().encodeToString(encryptCipher.doFinal(plainText.getBytes(UTF_8)));
}

From source file:no.digipost.api.useragreements.client.filters.request.RequestContentHashFilter.java

public RequestContentHashFilter(final Supplier<? extends ExtendedDigest> digestSupplier, final String header) {
    this.digestSupplier = digestSupplier;
    this.header = header;
    this.base64Encoder = Base64.getEncoder();
}

From source file:com.floreantpos.license.FiveStarPOSLicenseGenerator.java

private static String sign(byte[] message, PrivateKey privateKey)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {

    Signature dsa = Signature.getInstance("SHA/DSA");
    dsa.initSign(privateKey);/*from  w w w  .java  2s.  co m*/
    dsa.update(message);

    byte[] signature = dsa.sign();
    return Base64.getEncoder().encodeToString(signature);
}

From source file:org.mycontroller.restclient.core.RestHeader.java

public void addAuthorization(String username, String password) {
    if (username != null) {
        map.put("Authorization",
                "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes()));
    }/*from  w w w  .  j  a v a  2 s .co m*/
}

From source file:com.toptal.conf.SecurityUtils.java

/**
 * Encodes given string into base64.//w w  w .  ja v a2 s  .co  m
 * @param str String.
 * @return Encoded string.
 */
public static String encode(final String str) {
    String result = null;
    if (str != null) {
        result = Base64.getEncoder().encodeToString(str.getBytes(StandardCharsets.UTF_8));
    }
    return result;
}

From source file:org.onehippo.forge.content.pojo.model.BinaryValueTest.java

@Test
public void testBinaryValues() throws Exception {
    BinaryValue bv = BinaryValue.fromDataURI(RED_DOT_IMG_DATA_URI);
    assertEquals("image/png", bv.getMediaType());
    assertNull(bv.getCharset());/*from  w w w .ja va 2s .c o  m*/
    assertEquals(RED_DOT_IMG_DATA_IN_BASE64,
            Base64.getEncoder().encodeToString(IOUtils.toByteArray(bv.getStream())));
    assertEquals(RED_DOT_IMG_DATA_URI, bv.toUriString());
    bv.dispose();
}

From source file:net.dv8tion.jda.utils.AvatarUtil.java

public static Avatar getAvatar(BufferedImage img) {
    try {//from   ww  w .  j  a v  a 2  s  . c om
        //resizing
        img = resize(img);
        //writing + converting to jpg if necessary
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ImageIO.write(img, "jpg", bout);
        bout.close();

        return new Avatar("data:image/jpeg;base64,"
                + StringUtils.newStringUtf8(Base64.getEncoder().encode(bout.toByteArray())));
    } catch (IOException e) {
        JDAImpl.LOG.log(e);
    }
    return null;
}