为什么我的base64编码的SHA-1哈希包含56个字符? [英] Why does my base64 encoded SHA-1 hash contain 56 chars?

查看:199
本文介绍了为什么我的base64编码的SHA-1哈希包含56个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许是一个完全愚蠢的问题,但我无法解决...

Maybe a completely stupid question but I just cannot work it out...

首先,我需要使用提交标记的一部分来生成SHA-1哈希.哈希正确,输出为;

First I need to generate an SHA-1 hash using part of my submission markup. The hash is correct and the output is;

0623f7917a1e2e09e7bcc700482392fba620e6a2

接下来,我需要将此散列的base64编码为28个字符的字符串.这是我在努力的地方,因为当我运行代码(或使用在线生成器)时,我得到了56个字符的字符串.我得到的刺痛是;

Next I need to base64 encode this hash to a 28 character sting. This is where I am struggling as when I run my code (or use the online generators) I get a 56 character sting. The sting I get is;

MDYyM2Y3OTE3YTFlMmUwOWU3YmNjNzAwNDgyMzkyZmJhNjIwZTZhMg ==

问题是1)是否可以从上面的哈希中获取28个字符的字符串?和2)怎么...我要去哪里错了.

Question is 1) Is it possible to get a 28 char string from the hash above? and 2) how... where could I be going wrong.

感谢您提供的任何帮助.

Thank you for any help provided.

推荐答案

SHA-1哈希长度为20个字节,但是这些字节不太可能全部都是可打印字符. 因此,如果要向人类显示这20个字节,则必须将它们编码为可打印的字符.

A SHA-1 hash is 20 bytes long, but those bytes are unlikely to all be printable characters. Hence if we want to display those 20 bytes to a human we have to encode them in printable characters.

一种执行此操作的方法是十六进制,其中我们将每个字节都切成小段表示每个半部(4位值,数字0-15),其字符在0123456789abcdef范围内. 因此,每个字节被编码为2个十六进制值,因此我们的20字节哈希值被编码为40字节可打印字符.

One way to do this is hexadecimal, where we take each byte, chop it in half and represent each half (a 4-bit value, numerically 0-15) with characters in the range 0123456789abcdef. Thus each byte is encoded into 2 hex values, so our 20-byte hash value is encoded in 40 bytes of printable characters.

十六进制很容易计算,人类很容易查看编码并弄清楚字节的实际样子,但这并不是最有效的,因为我们只使用了95个ASCII可打印字符中的16个.

Hex is simple to calculate and it's easy for a human to look at an encoding and work out what the bytes actually look like, but it's not the most efficient as we're only using 16 out of the 95 ASCII printable characters.

将任意二进制数据编码为可打印字符的另一种方法是 Base 64 .这种方法效率更高,可以平均编码4个base64值中的3个字节,但是对于人类来说,解析该编码要困难得多.

Another way to encode arbitrary binary data into printable characters is Base 64. This is more efficient, encoding (on average) 3 bytes in 4 base64 values, but it's a lot harder for a human to parse the encoding.

您看到的行为是由于将20字节的哈希值编码为40字节的十六进制,然后将那些的40字节的十六进制编码为56字节(40 / 3 * 4,然后四舍五入到最接近的4个字节)的base64数据.

The behaviour you are seeing is due to encoding a 20-byte hash value into 40 bytes of hex, and then encoding those 40 bytes of hex into 56 bytes (40 / 3 * 4, then rounded up to the nearest 4 bytes) of base64 data.

您需要直接从原始哈希字节编码为base64(如果可用),或者在将十六进制值解码为字节后再编码为base64.

You need to either encode directly to base64 from the raw hash bytes (if available), or decode the hexadecimal value to bytes before encoding to base64.

这篇关于为什么我的base64编码的SHA-1哈希包含56个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆