Siphash tostring 和 getBytes()
Siphash tostring and getBytes()
我已经为 1 个字符串和 2 个长值生成了 SipHash(对于许多这样的字符串和长值组合)。我用过 -
Hasher hash = Hashing.sipHash24().newHasher().putUnencodedChars("abcd").putLong(123).putLong(123);
现在我使用 -
将此哈希值转换为字符串
String hashString = hash.hash().toString();
但是,我想要字符串的字节数组,有什么方法可以从这个字符串中获取字节数组,就像我从 byte[] hashBytes = hash.hash().asBytes();
中获取的字节数组一样我想将从这些哈希中得到的字符串转换为字节数组。
实际上我意识到字节数组只使用了 8 个字节的 space 用于 siphash,而字符串的长度是 18 个字节。所以,我想将哈希存储为字节数组会更加优化。
这是从字符串中获取字节数组的代码 -
public static byte[] getBytes(String hashString) {
final byte[] bytes = new byte[8];
HashMap<Character, String> bin = new HashMap<>();
bin.put('0', "0000");
bin.put('1', "0001");
bin.put('2', "0010");
bin.put('3', "0011");
bin.put('4', "0100");
bin.put('5', "0101");
bin.put('6', "0110");
bin.put('7', "0111");
bin.put('8', "1000");
bin.put('9', "1001");
bin.put('a', "1010");
bin.put('b', "1011");
bin.put('c', "1100");
bin.put('d', "1101");
bin.put('e', "1110");
bin.put('f', "1111");
for (int i = 0; i < 16 && i < hashString.length(); i += 2) {
final BitSet bitset = new BitSet(8);
String byteBinary = bin.get(hashString.charAt(i)) + bin.get(hashString.charAt(i + 1));
for (int j = 0; j<8; j++) {
if (byteBinary.charAt(j) == '1')
bitset.set(7-j, true);
else
bitset.set(7-j, false);
}
bytes[i/2] = bitset.toByteArray()[0];
//System.out.println(byteBinary);
}
return bytes;
}
BaseEncoding.base16().lowerCase().decode(string)
应该将 HashCode.toString()
转换回您从 asBytes()
获得的字节数组。
您可以使用 HashCode.fromString(string)
将字符串解析回 HashCode
实例。然后您可以在 HashCode
实例上调用 .asBytes()
以取回底层 byte[]
.
的副本
所以基本上你想要:
byte[] bytes = HashCode.fromString(string).asBytes();
我已经为 1 个字符串和 2 个长值生成了 SipHash(对于许多这样的字符串和长值组合)。我用过 -
Hasher hash = Hashing.sipHash24().newHasher().putUnencodedChars("abcd").putLong(123).putLong(123);
现在我使用 -
将此哈希值转换为字符串String hashString = hash.hash().toString();
但是,我想要字符串的字节数组,有什么方法可以从这个字符串中获取字节数组,就像我从 byte[] hashBytes = hash.hash().asBytes();
中获取的字节数组一样我想将从这些哈希中得到的字符串转换为字节数组。
实际上我意识到字节数组只使用了 8 个字节的 space 用于 siphash,而字符串的长度是 18 个字节。所以,我想将哈希存储为字节数组会更加优化。
这是从字符串中获取字节数组的代码 -
public static byte[] getBytes(String hashString) {
final byte[] bytes = new byte[8];
HashMap<Character, String> bin = new HashMap<>();
bin.put('0', "0000");
bin.put('1', "0001");
bin.put('2', "0010");
bin.put('3', "0011");
bin.put('4', "0100");
bin.put('5', "0101");
bin.put('6', "0110");
bin.put('7', "0111");
bin.put('8', "1000");
bin.put('9', "1001");
bin.put('a', "1010");
bin.put('b', "1011");
bin.put('c', "1100");
bin.put('d', "1101");
bin.put('e', "1110");
bin.put('f', "1111");
for (int i = 0; i < 16 && i < hashString.length(); i += 2) {
final BitSet bitset = new BitSet(8);
String byteBinary = bin.get(hashString.charAt(i)) + bin.get(hashString.charAt(i + 1));
for (int j = 0; j<8; j++) {
if (byteBinary.charAt(j) == '1')
bitset.set(7-j, true);
else
bitset.set(7-j, false);
}
bytes[i/2] = bitset.toByteArray()[0];
//System.out.println(byteBinary);
}
return bytes;
}
BaseEncoding.base16().lowerCase().decode(string)
应该将 HashCode.toString()
转换回您从 asBytes()
获得的字节数组。
您可以使用 HashCode.fromString(string)
将字符串解析回 HashCode
实例。然后您可以在 HashCode
实例上调用 .asBytes()
以取回底层 byte[]
.
所以基本上你想要:
byte[] bytes = HashCode.fromString(string).asBytes();