Android Open Source - NewAndroidTwitter Oauth Signature






From Project

Back to project page NewAndroidTwitter.

License

The source code is released under:

Apache License

If you think the Android project NewAndroidTwitter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.londatiga.android.twitter.oauth;
//from  ww  w . j a  v  a2 s. c om
import java.security.GeneralSecurityException;
import java.io.UnsupportedEncodingException;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import net.londatiga.android.twitter.util.Base64;
import net.londatiga.android.twitter.util.Debug;
import net.londatiga.android.twitter.util.URIUtil;

/**
 * Oauth signature.
 * 
 * @author Lorensius W. L. T <lorenz@londatiga.net>
 *
 */
public class OauthSignature {
  public static final String MAC_NAME = "HmacSHA1";

  public String createSignatureBase(String method, String url, String queryString) {
    StringBuffer sb = new StringBuffer();
    
    sb.append(method);
    sb.append("&");
    sb.append(URIUtil.encode(url));
    sb.append("&");
    sb.append(URIUtil.encode(queryString));
    
    return sb.toString();
  }

  public String createRequestSignature(String signatureBase, String consumerSecret, String tokenSecret) 
    throws Exception {
    
    try {
            String keyString   = URIUtil.encode(consumerSecret) + '&' + URIUtil.encode(tokenSecret);
            
            Debug.i("key " + keyString);
            
            byte[] keyBytes   = keyString.getBytes("UTF-8");

            SecretKey key     = new SecretKeySpec(keyBytes, MAC_NAME);
            Mac mac       = Mac.getInstance(MAC_NAME);
            
            mac.init(key);

            byte[] text     = signatureBase.getBytes("UTF-8");

            return Base64.encodeBytes(mac.doFinal(text)).trim();
        } catch (GeneralSecurityException e) {
            throw e;
        } catch (UnsupportedEncodingException e) {
            throw e;
        }
  }
}




Java Source Code List

net.londatiga.android.example.BaseActivity.java
net.londatiga.android.example.MainActivity.java
net.londatiga.android.example.UserActivity.java
net.londatiga.android.twitter.TwitterDialog.java
net.londatiga.android.twitter.TwitterRequest.java
net.londatiga.android.twitter.TwitterUser.java
net.londatiga.android.twitter.Twitter.java
net.londatiga.android.twitter.http.HttpConn.java
net.londatiga.android.twitter.http.HttpParams.java
net.londatiga.android.twitter.http.HttpValues.java
net.londatiga.android.twitter.oauth.OauthAccessToken.java
net.londatiga.android.twitter.oauth.OauthConsumer.java
net.londatiga.android.twitter.oauth.OauthHeader.java
net.londatiga.android.twitter.oauth.OauthProvider.java
net.londatiga.android.twitter.oauth.OauthSignature.java
net.londatiga.android.twitter.oauth.OauthToken.java
net.londatiga.android.twitter.oauth.OauthUtil.java
net.londatiga.android.twitter.util.Base64.java
net.londatiga.android.twitter.util.Cons.java
net.londatiga.android.twitter.util.Debug.java
net.londatiga.android.twitter.util.StringUtil.java
net.londatiga.android.twitter.util.URIUtil.java