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:backtype.storm.messaging.netty.SaslUtils.java

/**
 * Encode a identifier as a base64-encoded char[] array.
 * //from   w w  w  .  j  a va2 s. c om
 * @param identifier
 *            as a byte array.
 * @return identifier as a char array.
 */
static String encodeIdentifier(byte[] identifier) {
    return new String(Base64.encodeBase64(identifier), Charsets.UTF_8);
}

From source file:Bing.java

/**
 *
 * @param query - query to use for the search
 * @return - a json array of results. each contains a title, description, url,
 * and some other metadata that can be easily extracted since its in json format
 *///  www .j a v  a 2 s  .c o  m
public static JSONArray search(String query) {
    try {
        query = "'" + query + "'";
        String encodedQuery = URLEncoder.encode(query, "UTF-8");
        System.out.println(encodedQuery);
        //            URL url = new URL("https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27car%27");
        //            URL url = new URL("http://csb.stanford.edu/class/public/pages/sykes_webdesign/05_simple.html");
        String webPage = "https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query="
                + encodedQuery + "&$format=JSON";
        String name = "6604d12c-3e89-4859-8013-3132f78c1595";
        String password = "cefgNRl3OL4PrJJvssxkqLw0VKfYNCgyTe8wNXotUmQ";

        String authString = name + ":" + password;
        System.out.println("auth string: " + authString);
        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);
        System.out.println("Base64 encoded auth string: " + authStringEnc);

        URL url = new URL(webPage);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);

        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine).append("\n");
        }

        in.close();
        JSONParser parser = new JSONParser();
        JSONArray arr = (JSONArray) ((JSONObject) ((JSONObject) parser.parse(response.toString())).get("d"))
                .get("results");
        JSONObject obj = (JSONObject) arr.get(0);
        JSONArray out = (JSONArray) obj.get("Web");

        return out;

        //

    } catch (MalformedURLException ex) {
        Logger.getLogger(Bing.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Bing.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(Bing.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.philosophy.LicenseGen.java

public LicenseGen(String text) {
    license = text;/* ww w. j  a v a2  s.  c  o m*/
    byte[] keyBytes = { 88, 42, 94, 52, 56, 81, 82, 98 };
    System.out.println("Original License : " + license);
    try {
        SecretKey key = new SecretKeySpec(keyBytes, "DES");
        Cipher cipher = Cipher.getInstance("DES");
        byte[] data = license.getBytes();
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypted = cipher.doFinal(data);
        System.out.println("Encrypted license : " + new String(encrypted));
        //now convert to base64
        byte[] base64Enc = Base64.encodeBase64(encrypted);
        String encodedLicense = new String(base64Enc);
        System.out.println("Encoded license is : " + encodedLicense);
        System.out.println("Encoding string length is : " + encodedLicense.length());
    } catch (NoSuchAlgorithmException e) {
    } catch (NoSuchPaddingException e) {
    } catch (InvalidKeyException e) {
    } catch (IllegalStateException e) {
    } catch (IllegalBlockSizeException e) {
    } catch (BadPaddingException e) {
    }
}

From source file:AddressSvc.AddressSvc.java

public ValidateResult Validate(Address address) {

    //Create query/url
    String queryparams = address.toQuery();
    String addrval = svcURL + "/1.0/address/validate?" + queryparams;
    URL url;// w  w w .  j  a v  a2s  .c  o m

    HttpURLConnection conn;
    try {
        //Connect to specified URL with authorization header
        url = new URL(addrval);
        conn = (HttpURLConnection) url.openConnection();
        String encoded = "Basic " + new String(Base64.encodeBase64((accountNum + ":" + license).getBytes())); //Create auth content
        conn.setRequestProperty("Authorization", encoded); //Add authorization header
        conn.disconnect();

        ObjectMapper mapper = new ObjectMapper(); //Deserialization object

        if (conn.getResponseCode() != 200) //If we didn't get a success back, print out the error
        {
            ValidateResult vres = mapper.readValue(conn.getErrorStream(), ValidateResult.class); //Deserializes the response object
            return vres;
        }

        else //Otherwise, print out the validated address.
        {
            ValidateResult vres = mapper.readValue(conn.getInputStream(), ValidateResult.class); //Deserializes the response object
            return vres;
        }

    } catch (IOException e) {
        e.printStackTrace();
        return null;

    }
}

From source file:dtu.ds.warnme.app.utils.SecurityUtils.java

public static String hashSHA512Base64(String stringToHash) {
    if (StringUtils.isEmpty(stringToHash)) {
        return StringUtils.EMPTY;
    }// w  w  w  .j av a  2s  .co m
    try {
        byte[] bytes = stringToHash.getBytes(CHARSET_UTF8);
        byte[] md5bytes = DigestUtils.sha512(bytes);
        byte[] base64bytes = Base64.encodeBase64(md5bytes);
        return new String(base64bytes, CHARSET_UTF8);
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "This system does not support required hashing algorithms.", e);
        throw new IllegalStateException("This system does not support required hashing algorithms.", e);
    }
}

From source file:com.konakart.bl.modules.payment.barclaycardsmartpayhosted.BarclaycardSmartPayHostedHMACTools.java

/**
 * Get a HMAC signature using the specified secret 
 * @param secret//from  ww  w.ja va  2 s .com
 * @param signingData
 * @return a Base64 encoded signature
 */
public static String getBase64EncodedSignature(String secret, String signingData) {
    SecretKey key = getMacKey(secret);
    try {
        Mac mac = Mac.getInstance(key.getAlgorithm());
        mac.init(getMacKey(secret));
        byte[] digest = mac.doFinal(signingData.getBytes("UTF8"));
        return new String(Base64.encodeBase64(digest), "ASCII");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.baidu.terminator.plugin.signer.customized.app.Base64ForServer.java

/**
 * byte??64//from ww  w.  ja v a  2  s  .  co m
 * Encode Byte Array into Base64 String
 * 
 * @param data ?byte
 * @return ??64
 * @throws UnsupportedEncodingException ???UTF-8?
 */
public static String byteToBase64(byte[] data) throws UnsupportedEncodingException {
    if (data == null)
        throw new IllegalArgumentException("The parameter should not be null!");
    return new String(Base64.encodeBase64(data), "UTF-8");
}

From source file:jp.or.openid.eiwg.scim.util.SCIMUtil.java

/**
 * BASE64/*from   w  ww .  j ava 2s .  c  o m*/
 *
 * @param val ?
 * @return ?
 * @throws UnsupportedEncodingException
 * @throws Exception
 */
public static String encodeBase64(String val) throws UnsupportedEncodingException {
    return new String(Base64.encodeBase64(val.getBytes("UTF-8")));
}

From source file:com.jk.db.dataaccess.orm.eclipselink.FSSessionPCustomizer.java

/**
 * Encode.//w  ww  . j ava2s . c o m
 *
 * @param value
 *            the value
 * @return the string
 */
public static String encode(String value) {
    return new String(Base64.encodeBase64(value.getBytes()));
}

From source file:cr.ac.siua.tec.utils.PDFGenerator.java

/**
 * Encodes PDF object (PDDocument) to base4.
 *//*from  w  w w  .  ja v a  2 s.  c  om*/
public String encodePDF(PDDocument document) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        document.save(out);
        document.close();
    } catch (COSVisitorException | IOException e) {
        e.printStackTrace();
    }
    byte[] bytes = out.toByteArray();
    byte[] encoded = Base64.encodeBase64(bytes);
    return new String(encoded);
}