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

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

Introduction

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

Prototype

public static String encodeBase64String(final byte[] binaryData) 

Source Link

Document

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

Usage

From source file:co.tuzza.clicksend4j.http.HttpClientUtilsTest.java

/**
 * Test of getHttpClient method, of class HttpClientUtils.
 *//*ww w.ja v a 2  s  .co  m*/
@Test
public void testGetHttpClient() throws ProtocolException, UnsupportedEncodingException, IOException {
    System.out.println("getHttpClient");
    HttpClientUtils instance = new HttpClientUtils(10000, 10000);
    HttpClient expResult = null;
    HttpClient httpClient = instance.getHttpClient();

    // https://api.clicksend.com/rest/v2/send.json?method=rest&message=This%20is%20the%20message&to=%2B8611111111111%2C%2B61411111111%20
    String loginPassword = "tuzzmaniandevil:5C148EA8-D97B-B5F8-1A3F-1BE0658F4DF5";
    String auth = Base64.encodeBase64String(loginPassword.getBytes());
    HttpUriRequest method;

    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("method", "rest"));
    params.add(new BasicNameValuePair("to", "+61411111111"));
    params.add(new BasicNameValuePair("message", "This is a test message"));

    HttpPost httpPost = new HttpPost("https://api.clicksend.com/rest/v2/send.json");
    httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    httpPost.setHeader("Authorization", "Basic " + auth);

    method = httpPost;

    String url = "https://api.clicksend.com/rest/v2/send.json" + "?" + URLEncodedUtils.format(params, "utf-8");

    HttpResponse httpResponse = httpClient.execute(method);
    int status = httpResponse.getStatusLine().getStatusCode();

    String response = new BasicResponseHandler().handleResponse(httpResponse);
}

From source file:fi.metatavu.edelphi.taglib.chartutil.SvgImageHTMLEmitter.java

@Override
public String generateHTML() throws IOException, BirtException {
    byte[] chartData = ChartModelProvider.getChartData(chartModel, "SVG");

    StringBuilder html = new StringBuilder();
    html.append("<object type=\"image/svg+xml\"");

    if (lazy) {/*w  ww  .  j av a 2  s  .c o  m*/
        String id = UUID.randomUUID().toString();
        ReportChartCache.put(id, chartData);
        html.append(String.format(" data-data=\"/queries/chartimage.binary?id=%s\"", id));
        html.append(" data=\"//cdn.metatavu.io/assets/edelphi/report-image-loader.svg\"");
    } else {
        StringBuilder dataUrlBuilder = new StringBuilder();
        dataUrlBuilder.append("data:image/svg+xml;charset=UTF-8;base64,");
        dataUrlBuilder.append(Base64.encodeBase64String(chartData));

        html.append(String.format(" data=\"%s\"", dataUrlBuilder.toString()));
    }

    html.append(" width=\"").append(width).append('"').append(" height=\"").append(height).append('"')
            .append(" style=\"display: block\"").append("></object>");

    return html.toString();
}

From source file:com.apitrary.orm.codec.image.ImageJPGCodec.java

/** {@inheritDoc} */
@Override//from ww  w . j a v a2 s.  c o  m
public String encode(Image image) {
    if (image == null) {
        return null;
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
        ImageIO.write((BufferedImage) image, FORMAT, byteArrayOutputStream);
        byte[] imageData = byteArrayOutputStream.toByteArray();
        return Base64.encodeBase64String(imageData);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.devicehive.service.helpers.DefaultPasswordProcessor.java

/**
 * Implements self-made hash scheme.// w w  w .j  av a  2s. c  om
 */
@Override
public String hashPassword(String password, String salt) {
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] hashBytes = md.digest((salt + password).getBytes(Constants.UTF8));
        return Base64.encodeBase64String(hashBytes);
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.example.license.DESUtil.java

/**
 * ?//from  w  w  w . jav a2s  .  c o m
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String encrypt(String data, String key) throws Exception {
    Key deskey = keyGenerator(key);
    // Cipher??
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    SecureRandom random = new SecureRandom();
    // ?Cipher?
    cipher.init(Cipher.ENCRYPT_MODE, deskey, random);
    byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
    // http://tripledes.online-domain-tools.com/??
    for (int i = 0; i < results.length; i++) {
        System.out.print(results[i] + " ");
    }
    System.out.println();
    // ??Base64?
    return Base64.encodeBase64String(results);
}

From source file:io.druid.indexing.overlord.autoscaling.ec2.GalaxyEC2UserData.java

@Override
public String getUserDataBase64() {
    try {/*from   w  w w  .java  2  s  .  co m*/
        return Base64.encodeBase64String(jsonMapper.writeValueAsBytes(this));
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:localca.certstore.MockCertificateStorage.java

@Override
public void insert(String commonName, X509Certificate certificate) throws CertificateStorageException {
    if (map.containsKey(commonName)) {
        throw new CertificateStorageException(CertificateStorageExceptionType.ALREADY_EXISTS, commonName);
    }/* w w  w .  j a  va2s  .  c  om*/
    try {
        map.put(commonName,
                new StoredCertificate(commonName, Base64.encodeBase64String(certificate.getEncoded())));
    } catch (CertificateEncodingException e) {
        throw new CertificateStorageException(e, CertificateStorageExceptionType.ENCODING_ERROR, commonName);
    }
}

From source file:com.urswolfer.gerrit.client.rest.http.changes.FileApiRestClientTest.java

@Test
public void testContent() throws Exception {
    String requestUrl = getBaseRequestUrl() + "/content";
    String base64String = Base64.encodeBase64String(FILE_CONTENT.getBytes("UTF-8"));
    HttpResponse httpResponse = EasyMock.createMock(HttpResponse.class);
    HttpEntity httpEntity = EasyMock.createMock(HttpEntity.class);
    EasyMock.expect(httpEntity.getContent())
            .andStubReturn(new ByteArrayInputStream(base64String.getBytes("UTF-8")));
    EasyMock.expect(httpResponse.getEntity()).andStubReturn(httpEntity);
    EasyMock.expect(httpResponse.getFirstHeader("X-FYI-Content-Encoding"))
            .andStubReturn(new BasicHeader("X-FYI-Content-Type", "base64"));
    EasyMock.expect(httpResponse.getFirstHeader("X-FYI-Content-Type"))
            .andStubReturn(new BasicHeader("X-FYI-Content-Type", "text/plain"));
    EasyMock.replay(httpEntity, httpResponse);

    setupServices();//w  w  w  .j  a va  2 s .c o  m

    GerritRestClient gerritRestClient = new GerritRestClientBuilder()
            .expectRequest(requestUrl, null, GerritRestClient.HttpVerb.GET, httpResponse).get();

    FileApiRestClient fileApiRestClient = new FileApiRestClient(gerritRestClient, revisionApiRestClient, null,
            FILE_PATH);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BinaryResult binaryResult = fileApiRestClient.content();
    try {
        binaryResult.writeTo(byteArrayOutputStream);
        String actualContent = new String(Base64.decodeBase64(byteArrayOutputStream.toString()));

        Truth.assertThat(actualContent).isEqualTo(FILE_CONTENT);
        Truth.assertThat(binaryResult.isBase64()).isTrue();
        Truth.assertThat(binaryResult.getContentType()).isEqualTo("text/plain");
        EasyMock.verify(gerritRestClient);
    } finally {
        binaryResult.close();
        byteArrayOutputStream.close();
    }
}

From source file:de.alpharogroup.crypto.key.PublicKeyExtensions.java

/**
 * Transform the given {@link PublicKey} to a base64 encoded {@link String} value.
 *
 * @param publicKey/*  w  ww  . ja  v  a  2 s .com*/
 *            the public key
 * @return the base64 encoded {@link String} value.
 */
public static String toBase64(final PublicKey publicKey) {
    final byte[] encoded = publicKey.getEncoded();
    final String publicKeyAsBase64String = Base64.encodeBase64String(encoded);
    return publicKeyAsBase64String;
}

From source file:net.bryansaunders.jee6divelog.util.SecurityUtilsTest.java

/**
 * Test method for generatePasswordHash.
 */// www. j av  a 2s  .  c o  m
@Test
public void testGeneratePasswordHash() {
    // given
    final String password = "pass123";

    // when
    final String hashedPass = SecurityUtils.generatePasswordHash(password);

    // then
    final String expectedPass = Base64.encodeBase64String(DigestUtils.sha256Hex(password).getBytes());
    assertEquals(expectedPass, hashedPass);
}