我试图用Python实现这个Java方法,但是用纯Python重写它似乎很困难。在
public static String CalculateHash(String input, String token) {
SecretKeySpec signingKey = new SecretKeySpec(token.getBytes(), "HmacSHA1");
Mac mac = null;
mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
assert mac != null;
byte[] bytes = mac.doFinal(input.getBytes(Charset.forName("UTF-8")));
String form = "";
for (byte aByte : bytes) {
String str = Integer.toHexString(((int) aByte) & 0xff);
if (str.length() == 1) {
str = "0" + str;
}
form = form + str;
}
return form;
}
我试过这个,但它产生了另一个散列。在
^{pr2}$我应该在standart Python库中使用什么以及如何重写它?谢谢你
您的python代码和java代码不匹配,因为python代码使用base64,而java代码使用十六进制(base16)。在
您应该将phyton代码更改为使用base16作为其输出,这可以通过
hex()
函数来完成,注意用java代码所做的正确的0字符数字填充数字。在相关问题 更多 >
编程相关推荐