MD5 哈希和 Base64 编码

发布于 2024-10-04 13:47:25 字数 66 浏览 7 评论 0原文

如果我有一个 32 个字符的字符串(MD5 哈希)并使用 Base64 对其进行编码,那么编码字符串的最大长度是多少?

If I have a 32 character string (an MD5 hash) and I encode it using Base64, what's the maximun length of the encoded string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒尘 2024-10-11 13:47:25

MD5 值以 Base64 表示法始终为 22 个(有用)字符长。许多 Base64 算法在对 MD5 哈希进行编码时还会附加 2 个字符的填充,使总数达到 24 个字符。填充不会添加任何有用的信息,可以丢弃。只有前 22 个字符很重要。

原因如下:

MD5 哈希是一个 128 位值。 Base64字符串中的每个字符包含6位信息,因为该字符有64种可能的值,并且需要2的6次方才能达到64。每个字符包含6位信息,21个字符有126位信息, 22个字符包含132位信息。由于 128 位无法容纳 21 个字符,但可以容纳 22 个字符(还有一点剩余空间),因此 128 位值在 Base64 中将始终表示为 22 个字符。

关于填充的注释:

我上面提到,许多 Base64 编码算法在编码 MD5 值时会添加几个填充字符。这是因为 Base64 将 3 个字节的信息表示为 4 个字符。由于 MD5 有 16 个字节的信息,许多 Base64 编码算法会附加“==”来表示 16 个字节的输入比下一个 3 的倍数少 2 个字节,即 18 个字节。这两个等号不会向字符串添加任何信息,并且在存储时可以将其丢弃。

An MD5 value is always 22 (useful) characters long in Base64 notation. Many Base64 algorithms will also append 2 characters of padding when encoding an MD5 hash, bringing the total to 24 characters. The padding adds no useful information and can be discarded. Only the first 22 characters matter.

Here's why:

An MD5 hash is a 128-bit value. Every character in a Base64 string contains 6 bits of information, because there are 64 possible values for the character, and it takes 6 powers of 2 to reach 64. With 6 bits of information in every character, 21 characters has 126 bits of information, and 22 characters contains 132 bits of information. Since 128 bits cannot fit within 21 characters but does fit within 22 characters (with a little room to spare), a 128-bit value will always be represented as 22 characters in Base64.

A note on the padding:

I mentioned above that many Base64 encoding algorithms add a couple of characters of padding when encoding an MD5 value. This is because Base64 represents 3 bytes of information as 4 characters. Since MD5 has 16 bytes of information, many Base64 encoding algorithms append "==" to designate that the input of 16 bytes was 2 bytes short of the next multiple of 3, which would have been 18 bytes. These 2 equal signs add no information whatsoever to the string, and can be discarded when storing.

離人涙 2024-10-11 13:47:25

根据http://en.wikipedia.org/wiki/Base64

“请注意,给定输入n 个字节,输出长度将为 (n + 2 - ((n + 2) % 3)) / 3 * 4 字节长,对于大 n,其收敛到 n * 4 / 3 或 1.33333n。”

因此,它将是 ((32 + 2 - (32 + 2) % 3)) / 3 * 4 = 34 - (34 % 3) / 3 * 4 = (34 - 1) / 3 * 4 = 33/3 *4 = 44 个字符。

您始终可以以原始二进制形式(128 位)提取它,并将其直接编码为 Base 64,这意味着转换 16 字节而不是 32,当 Base 64 编码时,32 字节变为 24 字节。

As per http://en.wikipedia.org/wiki/Base64

"Note that given an input of n bytes, the output will be (n + 2 - ((n + 2) % 3)) / 3 * 4 bytes long, which converges to n * 4 / 3 or 1.33333n for large n."

So, it will be ((32 + 2 - (32 + 2) % 3)) / 3 * 4 = 34 - (34 % 3) / 3 * 4 = (34 - 1) / 3 * 4 = 33/3*4 = 44 characters.

You could always extract it in raw binary form (128 bits) and encode it directly into base 64, which means converting 16 bytes instead of 32, which becomes 24 bytes when base 64 encoded.

庆幸我还是我 2024-10-11 13:47:25

MD5 128 位在 Base64 中表示为 22 个字符。在这种情况下还有 2 个填充字符“=”。

如何?

$ md5sum ./README.md 
c6b5f48774aa0a87a82a276ff86be507  ./README.md
$ md5sum ./README.md | base64
YzZiNWY0ODc3NGFhMGE4N2E4MmEyNzZmZjg2YmU1MDcgIC4vUkVBRE1FLm1kCg==

在这种情况下,Base64编码的字符串不会短于MD5哈希长度

,因为编码的是MD5哈希的存储形式。不是 MD5 哈希值本身。

需要注意的是用多少位来存储一位MD5哈希值。

正确方法:

将哈希值转换为
1 将十六进制转换为二进制

2 将二进制转换为 Base64 编码的字符串

$ cat ./README.md |  openssl dgst -md5 
c6b5f48774aa0a87a82a276ff86be507
$ cat ./README.md |  openssl dgst -md5 -binary | openssl enc -base64
xrX0h3SqCoeoKidv+GvlBw==

$ md5sum ./LICENSE 
e3fc50a88d0a364313df4b21ef20c29e  ./LICENSE
$ cat ./LICENSE |  openssl dgst -md5 -binary | openssl enc -base64
4/xQqI0KNkMT30sh7yDCng==
$ (echo 0:; echo e3fc50a88d0a364313df4b21ef20c29e) | xxd -rp -l 16|base64
4/xQqI0KNkMT30sh7yDCng==

MD5 128 bits is represented as 22 characters in Base64. also have 2 padding charater '=' in this case.

How?

$ md5sum ./README.md 
c6b5f48774aa0a87a82a276ff86be507  ./README.md
$ md5sum ./README.md | base64
YzZiNWY0ODc3NGFhMGE4N2E4MmEyNzZmZjg2YmU1MDcgIC4vUkVBRE1FLm1kCg==

In this case Base64 encoded string does not shorter than the MD5 hash length

Because what is encoded is the storage form of MD5 hash. not MD5 hash value itself.

Need to note how many bit is used to store one digit of MD5 hash.

Right way:

convert the hash value so
1 convert the hexadecimal to binary

2 convert the binary to base64 coded sting

$ cat ./README.md |  openssl dgst -md5 
c6b5f48774aa0a87a82a276ff86be507
$ cat ./README.md |  openssl dgst -md5 -binary | openssl enc -base64
xrX0h3SqCoeoKidv+GvlBw==

or

$ md5sum ./LICENSE 
e3fc50a88d0a364313df4b21ef20c29e  ./LICENSE
$ cat ./LICENSE |  openssl dgst -md5 -binary | openssl enc -base64
4/xQqI0KNkMT30sh7yDCng==
$ (echo 0:; echo e3fc50a88d0a364313df4b21ef20c29e) | xxd -rp -l 16|base64
4/xQqI0KNkMT30sh7yDCng==
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文