HMAC-SHA1:如何在Java中正确执行它? [英] HMAC-SHA1: How to do it properly in Java?
问题描述
我使用HMAC-SHA1散列一些值,在Java中使用以下代码:
public static String hmacSha1字符串值,字符串键){
尝试{
//从原始密钥字节获取hmac_sha1密钥
byte [] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes,HmacSHA1);
//获取hmac_sha1 Mac实例并使用签名密钥初始化
Mac mac = Mac.getInstance(HmacSHA1);
mac.init(signingKey);
//计算输入数据字节的hmac
byte [] rawHmac = mac.doFinal(value.getBytes());
//将原始字节转换为十六进制
byte [] hexBytes = new Hex()。encode(rawHmac);
//向字符串隐藏十六进制字节数组
返回新字符串(hexBytes,UTF-8);
} catch(Exception e){
throw new RuntimeException(e);
code
$ b Hex() code>属于 org.apache.commons.codec
在PHP中,有一个类似的函数 hash_hmac(算法,数据,键)
,用于比较我的Java实现返回的值。
尝试是:
$ p $ hash_hmac(sha1,helloworld,mykey)// PHP
code>
返回: 74ae5a4a3d9996d5918defc2c3d475471bbf59ac
我的Java函数也返回 74ae5a4a3d9996d5918defc2c3d475471bbf59ac
。
hash_hmac(sha1,helloworld,PRIE7 $ oG2uS-Yf17kEnUEpi5hvW /#AFo)// PHP
返回: 这次我的Java impl返回: 我的PHP代码返回的哈希不等于我的Java函数返回的值,我找不到原因。 任何在你的PHP方面,围绕关键点使用单引号,这样 e98bcc5c5be6f11dc582ae55f520d1ec4ae29f7a $
$ b c19fccf57c613f1868dd22d586f9571cf6412cd0
$
hash_hmac(sha1,helloworld,'PRIE7 $ oG2uS-Yf17kEnUEpi5hvW /#AFo')
否则,您真正获得的密钥是 PRIE7-Yf17kEnUEpi5hvW /#AFo
(假设变量 $ oG2uS
未定义)。
I'm hashing some values using HMAC-SHA1, using the following code in Java:
public static String hmacSha1(String value, String key) {
try {
// Get an hmac_sha1 key from the raw key bytes
byte[] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(value.getBytes());
// Convert raw bytes to Hex
byte[] hexBytes = new Hex().encode(rawHmac);
// Covert array of Hex bytes to a String
return new String(hexBytes, "UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Hex()
belongs to org.apache.commons.codec
In PHP there's a similar function hash_hmac(algorithm, data, key)
that I use to compare the values returned by my Java implementation.
So the first try is:
hash_hmac("sha1", "helloworld", "mykey") // PHP
that returns: 74ae5a4a3d9996d5918defc2c3d475471bbf59ac
My Java function returns 74ae5a4a3d9996d5918defc2c3d475471bbf59ac
as well.
Ok, it seems working. Then I try to use a more complex key:
hash_hmac("sha1", "helloworld", "PRIE7$oG2uS-Yf17kEnUEpi5hvW/#AFo") // PHP
that returns: e98bcc5c5be6f11dc582ae55f520d1ec4ae29f7a
While this time my Java impl returns: c19fccf57c613f1868dd22d586f9571cf6412cd0
The hash returned by my PHP code is not equal to the value returned by my Java function, and I can't find out why.
Any tips?
On your PHP side, use single-quotes around the key so that the $
character is not treated as a variable reference. i.e.,
hash_hmac("sha1", "helloworld", 'PRIE7$oG2uS-Yf17kEnUEpi5hvW/#AFo')
Otherwise, the key you really get is PRIE7-Yf17kEnUEpi5hvW/#AFo
(assuming the variable $oG2uS
is not defined).
这篇关于HMAC-SHA1:如何在Java中正确执行它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!