如何 return 从 sha1 字节数组生成的字符串到该字节数组?
How to return a string generated from a sha1 bytearray to that bytearray?
首先抱歉英语不好。
好吧,我想从 torrent 文件中读取碎片哈希信息。目前,我正在使用 https://github.com/hyPiRion/java-bencode 这个 bencode 库来解码信息,但我的问题是当我想将字符串片段转换为字节数组时。
torrent 文件以 UTF-8 编码。但如果我这样做
Byte[] bytepieces = piecestring.getBytes("UTF-8");
效果很好。任何真正有用的东西。
另一方面,为了比较或尝试获取字符串,而不是获取字节,我已经读取了文件的第一部分,并计算了 sha1。在获得 sha1 的 20 大小字节数组后,如果我将它转换为字符串,实际上,字符串匹配大字符串的第一部分......但是,如果我尝试 return 生成的字符串,到创建它的 20 个原始字节...我不能...该怎么做?
小例子:
FileInputStream fin = new FileInputStream("miFile");
byte[] array = new Byte[512*1024]; //a piece of 512 kb
fin.read(array,0,512*1024);
MessageDigest md = MessageDigest.getInstanse ("SHA);
Byte [ sha1byte = md.digest(array);
String s = new String(sha1byte,"UTF-8");
执行此操作后,sha1byte.length 为 20,没问题,sha1 散列的正确大小。但如果我这样做
s.getBytes("UTF-8").length, in the case of my example i got... ¡33! ¡wuuut!
我想从生成的字符串中再次获取我的 20 个数组。我怎样才能得到这个?
好的谢谢 :P
I'm storing binary data as strings, because the BEncode format in .torrent files, store that binary data as string
Bencode "strings" 是字节序列,而不是 unicode 代码点序列。因此,一种语言的字节表示 - byte[]
或 ByteBuffer
in java - 是合适的,并且在某些情况下应该只解释为 utf8 字符串,当它们实际上包含应该是人类的东西时 -可读。
因此您应该使用支持提取原始字节的编码库。
谢谢你们的回答,但我可以使用这个 https://github.com/bedeho/bencodej
找到解决方案
该库始终将 Bencode 数据加载为带有自定义 类 的 bytearray,并且能够具有带有字节串的 1:1 :p
谢谢大家。
首先抱歉英语不好。
好吧,我想从 torrent 文件中读取碎片哈希信息。目前,我正在使用 https://github.com/hyPiRion/java-bencode 这个 bencode 库来解码信息,但我的问题是当我想将字符串片段转换为字节数组时。 torrent 文件以 UTF-8 编码。但如果我这样做
Byte[] bytepieces = piecestring.getBytes("UTF-8");
效果很好。任何真正有用的东西。
另一方面,为了比较或尝试获取字符串,而不是获取字节,我已经读取了文件的第一部分,并计算了 sha1。在获得 sha1 的 20 大小字节数组后,如果我将它转换为字符串,实际上,字符串匹配大字符串的第一部分......但是,如果我尝试 return 生成的字符串,到创建它的 20 个原始字节...我不能...该怎么做?
小例子:
FileInputStream fin = new FileInputStream("miFile");
byte[] array = new Byte[512*1024]; //a piece of 512 kb
fin.read(array,0,512*1024);
MessageDigest md = MessageDigest.getInstanse ("SHA);
Byte [ sha1byte = md.digest(array);
String s = new String(sha1byte,"UTF-8");
执行此操作后,sha1byte.length 为 20,没问题,sha1 散列的正确大小。但如果我这样做
s.getBytes("UTF-8").length, in the case of my example i got... ¡33! ¡wuuut!
我想从生成的字符串中再次获取我的 20 个数组。我怎样才能得到这个?
好的谢谢 :P
I'm storing binary data as strings, because the BEncode format in .torrent files, store that binary data as string
Bencode "strings" 是字节序列,而不是 unicode 代码点序列。因此,一种语言的字节表示 - byte[]
或 ByteBuffer
in java - 是合适的,并且在某些情况下应该只解释为 utf8 字符串,当它们实际上包含应该是人类的东西时 -可读。
因此您应该使用支持提取原始字节的编码库。
谢谢你们的回答,但我可以使用这个 https://github.com/bedeho/bencodej
找到解决方案该库始终将 Bencode 数据加载为带有自定义 类 的 bytearray,并且能够具有带有字节串的 1:1 :p 谢谢大家。