如何将短语和单词转换为 MD5 哈希值?

发布于 2024-08-27 19:31:46 字数 551 浏览 13 评论 0原文

谁能帮我解释一下如何将“我想买一些牛奶”这样的短语转换成MD5?我读过关于 MD5 的维基百科文章,但其中给出的解释超出了我的理解范围:

“MD5处理变长 消息转换成固定长度的输出 128 位。输入消息已损坏 分成 512 位块 (十六个 32 位小端字节序 整数)”

“十六个 32 位小端整数”对我来说已经很难了。我查了有关小端的 Wiki 文章,但有点不明白。

但是,该 Wiki 文章中的一些短语及其 MD5 哈希值的示例是很不错:

MD5(“敏捷的棕色狐狸跳过 懒狗”)= 9e107d9d372bb6826bd81d3542a419d6

MD5(“敏捷的棕色狐狸跳过 这只懒狗。”) = e4d909c290d0fb1ca068ffaddf22cbd0

谁能用一些非常简单的例子向我解释一下这个 MD5 算法是如何工作的?

而且,也许您知道一些可以将短语转换为 MD5 的软件或代码。如果是,请告诉我。

Can anyone, please, explain to me how to transform a phrase like "I want to buy some milk" into MD5? I read Wikipedia article on MD5, but the explanation given there is beyond my comprehension:

"MD5 processes a variable-length
message into a fixed-length output of
128 bits. The input message is broken
up into chunks of 512-bit blocks
(sixteen 32-bit little endian
integers)"

"sixteen 32-bit little endian integers" is already hard for me. I checked the Wiki article on little endians and didn't understand a bit.

However, the examples of some phrases and their MD5 hashes in that Wiki article are very nice:

MD5("The quick brown fox jumps over
the lazy dog") =
9e107d9d372bb6826bd81d3542a419d6

MD5("The quick brown fox jumps over
the lazy dog.") =
e4d909c290d0fb1ca068ffaddf22cbd0

Can anyone, please, explain to me how this MD5 algorithm works using some very simple example?

And also, perhaps you know some software or a code that would transform phrases into their MD5. If yes, please, let me know.

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

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

发布评论

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

评论(3

2024-09-03 19:31:46

忘记字节序:它只是一种信息编码方式的名称。

让我们关注维基百科 MD5 文章。您从输入消息开始。它可以是任意长:通常会创建 2GB ISO 文件的 MD5 哈希值,就像十几个字符长的字符串(例如密码)的哈希值一样。

哈希值将包含在寄存器 abcd 中。这些寄存器使用特殊值 (h0-h3) 进行初始化。

该算法将输入分成 16 个 4 字节块(“16 个 32 位小端字”)并应用特定的逻辑运算(函数 FGHI) 部分输入和寄存器的当前状态 abc< /code> 和 d。对于每组 16 个 4 字节块,它执行此操作 64 次。

当所有块都处理完毕后,abcd中剩下的就是最终的哈希值,您可以通过调用 md5sum testfile.txt 获得该文件。

更新:

如果您只想计算哈希值,那么自己实现它是没有意义的,因为它已经针对可能每种重要语言进行了完成和测试:

Python

import md5
md5.new("Nobody inspects the spammish repetition").digest()

SQL (MySQL):

SELECT MD5('Nobody inspects the spammish repetition')

Java:

String s="Nobody inspects the spammish repetition";
MessageDigest m=MessageDigest.getInstance("MD5");
m.update(s.getBytes(),0,s.length());
System.out.println(new BigInteger(1,m.digest()).toString(16));

等。

Forget about the endians: it's just a way name for a way to encode information.

Let's follow the wikipedia MD5 article. You start with an input message. It can be arbitrarily long: MD5 hashes for 2GB ISO files are routinely created, just like hashes for strings a dozen characters long (e.g. for passwords).

The hash will be contained in registers a , b, c and d. These registers are initialized with special values (h0-h3).

The algorithm breaks the input into 16 4-byte chunks ("sixteen 32-bit little-endian words") and applies specific logical operations (functions F, G, H and I) on parts of the input and the current state of registers a , b, c and d. It does this 64 times for each set of 16 4-byte chunks.

When all of the chunks are processed, what remains in a , b, c and d is the final hash, the one you might get by invoking md5sum testfile.txt.

Update:

If you just want to be able to calculate a hash, implementing it yourself makes no sense because it's been done and tested for probably every significant language out there:

Python:

import md5
md5.new("Nobody inspects the spammish repetition").digest()

SQL (MySQL):

SELECT MD5('Nobody inspects the spammish repetition')

Java:

String s="Nobody inspects the spammish repetition";
MessageDigest m=MessageDigest.getInstance("MD5");
m.update(s.getBytes(),0,s.length());
System.out.println(new BigInteger(1,m.digest()).toString(16));

etc.

旧情别恋 2024-09-03 19:31:46

Md5 是一种哈希算法:它生成输入文本的签名,因此更改输入中的任何字母都会对签名产生重大且不可预测的影响。

例如:

文本“这是一个相当短的文本,看起来很正常”的 md5 签名是“2bb1a5a5204aba95c886b3eb598c9d41”

相同文本的 md5 签名加上句点,“这是一个相当短的文本,看起来很正常”。是 '870df12558aae47b40bf738290ba8554'

如您所见,签名有很大不同。此属性使 md5 适合作为一种“指纹识别”:两本书仅相差一个字母,其 md5 完全不同。此外,对于任何一对不同的书来说,两个 md5 几乎都不会相同:冲突是极其罕见的。

md5 有多种实现,包括多个在线版本(这里是一个)。如果您想要某种特定语言的版本,请指定哪种语言。

Md5 is a hash algorithm: It produces a signature of the input text such that changing any letter in the input will have significant, unpredictable impact on the signature.

For instance:

The md5 signature of the text 'This is a quite short text which looks quite normal' is '2bb1a5a5204aba95c886b3eb598c9d41'

The md5 signature of the same text with an added period, 'This is a quite short text which looks quite normal.' is '870df12558aae47b40bf738290ba8554'

As you see, there signature differs significantly. This property makes md5 suitable as a type of 'fingerprinting': Two books who only differ by one letter have completely different md5s. Futhermore, two md5s are almost never the same for any pair of different books: collisions are extremely rare.

There are numerous implementations of md5, including several online versions (here is one). If you want one in a specific language, please specify which.

奶茶白久 2024-09-03 19:31:46

MD5 已经严重损坏,并且已经存在多年了。如果可以的话,请勿用于任何目的。在新应用程序中,使用 SHA-2 哈希函数,例如 SHA-256。

MD5 is horribly broken and has been for years. Do not use for any purpose if you can possibly help it. In new applications, use a SHA-2 hash function such as SHA-256.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文