[英]Converting a hash to Base64 encoding: Should I be storing the hash as a String or Byte before encoding?
生成 HMAC-SHA1 hash 时遇到问题。 我将其转换为 Base64 并将其发送到外部系统,由其验证结果。 但是,他们正在生产不同的 Base64 编码 hash。 据我所知,他们使用 Java Mac (javax.crypto.mac) 来生成 hash,而我使用的是 Google Guava Codec ZDB974238714CA8DE634A7CE1D083A1。
I checked a couple of online hash generators (ie https://www.freeformatter.com/hmac-generator.html ) and they produced the same hash as me. 我尝试了 Apache Commons Codec API,它也产生了相同的结果。 然而,这个外部系统正在生成不同的 Base64 编码 hash。 秘密/密钥和消息完全相同。
事实证明,首先将 hash 转换为字符串而不是字节是问题所在。 我首先将 hash 转换为字符串,然后使用 Base64 进行编码。 外部系统首先将 hash 转换为字节,然后使用 Base64 进行编码。 我发现的在线生成器似乎首先将 hash 转换为字符串,就像我一样,这就是为什么我的 hash 与在线 hash 生成器匹配的原因。
有没有合适的方法来转换 hash? 对于良好的做法,我应该使用 asBytes()、toString() 还是其他东西?
这是使用番石榴编解码器 API 的代码:
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import java.util.Base64;
public class HmacSha1TokenAuth {
public static void main(String[] args) {
String secret = "12345";
String valueToDigest = "TestUser";
byte[] key = secret.getBytes();
Hasher hasher = Hashing.hmacSha1(key).newHasher();
Hasher hasher2 = Hashing.hmacSha1(key).newHasher();
hasher.putBytes(valueToDigest.getBytes());
hasher2.putBytes(valueToDigest.getBytes());
// This is what I used and also found a few online hash generators using the same approach
String hashAsString = hasher.hash().toString();
// This is what the external system used
byte[] hashAsByte = hasher2.hash().asBytes();
String base64AsString = Base64.getEncoder().encodeToString(hashAsString.getBytes());
String base64AsByte = Base64.getEncoder().encodeToString(hashAsByte);
System.out.println("Hash As String: " + hashAsString);
System.out.println("Hash As String(Converted to Bytes): " + hashAsString.getBytes());
System.out.println("Base64 Value of Hash As String: " + base64AsString);
System.out.println("Hash As Byte: " + hashAsByte);
System.out.println("Base64 Value of Hash As Byte: " + base64AsByte);
/*** My Results ***/
// Hash As String: c8094bb1e0896a3f813036bdaeb37b753d9f4f5b
// Hash As String(Converted to Bytes): [B@61443d8f
// Base64 Value of Hash As String: YzgwOTRiYjFlMDg5NmEzZjgxMzAzNmJkYWViMzdiNzUzZDlmNGY1Yg==
/*** External System Results ***/
// Hash As Byte: [B@445b84c0
// Base64 Value of Hash As Byte: yAlLseCJaj+BMDa9rrN7dT2fT1s=
}
}
Base64 编码字节,散列器产生字节,因此没有理由转换为字符串。
我想您看到的示例是关于从字符串变量开始转换 base64 的。
不同的结果是由于 toString() 或 getBytes() 转换的不同编码。
从 guava 文档中您可以看到 hashcode.toString() 转换为十六进制,而 bytes.toString() 是平台相关的(假设是 UTF8)?
https://guava.dev/releases/16.0/api/docs/com/google/common/hash/HashCode.html#toString() https://guava.dev/releases/16.0/api/docs/com/google /common/hash/HashCode.html#fromString(java.lang.String)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.