Example usage for twitter4j BASE64Encoder encode

List of usage examples for twitter4j BASE64Encoder encode

Introduction

In this page you can find the example usage for twitter4j BASE64Encoder encode.

Prototype

public static String encode(byte[] from) 

Source Link

Usage

From source file:com.daiv.android.twitter.utils.api_helper.APIHelper.java

License:Apache License

/**
 * Generates the signature to use with the header
 * @param data base signature data// w w w  .j a va2s . c o  m
 * @param token the user's access token
 * @return String of the signature to use in your header
 */
public String generateSignature(String data, AccessToken token) {
    byte[] byteHMAC = null;
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec spec;
        String oauthSignature = HttpParameter.encode(AppSettings.TWITTER_CONSUMER_SECRET) + "&"
                + HttpParameter.encode(token.getTokenSecret());
        spec = new SecretKeySpec(oauthSignature.getBytes(), "HmacSHA1");
        mac.init(spec);
        byteHMAC = mac.doFinal(data.getBytes());
    } catch (InvalidKeyException ike) {
        throw new AssertionError(ike);
    } catch (NoSuchAlgorithmException nsae) {
        throw new AssertionError(nsae);
    }
    return BASE64Encoder.encode(byteHMAC);
}

From source file:com.daiv.android.twitter.utils.api_helper.TwitterDMPicHelper.java

License:Apache License

private static String computeSignature(String baseString, String keyString)
        throws GeneralSecurityException, UnsupportedEncodingException {
    SecretKey secretKey = null;/*w  w  w .ja  va  2  s.  co m*/

    byte[] keyBytes = keyString.getBytes();
    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(secretKey);

    byte[] text = baseString.getBytes();

    return new String(BASE64Encoder.encode(mac.doFinal(text))).trim();
}