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

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

Introduction

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

Prototype

public Base64(final int lineLength, final byte[] lineSeparator) 

Source Link

Document

Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.

Usage

From source file:com.vmware.identity.rest.core.util.CertificateHelper.java

/**
 * Converts an encoding into a PEM-formatted string.
 *
 * @param encoding the byte array to encode in the PEM format.
 * @return the PEM-formatted string of the encoding.
 *//*from  www.  ja  v  a2s. co  m*/
public static String convertToPEM(byte[] encoding) {
    StringBuilder builder = new StringBuilder();
    Base64 encoder = new Base64(LINE_LENGTH, CHUNK_SEPARATOR);
    builder.append(BEGIN_CERT).append("\n");

    String encoded = new String(encoder.encode(encoding), Charset.defaultCharset());
    builder.append(encoded);

    builder.append(END_CERT);
    return builder.toString();
}

From source file:com.googlecode.deadalus.integrations.foursquare.FourSquareApi.java

public LocationUpdate getLastKnownLocation(DeadalusUser user) throws IOException {
    String userName = "jwijgerd@gmail.com";
    String password = "xxxxxxx";
    Base64 codec = new Base64(76, new byte[] {});
    // String encodedPW = Base64.encodeBase64URLSafeString((userName+":"+password).getBytes(Charset.forName(HTTP.UTF_8)));
    String encodedPW = codec.encodeToString((userName + ":" + password).getBytes(Charset.forName(HTTP.UTF_8)));
    //((DefaultHttpClient)httpClient).getCredentialsProvider().setCredentials(AUTHSCOPE,new UsernamePasswordCredentials("jwijgerd","I2l0v3JT"));
    HttpGet historyMethod = new HttpGet("http://api.foursquare.com/v1/history.json?l=5");
    historyMethod.setHeader("Authorization", "Basic " + encodedPW);
    HttpResponse response = httpClient.execute(historyMethod);
    if (response.getStatusLine().getStatusCode() == 200) {
        String content = EntityUtils.toString(response.getEntity());
        // now we need to parse the coordinate
        Map<String, Object> jsonContent = mapper.readValue(content, Map.class);
        ArrayList<Map<String, Object>> checkins = (ArrayList<Map<String, Object>>) jsonContent.get("checkins");
        for (Map<String, Object> checkin : checkins) {
            try {
                Date date = dateFormat.parse((String) checkin.get("created"));
                // now get the venue
                Coordinate newLocation = FourSquareUtils
                        .venueToCoordinate((Map<String, Object>) checkin.get("venue"));
                if (newLocation != null) {
                    return new LocationUpdate("http://foursquare.com/", user, newLocation, date);
                }/*ww w .  jav a2 s .  c o m*/
            } catch (ParseException e) {
                // ignore
            }

        }
        return null;
    } else {
        response.getEntity().consumeContent();
        return null;
    }
}

From source file:cc.arduino.plugins.wifi101.flashers.java.NinaFlasher.java

protected static String convertToPem(X509Certificate cert) {
    Base64 encoder = new Base64(64, "\n".getBytes());
    String cert_begin = "-----BEGIN CERTIFICATE-----\n";
    String end_cert = "-----END CERTIFICATE-----";

    try {/* w  ww .j  a v  a2s .c o  m*/
        byte[] derCert = cert.getEncoded();
        String pemCertPre = new String(encoder.encode(derCert));
        String pemCert = cert_begin + pemCertPre + end_cert;
        return pemCert;
    } catch (Exception e) {
        // do nothing
        return "";
    }
}

From source file:curatelist.amazonProductAdvertisingAPI.SignedRequestsHelper.java

/**
 * Compute the HMAC./*from   w  w  w.  j  a  v a  2 s  . co  m*/
 *  
 * @param stringToSign  String to compute the HMAC over.
 * @return              base64-encoded hmac value.
 */
private String hmac(String stringToSign) {
    String signature = null;
    byte[] data;
    byte[] rawHmac;
    try {
        data = stringToSign.getBytes(UTF8_CHARSET);
        rawHmac = mac.doFinal(data);
        //Base64 encoder = new Base64();
        Base64 encoder = new Base64(76, new byte[0]);
        signature = new String(encoder.encode(rawHmac));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(UTF8_CHARSET + " is unsupported!", e);
    }
    return signature;
}

From source file:fr.unice.miage.gamestershop.utils.SignedRequestsHelper.java

/**
 * Compute the HMAC.//  www  .  java2  s . c  o m
 *  
 * @param stringToSign  String to compute the HMAC over.
 * @return              base64-encoded hmac value.
 */
private String hmac(String stringToSign) {
    String signature = null;
    byte[] data;
    byte[] rawHmac;
    try {
        data = stringToSign.getBytes(UTF8_CHARSET);
        rawHmac = mac.doFinal(data);
        Base64 encoder = new Base64(76, new byte[0]);
        //Base64 encoder = new Base64(0);
        //Base64 encoder = new Base64();
        signature = new String(encoder.encode(rawHmac));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(UTF8_CHARSET + " is unsupported!", e);
    }
    return signature;
}

From source file:eu.dety.burp.joseph.attacks.key_confusion.KeyConfusionInfo.java

String transformKeyByPayload(Enum payloadTypeId, PublicKey key) throws UnsupportedEncodingException {
    Base64 base64Pem = new Base64(64, "\n".getBytes("UTF-8"));

    String modifiedKey;/*from ww w .  j  a v  a 2  s  .c  o  m*/

    switch ((PayloadType) payloadTypeId) {

    case PKCS8_WITH_HEADER_FOOTER:
        modifiedKey = "-----BEGIN PUBLIC KEY-----" + Base64.encodeBase64String(key.getEncoded())
                + "-----END PUBLIC KEY-----";
        break;

    case PKCS8_WITH_LF:
        modifiedKey = base64Pem.encodeToString(key.getEncoded());
        break;

    case PKCS8_WITH_HEADER_FOOTER_LF:
        modifiedKey = "-----BEGIN PUBLIC KEY-----\n" + base64Pem.encodeToString(key.getEncoded())
                + "-----END PUBLIC KEY-----";
        break;

    case PKCS8_WITH_HEADER_FOOTER_LF_ENDING_LF:
        modifiedKey = transformKeyByPayload(PayloadType.PKCS8_WITH_HEADER_FOOTER_LF, key) + "\n";
        break;

    case PKCS8:
    default:
        modifiedKey = Base64.encodeBase64String(key.getEncoded());
        break;
    }

    return modifiedKey;
}