java 相当于 python 用于散列
java equivalent to python for hashing
我在 java 文档中有以下代码(需要 secret_key
和 data
作为输入):
javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1")
mac.init(new javax.crypto.spec.SecretKeySpec(secret_key.getBytes(), "HmacSHA1"))
byte[] hexBytes = new org.apache.commons.codec.binary.Hex().encode(mac.doFinal(data.getBytes()))
String signature = new String(hexBytes, "UTF-8")
在网上做了一些 RnD 之后,我写了等价的 python 到 :
decodedKey = secret_key.decode("hex")
hmac_val = hmac.new(decodedKey, data.encode('UTF-8'), hashlib.sha1)
signature = hmac_val.digest().encode('base64')
但是在 header 中使用此签名值执行 post 请求时,我得到
ValueError: Invalid header value 'XXXXXXXXXX'
我的 python 等价物是否正确?如果有人能解释一下,那将是很大的帮助!
编辑
Java
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
String secret_key = "c84766ca4a3ce52c3602bbf02ad1f7";
String data = "some data";
javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
mac.init(new javax.crypto.spec.SecretKeySpec(secret_key.getBytes(), "HmacSHA1"));
byte[] hexBytes = new org.apache.commons.codec.binary.Hex().encode(mac.doFinal(data.getBytes()));
String signature = new String(hexBytes, "UTF-8");
System.out.println("signature : "+signature);
}
o/p
signature : 2b565c0476eed0f350ddb3a2852a4cab91281bdc
Python :
In [1]: import hmac
In [2]: import hashlib
In [3]: secret_key = "c84766ca4a3ce52c3602bbf02ad1f7"
In [4]: data = "some data"
In [5]: decodedKey = secret_key.decode("hex")
In [6]: hmac_val = hmac.new(decodedKey, data.encode('UTF-8'), hashlib.sha1)
In [7]: signature = hmac_val.digest().encode('base64')
In [8]: signature
Out[8]: '3qE5SqSdvBEJcy8mSF+srqNXCd4=\n'
In [9]:
pycrypto 有一个散列函数 https://pypi.python.org/pypi/pycrypto
因为 ValueError:无效 header 值 'XXXXXXXXXX' 请参阅此线程 ValueError: Invalid header value 'H2O Python client/2.7.9 (default, Apr 2 2015, 15:33:21) \n[GCC 4.9.2]'
也许您 post 中的 header 与您用于 post
的库不兼容
您在 python 代码中导入了哪些库?
如果你想轻松一点,试试这个:https://pythonhosted.org/pycrypto/Crypto.Hash.HMAC-module.html
也许编码会影响结果,[UTF-8]然后[base-64]
指的是这个线程:
Java method which can provide the same output as Python method for HMAC-SHA256 in Hex
sha1
的小调整,下面是简单的等价物:
In [13]: print hmac.new(secret_key, data, hashlib.sha1).hexdigest()
2b565c0476eed0f350ddb3a2852a4cab91281bdc
我在 java 文档中有以下代码(需要 secret_key
和 data
作为输入):
javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1")
mac.init(new javax.crypto.spec.SecretKeySpec(secret_key.getBytes(), "HmacSHA1"))
byte[] hexBytes = new org.apache.commons.codec.binary.Hex().encode(mac.doFinal(data.getBytes()))
String signature = new String(hexBytes, "UTF-8")
在网上做了一些 RnD 之后,我写了等价的 python 到 :
decodedKey = secret_key.decode("hex")
hmac_val = hmac.new(decodedKey, data.encode('UTF-8'), hashlib.sha1)
signature = hmac_val.digest().encode('base64')
但是在 header 中使用此签名值执行 post 请求时,我得到
ValueError: Invalid header value 'XXXXXXXXXX'
我的 python 等价物是否正确?如果有人能解释一下,那将是很大的帮助!
编辑
Java
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
String secret_key = "c84766ca4a3ce52c3602bbf02ad1f7";
String data = "some data";
javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
mac.init(new javax.crypto.spec.SecretKeySpec(secret_key.getBytes(), "HmacSHA1"));
byte[] hexBytes = new org.apache.commons.codec.binary.Hex().encode(mac.doFinal(data.getBytes()));
String signature = new String(hexBytes, "UTF-8");
System.out.println("signature : "+signature);
}
o/p
signature : 2b565c0476eed0f350ddb3a2852a4cab91281bdc
Python :
In [1]: import hmac
In [2]: import hashlib
In [3]: secret_key = "c84766ca4a3ce52c3602bbf02ad1f7"
In [4]: data = "some data"
In [5]: decodedKey = secret_key.decode("hex")
In [6]: hmac_val = hmac.new(decodedKey, data.encode('UTF-8'), hashlib.sha1)
In [7]: signature = hmac_val.digest().encode('base64')
In [8]: signature
Out[8]: '3qE5SqSdvBEJcy8mSF+srqNXCd4=\n'
In [9]:
pycrypto 有一个散列函数 https://pypi.python.org/pypi/pycrypto
因为 ValueError:无效 header 值 'XXXXXXXXXX' 请参阅此线程 ValueError: Invalid header value 'H2O Python client/2.7.9 (default, Apr 2 2015, 15:33:21) \n[GCC 4.9.2]'
也许您 post 中的 header 与您用于 post
的库不兼容您在 python 代码中导入了哪些库?
如果你想轻松一点,试试这个:https://pythonhosted.org/pycrypto/Crypto.Hash.HMAC-module.html
也许编码会影响结果,[UTF-8]然后[base-64]
指的是这个线程:
Java method which can provide the same output as Python method for HMAC-SHA256 in Hex
sha1
的小调整,下面是简单的等价物:
In [13]: print hmac.new(secret_key, data, hashlib.sha1).hexdigest()
2b565c0476eed0f350ddb3a2852a4cab91281bdc