Java 编码 SHA-1 字节数组
我有一个 SHA-1 字节数组,我想在 GET 请求中使用它。 我需要对此进行编码。 URLEncoder
需要一个字符串,如果我创建一个字符串然后对其进行编码,它会损坏吗?
澄清一下,这有点像我的另一个问题的后续。 (Bitorrent Tracker 请求)我可以获取十六进制字符串形式的值,但这不能被追踪器。 另一方面,编码的答案标记提供返回 200 OK。
所以我需要将我得到的十六进制表示形式转换
9a81333c1b16e4a83c10f3052c1590aadf5e2e20
为编码形式
%9A%813%3C%1B%16%E4%A8%3C%10%F3%05%2C%15%90%AA%DF%5E.%20
I have an SHA-1 byte array that I would like to use in a GET request. I need to encode this. URLEncoder
expects a string, and if I create a string of it and then encode it, it gets corrupt?
To clarify, this is kinda a follow up to another question of mine.
(Bitorrent Tracker Request) I can get the value as a hex string, but that is not recognized by the tracker. On the other hand, encoded answer mark provided return 200 OK.
So I need to convert the hex representation that I got:
9a81333c1b16e4a83c10f3052c1590aadf5e2e20
into encoded form
%9A%813%3C%1B%16%E4%A8%3C%10%F3%05%2C%15%90%AA%DF%5E.%20
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是在我回复时编辑的,以下是附加代码,应该可以使用(使用我的十六进制转换代码):
您需要将原始字节转换为十六进制字符或任何 URL 友好的编码使用。 Base32 或 Base64 编码都是可能的,但直接十六进制字符是最常见的。 此字符串不需要 URLEncoder,因为它不应包含任何需要 URL 编码为 %NN 格式的字符。
下面的代码会将哈希(SHA-1、MD5SUM 等)的字节转换为十六进制字符串:
此代码相当优化且速度很快,我将其用于我自己的 SHA-1 字节编码。 请注意,您可能需要使用 String.toLowerCase() 方法将大写转换为小写,具体取决于服务器接受的形式。
Question was edited while I was responding, following is ADDITIONAL code and should work (with my hex conversion code):
You'll need to convert the raw bytes to hexadecimal characters or whatever URL-friendly encoding they are using. Base32 or Base64 encodings are possible, but straight hexadecimal characters is the most common. URLEncoder is not needed for this string, because it shouldn't contain any characters that would require URL Encoding to %NN format.
The below will convert bytes for a hash (SHA-1, MD5SUM, etc) to a hexadecimal string:
This code is fairly optimized and fast, and I am using it for my own SHA-1 byte encoding. Note that you may need to convert uppercase to lowercase with the String.toLowerCase() method, depending on which form the server accepts.
这取决于您请求的接收者的期望。
我想它可能是散列中字节的十六进制表示。 字符串可能不是最好的主意,因为哈希数组很可能包含不可打印的字符值。
我将迭代数组并使用 Integer.toHexValue() 将字节转换为十六进制。
This depends on what the recipient of your request expects.
I would imagine it could be a hexadecimal representation of the bytes in your hash. A string would probably not be the best idea, because the hash array will most likely contain non-printable character values.
I'd iterate over the array and use Integer.toHexValue() to convert the bytes to hex.
SHA1 是十六进制格式 [0-9a-f],不需要对其进行 URLEncode。
SHA1 is in hex format [0-9a-f], there should be no need to URLEncode it.
使用 Apache Commons-Codec 满足您的所有编码/解码需求(ASN.1 除外,这是一个令人痛苦的事情)
Use Apache Commons-Codec for all your encoding/decoding needs (except ASN.1, which is a pain in the ass)