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

如何在android中生成HMAC-SHA1签名?,android,linkedin,Android,Linkedin,这是我的基本字符串: String args ="oauth_consumer_key="+enc(consumerkey) + "&oauth_nonce="+enc(generateNonce()) + "&oauth_signature_method=HMAC-SHA1" + "&oauth_timestamp="+ timestamp

这是我的基本字符串:

        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);
这是我的标题:

        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+"\"" ;
我使用以下方法生成签名:

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);
}
在执行此代码时,我得到以下错误

oauth_problem=signature_invalid&    
oauth_problem_advice=com.linkedin.security.auth.pub.LoginDeniedInvalidAuthTokenException
有人能帮我解决这个问题吗


谢谢…

凌乱,但我用的是:

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 "";
    }
然后像这样调用它:

hash_hmac("HmacSHA1", value, key);

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);
}
私有静态字符串hmacSha1(字符串值、字符串键)
抛出不支持的CodingException、NoSuchAlgorithmException、,
InvalidKeyException{
字符串类型=“HmacSHA1”;
SecretKeySpec secret=新的SecretKeySpec(key.getBytes(),type);
Mac Mac=Mac.getInstance(类型);
mac.init(秘密);
byte[]bytes=mac.doFinal(value.getBytes());
返回bytesToHex(字节);
}
私有最终静态字符[]hexArray=“0123456789abcdef”.toCharArray();
私有静态字符串bytesToHex(字节[]字节){
char[]hexChars=新字符[bytes.length*2];
INTV;
对于(int j=0;j>>4];
hexChars[j*2+1]=hexArray[v&0x0F];
}
返回新字符串(hexChars);
}

谢谢你的回答…你能告诉我传递给Hashhmac的参数是什么吗?我正在为LinkedIn使用此签名一点也不熟悉LinkedIn签名验证算法,抱歉。您最好看看这样的内容:值和密钥参数是用来做什么的?我应该作为密钥传递什么?密钥是您要使用的加密密钥/salt找不到方法
android.util.Base64.encodeBase64String(…)
你能解释一下这里的值和密钥是什么吗?@user512 hmac有一条消息你想加密,还有一个密钥你想用它来加密你的消息<代码>值是一条消息<代码>键是一个键