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) 

Source Link

Document

Encodes binary data using the base64 algorithm but does not chunk the output.

Usage

From source file:net.sf.serfj.serializers.Base64Serializer.java

/**
 * Serialize object to an encoded base64 string.
 *///w w w . j a  v  a  2  s.  c o  m
public String serialize(Object object) {
    ObjectOutputStream oos = null;
    ByteArrayOutputStream bos = null;
    try {
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        return new String(Base64.encodeBase64(bos.toByteArray()));
    } catch (IOException e) {
        LOGGER.error("Can't serialize data on Base 64", e);
        throw new IllegalArgumentException(e);
    } catch (Exception e) {
        LOGGER.error("Can't serialize data on Base 64", e);
        throw new IllegalArgumentException(e);
    } finally {
        try {
            if (bos != null) {
                bos.close();
            }
        } catch (Exception e) {
            LOGGER.error("Can't close ObjetInputStream used for serialize data on Base 64", e);
        }
    }
}

From source file:com.aqnote.shared.cryptology.asymmetric.dsa.DSAKeyPairGenTest.java

public static void generator() throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);

    // ???/*from w ww  .j  a  v a 2s.  c  om*/
    SecureRandom secrand = new SecureRandom();
    secrand.setSeed(seed); // ??
    // ??, ??keysize ?. 512  1024  64 ?
    keygen.initialize(512, secrand);
    // ?pubkey?prikey
    KeyPair keys = keygen.generateKeyPair(); // ?
    PublicKey pubkey = keys.getPublic();
    PrivateKey prikey = keys.getPrivate();

    byte[] pubkeyByteArray = Base64.encodeBase64(pubkey.getEncoded());
    OutputStream os = new FileOutputStream(new File(PUBKEY_FILE_NAME));
    ByteArrayInputStream bais = new ByteArrayInputStream(pubkeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();

    byte[] prikeyByteArray = Base64.encodeBase64(prikey.getEncoded());
    os = new FileOutputStream(new File(PRIKEY_FILE_NAME));
    bais = new ByteArrayInputStream(prikeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();
}

From source file:com.ibm.iot.auto.bluemix.samples.ui.Connection.java

public Connection(Configuration config) {
    this.apiBaseUrl = config.apiBaseUrl();
    this.tenantId = config.tenantId();
    this.userName = config.userName();
    this.password = config.password();
    String authorizationString = userName + ":" + password;
    authorizationValue = new String(Base64.encodeBase64(authorizationString.getBytes()));
}

From source file:cz.muni.fi.mushroomhunter.restclient.LocationUpdateSwingWorker.java

@Override
protected Integer doInBackground() throws Exception {
    DefaultTableModel model = (DefaultTableModel) restClient.getTblLocation().getModel();
    int selectedRow = restClient.getTblLocation().getSelectedRow();

    String plainCreds = RestClient.USER_NAME + ":" + RestClient.PASSWORD;
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    List<MediaType> mediaTypeList = new ArrayList<>();
    mediaTypeList.add(MediaType.ALL);/*from  www  .j a  v  a2 s . com*/
    headers.setAccept(mediaTypeList);

    headers.add("Authorization", "Basic " + base64Creds);

    HttpEntity request = new HttpEntity<>(headers);

    ResponseEntity<LocationDto> responseEntity = restTemplate.exchange(
            RestClient.SERVER_URL + "pa165/rest/location/" + RestClient.getLocationIDs().get(selectedRow),
            HttpMethod.GET, request, LocationDto.class);

    LocationDto locationDto = responseEntity.getBody();

    locationDto.setName(restClient.getTfLocationName().getText());
    locationDto.setDescription(restClient.getTfLocationDescription().getText());
    locationDto.setNearCity(restClient.getTfLocationNearCity().getText());

    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String json = ow.writeValueAsString(locationDto);
    request = new HttpEntity(json, headers);

    restTemplate.exchange(RestClient.SERVER_URL + "pa165/rest/location", HttpMethod.PUT, request,
            LocationDto.class);
    return selectedRow;
}

From source file:com.rackspacecloud.blueflood.io.serializers.astyanax.TimerSerializationTest.java

@Test
public void testV1RoundTrip() throws IOException {
    // build up a Timer
    BluefloodTimerRollup r0 = new BluefloodTimerRollup().withSum(Double.valueOf(42)).withCountPS(23.32d)
            .withAverage(56).withVariance(853.3245d).withMinValue(2).withMaxValue(987).withCount(345);
    r0.setPercentile("foo", 741.32d);
    r0.setPercentile("bar", 0.0323d);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //The V1 serialization is artificially constructed for the purposes of this test and should no longer be used.
    baos.write(/*from w  ww  . j av  a2s .  com*/
            Base64.encodeBase64(Serializers.timerRollupInstance.toByteBufferWithV1Serialization(r0).array()));
    baos.write("\n".getBytes());
    baos.close();

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(new ByteArrayInputStream(baos.toByteArray())));
    ByteBuffer bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
    BluefloodTimerRollup r1 = Serializers.timerRollupInstance.fromByteBuffer(bb);
    Assert.assertEquals(r0, r1);
}

From source file:io.urmia.util.DigestUtilTest.java

@Test
public void testDigest2() throws Exception {
    InputStream in1 = this.getClass().getClassLoader().getResourceAsStream("abc.txt");
    InputStream in2 = this.getClass().getClassLoader().getResourceAsStream("abc.txt");
    byte[] md5 = org.apache.commons.codec.digest.DigestUtils.md5(in1);
    String md5commons = new String(Base64.encodeBase64(md5));
    String md5util = md5sum(in2);
    System.out.println("sum = " + md5util);
    Assert.assertEquals(md5commons, md5util);
    Assert.assertEquals("kAFQmDzST7DWlj99KOF/cg==", md5util);
}

From source file:com.dianxin.imessage.common.util.SignUtil.java

public static String sign(String src, PrivateKey privateKey) throws Exception {
    Signature rsa = Signature.getInstance("SHA1WithRSA");
    rsa.initSign(privateKey);/*from  w  ww.ja  v  a 2s.  co  m*/
    rsa.update(src.getBytes());
    byte[] sign = rsa.sign();
    return new String(Base64.encodeBase64(sign));
}

From source file:davmail.util.IOUtil.java

/**
 * Base64 encode value.// w  w  w. java 2  s  .c  om
 *
 * @param value input value
 * @return base64  value
 * @throws IOException on error
 */
public static String encodeBase64AsString(String value) throws IOException {
    return new String(Base64.encodeBase64(value.getBytes("UTF-8")), "ASCII");
}

From source file:net.orpiske.tcs.service.rest.functional.DomainCreateTest.java

private HttpHeaders getHeaders(String auth) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    byte[] encodedAuthorisation = Base64.encodeBase64(auth.getBytes());
    headers.add("Authorization", "Basic " + new String(encodedAuthorisation));

    return headers;
}

From source file:com.android.im.imps.StandardPasswordDigest.java

public String digest(String schema, String nonce, String password) throws ImException {
    byte[] digestBytes;
    byte[] inputBytes;

    try {//from   w  w w. ja v  a  2s .  c o m
        inputBytes = (nonce + password).getBytes("ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        throw new ImException(e);
    }

    try {
        if ("SHA".equals(schema))
            schema = "SHA-1";
        MessageDigest md = MessageDigest.getInstance(schema);
        digestBytes = md.digest(inputBytes);
    } catch (NoSuchAlgorithmException e) {
        throw new ImException("Unsupported schema: " + schema);
    }
    return new String(Base64.encodeBase64(digestBytes));
}