我正在使用规范文档来访问API,它说哈希是根据以下逻辑生成的签名计算的:
Signature = (field1 + field2 + field3 + field4 + field5 + field6) + (field7 + field8)
我只是想知道这是什么意思?
因此,当我使用sha-256连接字段和哈希时,我得到的哈希格式与预期的32字节不同;示例哈希具有以下格式:
PajZG3NEUUHgrycwtPKcKkvTdBg / Kkx6OhlULgSV + KO = 与此示例相反:
c7477242d3901f537387b2b6c61099380634c013a060960a5bf4d87734d54f0e
这是我的代码:
stringToHash = "field1field2field3field4field5field6field7field8";
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(stringToHash.getBytes(StandardCharsets.UTF_8));
String encoded = Base64.getEncoder().encodeToString(hash);
我能错过什么?
答案 0 :(得分:0)
我从这个链接中找到了答案:http://www.mytecbits.com/tools/cryptography/sha2generator 这段代码就是这样做的:
String input = "String to hash";
MessageDigest objSHA = MessageDigest.getInstance("SHA-256");
byte[] bytSHA = objSHA.digest(input.getBytes());
BigInteger intNumber = new BigInteger(1, bytSHA);
String stringHashCode = intNumber.toString(16);
// pad with 0 if the hexa digits are less then 64.
while (stringHashCode.length() < 64) {
stringHashCode = "0" + stringHashCode;
}