Example usage for org.apache.commons.codec.binary Base64 encodeBase64

List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 encodeBase64.

Prototype

public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked) 

Source Link

Document

Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.

Usage

From source file:baldrickv.s3streamingtool.Hash.java

/**
 * Cause apparently a hex encoded MD5 isn't good enough for S3.
 * They want a base64 encode of the raw binary hash.  blech.
 *///  w w w.jav a2  s  .c  o  m
public static String getMd5ForS3(byte b[]) {
    try {
        MessageDigest sig = MessageDigest.getInstance("MD5");
        sig.update(b, 0, b.length);
        byte d[] = sig.digest();

        byte encoded[] = Base64.encodeBase64(d, false);

        return new String(encoded);

    } catch (Exception e) {

        e.printStackTrace();
        System.exit(-1);
        return null;
    }

}

From source file:com.terremark.handlers.BasicAuthenticationHandler.java

/**
 * Default constructor. Takes the credentials and pre-computes the {@code Authorization} header.
 *
 * @param userName User name.//from w w w  . j a v  a  2  s.  c om
 * @param password Password.
 */
public BasicAuthenticationHandler(final String userName, final String password) {
    try {
        final String credentials = userName + ":" + password;
        final byte[] encodedCredentials = Base64.encodeBase64(credentials.getBytes("UTF-8"), false);
        this.authHeader = "Basic " + new String(encodedCredentials, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        throw new IllegalArgumentException(ex);
    }
}

From source file:com.giacomodrago.immediatecrypt.messagecipher.MessageCipher.java

public String encrypt(String source, String password) {

    source = source.trim();//www  .j  a va  2  s  . c o m
    byte[] plaintext;

    plaintext = Compression.compress(source.getBytes(Charsets.UTF_8));

    AESEncryptedMessage encryptedMessage;

    try {
        encryptedMessage = aes.encrypt(plaintext, password);
    } catch (EncryptionException ex) {
        return null;
    }

    String salt = encryptedMessage.getSalt();
    byte[] iv = encryptedMessage.getIv();
    byte[] ciphertext = encryptedMessage.getCiphertext();

    StringBuilder message = new StringBuilder();

    String iv_base64 = Base64.encodeBase64String(iv);
    String ciphertext_base64 = new String(Base64.encodeBase64(ciphertext, true));

    message.append(MESSAGE_HEADER).append(';').append(salt).append(';').append(iv_base64).append(';')
            .append('\n').append(ciphertext_base64);

    return message.toString();

}

From source file:com.netflix.zeno.genericobject.GenericObjectFrameworkSerializer.java

@Override
public void serializeBytes(GenericObject rec, String fieldName, byte[] value) {
    String str = null;//from  ww  w.  j av a  2  s.  co  m
    if (value != null) {
        byte encoded[] = Base64.encodeBase64(value, true);
        try {
            str = new String(encoded, "UTF-8");
        } catch (UnsupportedEncodingException ignore) {
        }
    }
    rec.add(fieldName, str);
}

From source file:com.netflix.zeno.json.JsonFrameworkSerializer.java

@Override
public void serializeBytes(JsonWriteGenericRecord rec, String fieldName, byte[] value) {
    String str = null;//from   www .  ja  v a2s .c  om
    if (value != null) {
        byte encoded[] = Base64.encodeBase64(value, false);
        str = new String(encoded, Charset.forName("UTF-8"));
    }
    serializePrimitive(rec, fieldName, str);
}

From source file:com.springcryptoutils.core.cipher.symmetric.Base64EncodedKeyGeneratorImpl.java

/**
 * Generates a base64 encoded version of a newly instanced symmetric
 * encryption key.//from w  ww.  ja v  a2s  . c  o  m
 *
 * @return the base64 encoded version of a newly instanced symmetric
 *         encryption key
 */
public String generate() {
    return new String(Base64.encodeBase64(generator.generate(), chunkOutput));
}

From source file:com.springcryptoutils.core.signature.Base64EncodedSignerImpl.java

/**
 * Signs a message./*from w  w  w. j  a v a  2 s. c  o  m*/
 *
 * @param message the message to sign
 * @return a base64 encoded version of the signature
 */
public String sign(String message) {
    try {
        final byte[] signature = signer.sign(message.getBytes(charsetName));
        return new String(Base64.encodeBase64(signature, false));
    } catch (UnsupportedEncodingException e) {
        throw new SignatureException("unsupported encoding: charsetName=" + charsetName, e);
    }
}

From source file:cn.vlabs.clb.server.web.AccessTokenService.java

private String generateToken(int appid, int docid, int version, String publicKey) {
    try {/*from  ww  w .j  av a2 s .c  o  m*/
        String raw = appid + "#" + docid + "#" + version;
        byte[] b = Base64.encodeBase64(raw.getBytes(), true);
        String firstKey = new String(b, "UTF-8");
        String secondLevel = publicKey.substring(0, 10) + firstKey + publicKey.substring(11);
        String token = DigestUtils.md5Hex(secondLevel);
        add(token, appid, docid, version);
        return token;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:be.fedict.eidviewer.lib.file.Version4XMLFileCertificates.java

public void fromCertChains(List<X509Certificate> authChain, List<X509Certificate> signChain,
        List<X509Certificate> rrnChain) throws CertificateEncodingException {
    if (authChain != null && authChain.size() == 3) {
        setAuthenticationCertificate(/* w ww.j  a  va2s .  c  o m*/
                new String(Base64.encodeBase64(authChain.get(0).getEncoded(), false)).trim());
        setCitizenCACertificate(new String(Base64.encodeBase64(authChain.get(1).getEncoded(), false)).trim());
        setRootCertificate(new String(Base64.encodeBase64(authChain.get(2).getEncoded(), false)).trim());
    }

    if (signChain != null && signChain.size() == 3)
        setSigningCertificate(new String(Base64.encodeBase64(signChain.get(0).getEncoded(), false)).trim());

    if (rrnChain != null && rrnChain.size() == 2)
        setRRNCertificate(new String(Base64.encodeBase64(rrnChain.get(0).getEncoded(), false)).trim());
}

From source file:com.google.gerrit.server.account.GeneratePassword.java

private String generate() {
    byte[] rand = new byte[LEN];
    rng.nextBytes(rand);/*  w ww  .  j av  a 2s .co m*/

    byte[] enc = Base64.encodeBase64(rand, false);
    StringBuilder r = new StringBuilder(LEN);
    for (int i = 0; i < LEN; i++) {
        if (enc[i] == '=') {
            break;
        }
        r.append((char) enc[i]);
    }
    return r.toString();
}