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:com.github.horrorho.inflatabledonkey.KeyBagManager.java

public Optional<KeyBag> keyBag(byte[] uuid) {
    String encoded = Base64.getEncoder().encodeToString(uuid);
    return keyBag(encoded);
}

From source file:org.codice.alliance.distribution.branding.AllianceBrandingPlugin.java

@Override
public String getBase64VendorImage() throws IOException {
    byte[] vendorImageAsBytes = IOUtils
            .toByteArray(AllianceBrandingPlugin.class.getResourceAsStream(getVendorImage()));
    if (vendorImageAsBytes.length > 0) {
        return Base64.getEncoder().encodeToString(vendorImageAsBytes);
    }//from  w  ww .ja va2 s  .c om
    return "";
}

From source file:com.alanjz.spin.project.util.JSONSpinfileParser.java

public String digest64(MessageDigest md, String str) {
    return Base64.getEncoder().encodeToString(md.digest(str.getBytes()));
}

From source file:com.github.ffremont.microservices.springboot.node.services.MsService.java

/**
 * Retourne le flux  lire//from www.  j  a  va  2 s. c o m
 *
 * @param msName
 * @return
 */
public Path getBinary(String msName) {
    MicroServiceRest ms = this.getMs(msName);

    if (ms == null) {
        return null;
    }

    MasterUrlBuilder builder = new MasterUrlBuilder(cluster, node, masterhost, masterPort, masterCR);
    builder.setUri(msName + "/binary");

    Path tempFileBinary = null;
    try {
        tempFileBinary = Files.createTempFile("node", "jarBinary");
        HttpURLConnection managerConnection = (HttpURLConnection) (new URL(builder.build())).openConnection();
        managerConnection.setRequestProperty("Authorization", "Basic "
                .concat(new String(Base64.getEncoder().encode((username + ":" + password).getBytes()))));
        managerConnection.setRequestProperty("Accept", "application/java-archive");
        managerConnection.connect();

        if (managerConnection.getResponseCode() != 200) {
            LOG.warn("Manager : rcupration impossible, statut {}", managerConnection.getResponseCode());
            return tempFileBinary;
        }

        FileOutputStream fos = new FileOutputStream(tempFileBinary.toFile());
        try (InputStream is = managerConnection.getInputStream()) {
            byte[] buffer = new byte[10240]; // 10ko
            int read;
            while (-1 != (read = is.read(buffer))) {
                fos.write(buffer, 0, read);
            }
            fos.flush();
            is.close();
        }
    } catch (IOException ex) {
        LOG.error("Impossible de rcuprer le binaire", ex);
        if (tempFileBinary != null) {
            try {
                Files.delete(tempFileBinary);
            } catch (IOException e) {
            }
        }
    }

    return tempFileBinary;
}

From source file:software.uncharted.image.ImageProcessing.java

public static String toBase64(BufferedImage bi) throws IOException {
    return Base64.getEncoder().encodeToString(bufferedImageToByteArray(bi));
}

From source file:org.codice.alliance.distribution.branding.AllianceBrandingPluginTest.java

@Test
public void testFavIcon() throws IOException {
    allianceBrandingPlugin.init();/*ww w. jav  a2s . c om*/
    assertThat(allianceBrandingPlugin.getBase64FavIcon(), is(equalTo(Base64.getEncoder().encodeToString(
            IOUtils.toByteArray(AllianceBrandingPluginTest.class.getResourceAsStream(favIcon))))));
}

From source file:org.codice.alliance.distribution.branding.TestAllianceBrandingPlugin.java

@Test
public void testFavIcon() throws IOException {
    allianceBrandingPlugin.init();/*from   w w  w. j  a v  a  2s.  co m*/
    assertThat(allianceBrandingPlugin.getBase64FavIcon(), is(equalTo(Base64.getEncoder().encodeToString(
            IOUtils.toByteArray(TestAllianceBrandingPlugin.class.getResourceAsStream(favIcon))))));
}

From source file:org.codice.ddf.branding.impl.DdfBrandingPlugin.java

@Override
public String getBase64VendorImage() throws IOException {
    try (InputStream inputStream = DdfBrandingPlugin.class.getResourceAsStream(getVendorImage())) {
        byte[] vendorImageAsBytes = IOUtils.toByteArray(inputStream);
        if (vendorImageAsBytes.length > 0) {
            return Base64.getEncoder().encodeToString(vendorImageAsBytes);
        }// ww w . j  a  v  a  2  s. com
    }
    return "";
}

From source file:org.apache.accumulo.shell.commands.GetSplitsCommand.java

private static String encode(final boolean encode, final Text text) {
    if (text == null) {
        return null;
    }// w  ww.j av a2s  . co  m
    final int length = text.getLength();
    return encode ? Base64.getEncoder().encodeToString(TextUtil.getBytes(text))
            : DefaultFormatter.appendText(new StringBuilder(), text, length).toString();
}

From source file:com.cws.esolutions.security.utils.PasswordUtils.java

/**
 * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility
 * is required but encryption (obfuscation, technically) is required.
 *
 * @param value - The plain text data to encrypt
 * @param salt - The salt value to utilize for the request
 * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory
 * @param iterations - The number of times to loop through the keyspec
 * @param keyBits - The size of the key, in bits
 * @param algorithm - The algorithm to encrypt the data with
 * @param cipherInstance - The cipher instance to utilize
 * @param encoding - The text encoding/*from   w w  w.j av  a 2s.  c  o m*/
 * @return The encrypted string in a reversible format
 * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing
 */
public static final String encryptText(final String value, final String salt, final String secretInstance,
        final int iterations, final int keyBits, final String algorithm, final String cipherInstance,
        final String encoding) throws SecurityException {
    final String methodName = PasswordUtils.CNAME
            + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", secretInstance);
        DEBUGGER.debug("Value: {}", iterations);
        DEBUGGER.debug("Value: {}", keyBits);
        DEBUGGER.debug("Value: {}", algorithm);
        DEBUGGER.debug("Value: {}", cipherInstance);
        DEBUGGER.debug("Value: {}", encoding);
    }

    String encPass = null;

    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance);
        PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits);
        SecretKey keyTmp = keyFactory.generateSecret(keySpec);
        SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm);

        Cipher pbeCipher = Cipher.getInstance(cipherInstance);
        pbeCipher.init(Cipher.ENCRYPT_MODE, sks);

        AlgorithmParameters parameters = pbeCipher.getParameters();
        IvParameterSpec ivParameterSpec = parameters.getParameterSpec(IvParameterSpec.class);

        byte[] cryptoText = pbeCipher.doFinal(value.getBytes(encoding));
        byte[] iv = ivParameterSpec.getIV();

        String combined = Base64.getEncoder().encodeToString(iv) + ":"
                + Base64.getEncoder().encodeToString(cryptoText);

        encPass = Base64.getEncoder().encodeToString(combined.getBytes());
    } catch (InvalidKeyException ikx) {
        throw new SecurityException(ikx.getMessage(), ikx);
    } catch (NoSuchAlgorithmException nsx) {
        throw new SecurityException(nsx.getMessage(), nsx);
    } catch (NoSuchPaddingException npx) {
        throw new SecurityException(npx.getMessage(), npx);
    } catch (IllegalBlockSizeException ibx) {
        throw new SecurityException(ibx.getMessage(), ibx);
    } catch (BadPaddingException bpx) {
        throw new SecurityException(bpx.getMessage(), bpx);
    } catch (UnsupportedEncodingException uex) {
        throw new SecurityException(uex.getMessage(), uex);
    } catch (InvalidKeySpecException iksx) {
        throw new SecurityException(iksx.getMessage(), iksx);
    } catch (InvalidParameterSpecException ipsx) {
        throw new SecurityException(ipsx.getMessage(), ipsx);
    }

    return encPass;
}