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:org.mascherl.example.service.LoginService.java

public static String sha256(String value) {
    MessageDigest messageDigest = createMessageDigest();
    messageDigest.update(value.getBytes(StandardCharsets.UTF_8));
    byte[] digest = messageDigest.digest();
    byte[] base64Digest = Base64.getEncoder().encode(digest);
    return new String(base64Digest, StandardCharsets.UTF_8);
}

From source file:de.mas.telegramircbot.utils.images.ImgurUploader.java

public static String uploadImageAndGetLink(String clientID, byte[] image) throws IOException {
    URL url;/*from   w  w  w .  j  a v  a  2  s  .  c om*/
    url = new URL(Settings.IMGUR_API_URL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    String dataImage = Base64.getEncoder().encodeToString(image);
    String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(dataImage, "UTF-8");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Client-ID " + clientID);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    conn.connect();
    StringBuilder stb = new StringBuilder();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        stb.append(line).append("\n");
    }
    wr.close();
    rd.close();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ImgurResponse.class, new ImgurResponseDeserializer());
    Gson gson = gsonBuilder.create();

    // The JSON data
    try {
        ImgurResponse response = gson.fromJson(stb.toString(), ImgurResponse.class);
        return response.getLink();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return stb.toString();
}

From source file:io.viewserver.core.Utils.java

public static String serialise(String value) {
    return new String(Base64.getEncoder().encode(getBytes(value.getBytes(Charsets.UTF_8))), Charsets.UTF_8);
}

From source file:org.codice.ddf.security.certificate.generator.PkiTools.java

/**
 * Convert a byte array to a Java String.
 *
 * @param bytes DER encoded bytes//  ww  w  .  j  ava 2  s  . co m
 * @return PEM encoded bytes
 */
public static String derToPem(byte[] bytes) {
    Validate.isTrue(bytes != null, "Argument bytes cannot be null");
    return Base64.getEncoder().encodeToString(bytes);
}

From source file:org.finra.herd.core.HerdStringUtilsTest.java

@Test
public void testDecodeBase64() {
    // Test decode using hard coded values.
    assertEquals("UT_SomeText", HerdStringUtils.decodeBase64("VVRfU29tZVRleHQ="));

    // Test decode using random string and encoder.
    String encodedText = StringUtils.toEncodedString(
            Base64.getEncoder().encode(STRING_VALUE.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    assertEquals(STRING_VALUE, HerdStringUtils.decodeBase64(encodedText));
}

From source file:tk.jomp16.plugin.codecutils.CodecUtils.java

@Command("encode")
public void encode(CommandEvent commandEvent) throws Exception {
    if (commandEvent.getArgs().size() >= 2) {
        switch (commandEvent.getArgs().get(0).toLowerCase()) {
        case "base64":
            commandEvent//from w  w w. j a v  a 2  s .c o m
                    .respond(new String(Base64.getEncoder().encode(commandEvent.getArgs().get(1).getBytes())));
            break;
        case "hex":
            commandEvent.respond(String.valueOf(Hex.encodeHex(commandEvent.getArgs().get(1).getBytes())));
            break;
        case "binary":
            commandEvent.respond(getBinary(commandEvent.getArgs().get(1)));
            break;
        default:
            commandEvent.showUsage(this, "encode");
            break;
        }
    } else {
        commandEvent.showUsage(this, "encode");
    }
}

From source file:ai.susi.tools.JsonSignature.java

public static void addSignature(Map<String, byte[]> obj, PrivateKey key)
        throws InvalidKeyException, SignatureException {

    removeSignature(obj);//ww  w.j a  v a  2  s .  com

    Signature signature;
    try {
        signature = Signature.getInstance("SHA256withRSA");
    } catch (NoSuchAlgorithmException e) {
        return; //does not happen
    }

    signature.initSign(key);
    signature.update(obj.toString().getBytes(StandardCharsets.UTF_8));

    byte[] sigBytes = signature.sign();

    obj.put(signatureString, Base64.getEncoder().encode(sigBytes));
}

From source file:org.camunda.bpm.extension.batch.core.CustomBatchConfigurationJsonConverter.java

@Override
public JSONObject toJsonObject(final CustomBatchConfiguration<T> customBatchConfiguration) {
    final JSONObject json = new JSONObject();

    JsonUtil.addField(json, EXCLUSIVE, customBatchConfiguration.isExclusive());
    JsonUtil.addField(json, DATA_SERIALIZED, Base64.getEncoder()
            .encodeToString(SerializationUtils.serialize((Serializable) customBatchConfiguration.getData())));

    return json;//ww w .  ja  va2 s . c om
}

From source file:br.com.ufjf.labredes.rest.ConectaResource.java

@POST
@Path("/{client_pk}")
//2.Envia PK Criptografada
public String answer(@PathParam("client_pk") String client_pk) {
    Response ans = new Response();

    if (!client_pk.equals("")) {
        byte[] client = Base64.getDecoder().decode(client_pk);
        client_aes = rsa.decrypt(client, sever_privateKey);
        ans.Ok("Conexo aceita");
        //2.1 Responde
        byte[] data = SerializationUtils.serialize(rsa.encrypt(ans, client_aes));
        String aux = Base64.getEncoder().encodeToString(data);
        return aux;
    } else {/*from  ww w.  ja v a 2s.c o m*/
        ans.Error("Erro ao decriptar chave");
        //2.1 Responde
        byte[] data = SerializationUtils.serialize(rsa.encrypt(ans, client_aes));
        String aux = Base64.getEncoder().encodeToString(data);
        return aux;
    }
}

From source file:br.com.elotech.karina.service.impl.GeradorSenhaElotech.java

@SneakyThrows
private String internalGenerate(String concatLicense) {

    System.out.println(concatLicense);

    String keyForSha1 = DigestUtils.md5Hex(KEY_ELOTECH).toUpperCase();
    System.out.println(keyForSha1);

    String keyForIdea = new String(Hex.encode(DigestUtils.getSha1Digest().digest(keyForSha1.getBytes())))
            .toUpperCase();/*from w w w.j ava2  s  .c o m*/

    System.out.println(keyForIdea);

    byte[] input = concatLicense.getBytes();
    byte[] out = new byte[input.length];

    KeyParameter keyParameter = new KeyParameter(keyForIdea.getBytes());

    BufferedBlockCipher cipher = new BufferedBlockCipher(new IDEAEngine());

    cipher.init(true, keyParameter);

    cipher.processBytes(input, 0, input.length, out, 0);

    String ideaSecret = Base64.getEncoder().encodeToString(out);

    System.out.println(ideaSecret);

    return null;

}