我已经为一个字符串和两个长值生成了SipHash (对于许多这样的字符串和long组合)。我用过-
Hasher hash = Hashing.sipHash24().newHasher().putUnencodedChars("abcd").putLong(123).putLong(123);现在我使用-
String hashString = hash.hash().toString();但是,我想要字符串的字节数组,有可能有任何方法,这样我就可以从这个字符串中获得字节数组,就像从byte[] hashBytes = hash.hash().asBytes();获得的字节数组一样,我想将我从这些散列中得到的字符串转换为字节数组。
实际上,我意识到字节数组只使用8字节的空间作为siphash,其中字符串的长度为18字节。因此,我想将散列存储为字节数组会得到更多的优化。
发布于 2016-02-17 01:27:51
BaseEncoding.base16().lowerCase().decode(string)应该将HashCode.toString()转换回从asBytes()获得的字节数组。
发布于 2016-02-18 02:50:28
可以使用HashCode将字符串解析回HashCode.fromString(string)实例。然后,您可以在.asBytes()实例上调用HashCode,以获取底层byte[]的副本。
所以基本上你想:
byte[] bytes = HashCode.fromString(string).asBytes();
发布于 2016-02-16 14:11:35
下面是从字符串获取字节数组的代码-
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;
}https://stackoverflow.com/questions/35429337
复制相似问题