如何在android中生成HMAC-SHA1签名?

Tar*_*uni 11 android linkedin

这是我的基本字符串:

        String args ="oauth_consumer_key="+enc(consumerkey) +
                   "&oauth_nonce="+enc(generateNonce()) +
                   "&oauth_signature_method=HMAC-SHA1" +
                   "&oauth_timestamp="+ timestamp +
                   "&oauth_token="+enc(Home.consToken) +
                   "&oauth_verifier="+verifier+"&oauth_version=1.0";
                    String base ="POST&"+enc("https://api.linkedin.com/uas/oauth   /accessToken") +"&"+ enc(args);

       String signature =computeHmac(base,consumer_secret+"&"+secretToken);
Run Code Online (Sandbox Code Playgroud)

这是我的标题:

        String header = "OAuth " +
       "oauth_consumer_key=\""+ enc(consumerkey)+ "\"," +
       "oauth_nonce=\""+ enc(generateNonce()) + "\"," +
       "oauth_signature_method=\"HMAC-SHA1\"," +
       "oauth_timestamp=\""+ timestamp + "\"," +
       "oauth_token=\""+Home.consToken + "\"," +
       "oauth_signature=\""+enc(signature)+"\","+
       "oauth_verifier=\""+verifier +"\","+ 
       "oauth_version=\""+1.0+"\"" ;
Run Code Online (Sandbox Code Playgroud)

我使用以下方法生成签名:

public String computeHmac(String baseString, String key)
    throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException,  UnsupportedEncodingException
{
    Mac mac = Mac.getInstance("HmacSHA1");
    SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());
    mac.init(secret);
    byte[] digest = mac.doFinal(baseString.getBytes());
    byte[] result=Base64.encodeBase64(digest);
    return new String(result);
}
Run Code Online (Sandbox Code Playgroud)

执行此代码时,我收到以下错误...

oauth_problem=signature_invalid&    
oauth_problem_advice=com.linkedin.security.auth.pub.LoginDeniedInvalidAuthTokenException
Run Code Online (Sandbox Code Playgroud)

任何人都能帮帮我吗?

谢谢...

And*_*yeu 30

Femi的答案是绝对正确的,然而,对我来说这究竟是什么并不明显intval(b).据我所知,这是b & 0xFF.

我也应用了一些优化(我在这里找到),这是我的代码:

private static String hmacSha1(String value, String key)
        throws UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeyException {
    String type = "HmacSHA1";
    SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
    Mac mac = Mac.getInstance(type);
    mac.init(secret);
    byte[] bytes = mac.doFinal(value.getBytes());
    return bytesToHex(bytes);
}

private final static char[] hexArray = "0123456789abcdef".toCharArray();

private static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    int v;
    for (int j = 0; j < bytes.length; j++) {
        v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}
Run Code Online (Sandbox Code Playgroud)


Fem*_*emi 7

凌乱,但我正在使用这个:

static String hash_hmac(String type, String value, String key)
    {
    try {
        javax.crypto.Mac mac = javax.crypto.Mac.getInstance(type);
        javax.crypto.spec.SecretKeySpec secret = new javax.crypto.spec.SecretKeySpec(key.getBytes(), type);
        mac.init(secret);
        byte[] digest = mac.doFinal(value.getBytes());
        StringBuilder sb = new StringBuilder(digest.length*2);
        String s;
        for (byte b : digest){
        s = Integer.toHexString(intval(b));
        if(s.length() == 1) sb.append('0');
        sb.append(s);
        }
        return sb.toString();
    } catch (Exception e) {
        android.util.Log.v("TAG","Exception ["+e.getMessage()+"]", e);
    }
        return "";
    }
Run Code Online (Sandbox Code Playgroud)

然后你像这样调用它:

hash_hmac("HmacSHA1", value, key);
Run Code Online (Sandbox Code Playgroud)