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.requests.HeadersLegacy.java

static String token(String type, String left, String right) {
    return type + " "
            + Base64.getEncoder().encodeToString((left + ":" + right).getBytes(StandardCharsets.UTF_8));
}

From source file:io.kodokojo.bdd.stage.HttpUserSupport.java

public static Request.Builder addBasicAuthentification(String username, String password,
        Request.Builder builder) {
    assert StringUtils.isNotBlank(username) : "username must be defined";
    assert builder != null : "builder must be defined";

    String value = "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
    builder.addHeader("Authorization", value);
    return builder;
}

From source file:twister.rpc.TwisterPeer.java

public TwisterPeer(String serverURL, String serverUser, String serverPass) {
    this.url = serverURL;

    conman.setMaxTotal(MAX_HTTP_CONNECTION);

    this.auth = Base64.getEncoder().encodeToString((serverUser + ':' + serverPass).getBytes());

}

From source file:net.dv8tion.jda.core.entities.Icon.java

/**
 * Creates an {@link Icon Icon} with the specified image data.
 *
 * @param data//from   w w w.  j  a va  2  s .  c om
 *      not-null image data bytes.
 * @return
 *      An Icon instance representing the specified image data
 * @throws IllegalArgumentException
 *      if the provided data is null
 */
public static Icon from(byte[] data) {
    Args.notNull(data, "Provided byte[]");

    String encoding = StringUtils.newStringUtf8(Base64.getEncoder().encode(data));
    return new Icon(encoding);
}

From source file:org.dragonet.proxy.protocol.packet.LoginPacket.java

@Override
public void encode() {
    try {/*from  w  ww  . j  a va 2  s.  c o  m*/
        setChannel(NetworkChannel.CHANNEL_PRIORITY);

        // Basic info
        JSONObject jsonBasic = new JSONObject();
        String b64Signature;
        String b64User;
        {
            JSONObject jsonSignature = new JSONObject();
            jsonSignature.put("alg", "ES384");
            jsonSignature.put("x5u",
                    "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE+SKWsy2I5ecBTPZEfNgwgvuin2/iqqi3haMxXPpknsVKINpp5E7jlygXqBmSJad5VG+1uq75V9irGEtmpUINP5xhYiOrlEma+2aBJIUe17UT/r50yTnDhDrPoOY/eAHL");
            b64Signature = Base64.getEncoder().encodeToString(jsonSignature.toString().getBytes("UTF-8"));
        }
        {
            JSONObject jsonUser = new JSONObject();
            jsonUser.put("exp", System.currentTimeMillis() / 1000L + (60 * 10));
            JSONObject jsonUserInfo = new JSONObject();
            jsonUserInfo.put("displayName", username);
            jsonUserInfo.put("identity", clientUuid.toString());
            jsonUser.put("extraData", jsonUserInfo);
            jsonUser.put("identityPublicKey", ""); // publicKey
            jsonUser.put("nbf", System.currentTimeMillis() / 1000L);
            b64User = Base64.getEncoder().encodeToString(jsonUser.toString().getBytes("UTF-8"));
            System.out.println(jsonUser.toString());
        }
        String b64Basic = b64Signature + "." + b64User;

        // Meta info
        JSONObject jsonMeta = new JSONObject();
        String strMeta;
        {
            jsonMeta.put("ClientRandomId", clientID);
            jsonMeta.put("ServerAddress", serverAddress);
            jsonMeta.put("SkinId", skin.getModel());
            jsonMeta.put("SkinData", Base64.getEncoder().encodeToString(skin.getData()));
            strMeta = Base64.getEncoder().encodeToString(jsonMeta.toString().getBytes("UTF-8"));
        }
        String b64Meta = b64Signature + "." + strMeta;

        byte[] chainData;
        {
            byte[] dataBasic = b64Basic.getBytes("UTF-8");
            byte[] dataMeta = b64Meta.getBytes("UTF-8");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            PEBinaryWriter writer = new PEBinaryWriter(bos);
            writer.switchEndianness();
            writer.writeInt(dataBasic.length);
            writer.switchEndianness();
            writer.write(dataBasic);
            writer.switchEndianness();
            writer.writeInt(dataMeta.length);
            writer.switchEndianness();
            writer.write(dataMeta);

            chainData = bos.toByteArray();
        }

        JSONObject jsonChain = new JSONObject();
        jsonChain.put("chain", chainData);
        String strChain = jsonChain.toString();
        byte[] b64Chain = Base64.getEncoder().encode(strChain.getBytes("UTF-8"));
        Deflater deflater = new Deflater(7);
        deflater.setInput(b64Chain);
        deflater.finish();
        byte[] buff = new byte[40960];
        int deflated = deflater.deflate(buff);
        buff = ArrayUtils.subarray(buff, 0, deflated);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PEBinaryWriter writer = new PEBinaryWriter(bos);
        writer.writeByte((byte) (this.pid() & 0xFF));
        writer.writeInt(protocol);
        writer.writeInt(buff.length);
        writer.write(buff);
        this.setData(bos.toByteArray());
    } catch (IOException | JSONException e) {
    }
}

From source file:org.pentaho.di.kitchen.KitchenCommandExecutorTest.java

@Test
public void testFilesystemBase64Zip() throws Exception {
    String fileName = "hello-world.kjb";
    File zipFile = new File(getClass().getResource("testKjbArchive.zip").toURI());
    String base64Zip = Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(zipFile));
    Job job = mockedKitchenCommandExecutor.loadJobFromFilesystem(null, fileName, base64Zip);
    assertNotNull(job);/*from ww  w.j av a  2 s  .c  om*/
}

From source file:org.apache.syncope.core.workflow.java.AbstractUserWorkflowAdapter.java

protected String encrypt(final String clear) {
    byte[] encrypted = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(clear.getBytes());
    return Base64.getEncoder().encodeToString(encrypted);
}

From source file:com.sixt.service.framework.jetty.RpcReadException.java

public String toJson(HttpServletRequest req) {
    JsonObject obj = new JsonObject();

    Enumeration<String> h = req.getHeaderNames();
    while (h.hasMoreElements()) {
        String hKey = h.nextElement();
        String hValue = req.getHeader(hKey);
        obj.addProperty("request_header_" + hKey, hValue);
    }/*w  w w .  j a v  a 2  s. c  om*/

    obj.addProperty("exception_message", this.getMessage());
    obj.addProperty("request_query_string", req.getQueryString());
    obj.addProperty("request_url", req.getRequestURL().toString());
    obj.addProperty("request_remote_addr", req.getRemoteAddr());
    obj.addProperty("request_remote_port", req.getRemotePort());
    obj.addProperty("request_remote_host", req.getRemoteHost());
    obj.addProperty("request_remote_user", req.getRemoteUser());

    String readBody = "success";
    // read the whole remaining body and put the joined base64 encoded message into the json object
    try {
        byte[] ba = IOUtils.toByteArray(this.in);
        byte[] combined;
        if ((ba != null) && (this.incomplete != null)) {
            combined = new byte[ba.length + this.incomplete.length];
            System.arraycopy(incomplete, 0, combined, 0, this.incomplete.length);
            System.arraycopy(ba, 0, combined, this.incomplete.length, ba.length);
            obj.addProperty("request_body", Base64.getEncoder().encodeToString(combined));
        } else if (ba != null) {
            combined = ba;
        } else if (this.incomplete != null) {
            combined = this.incomplete;
        } else {
            readBody = "body is empty";
        }
    } catch (Exception ex) {
        readBody = String.format("failed because: %s", ex.getCause());
    }
    obj.addProperty("read_body", readBody);

    return obj.toString();
}

From source file:com.baidu.rigel.biplatform.ac.util.AesUtil.java

/**
 * ??/*from   w  w  w.j av  a  2 s .  c  om*/
 * 
 * @param data 
 * @param keyValue 
 * @return ?
 * @throws IllegalArgumentException 
 * @throws Exception InvalidKeyExceptionIllegalBlockSizeException, BadPaddingException?
 */
public String encrypt(String data, String keyValue) throws Exception {
    if (StringUtils.isBlank(data)) {
        throw new IllegalArgumentException("encode string can not be blank!");
    }
    Key key = generateKey(keyValue);
    Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
    ;
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = cipher.doFinal(data.getBytes());
    String encryptedValue = Base64.getEncoder().encodeToString(encVal);
    return encryptedValue;
}

From source file:com.tesco.mewbase.bson.Bson.java

@SuppressWarnings("unchecked")
static Object checkAndCopy(Object val, boolean copy) {
    if (val == null) {
        // OK/*from w  w w  . j a  v a2s .c o m*/
    } else if (val instanceof Number && !(val instanceof BigDecimal)) {
        // OK
    } else if (val instanceof Boolean) {
        // OK
    } else if (val instanceof String) {
        // OK
    } else if (val instanceof Character) {
        // OK
    } else if (val instanceof CharSequence) {
        val = val.toString();
    } else if (val instanceof BsonObject) {
        if (copy) {
            val = ((BsonObject) val).copy();
        }
    } else if (val instanceof BsonArray) {
        if (copy) {
            val = ((BsonArray) val).copy();
        }
    } else if (val instanceof Map) {
        if (copy) {
            val = (new BsonObject((Map) val)).copy();
        } else {
            val = new BsonObject((Map) val);
        }
    } else if (val instanceof List) {
        if (copy) {
            val = (new BsonArray((List) val)).copy();
        } else {
            val = new BsonArray((List) val);
        }
    } else if (val instanceof byte[]) {
        val = Base64.getEncoder().encodeToString((byte[]) val);
    } else if (val instanceof Instant) {
        val = ISO_INSTANT.format((Instant) val);
    } else {
        throw new IllegalStateException("Illegal type in BsonObject: " + val.getClass());
    }
    return val;
}