HMACSHA1签名在Java和C#代码中生成不同的签名

时间:2016-06-08 17:54:21

标签: java

爪哇:

SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(),HMAC_SHA1_ALGORITHM);

Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey);

byte[] bytes = mac.doFinal(signatureString.getBytes()); return BASE64.encodeBAse64String(bytes);

C#:

public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, signatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters) {

 string signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, httpMethod, timeStamp, nonce, HMACSHA1SignatureType, out normalizedUrl, out normalizedRequestParameters);

 HMACSHA1 hmacsha1 = new HMACSHA1();

 hmacsha1.key = Encoding.ASCII.GetBytes(string.Format("{0} & {1}", UrlEncode(consumerSecret), string.IsNullEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret)));

 //data is generated signature base string with attaching all parameters.

 byte[] dataBuffer = System.Text.Encoding.ASCII.GetBytes(data);
 byte[] hashBytes = hashAlgorithm.ComputeHash(dataBuffer);
 return Convert.ToBase64String(hashBytes);
}

我正在创建REST服务,我正在使用OAuth 1.0签名并生成Oauth我需要使用HMACSHA1算法。我的客户端API在C#中,我的API是用Java编写的,我必须生成一个应该由我的客户端成功加密的签名。

0 个答案:

没有答案