如何在 PHP 中使用 md5() 的 raw_output 返回值?
我对 PHP 缺乏经验,所以这对你们中的一些人来说可能是显而易见的,但如果我在 PHP 中调用 md5($mystring,true) ,它会返回“长度为 16 的原始二进制格式”。那么那是什么?它是一个数组吗?什么的数组?如何读取该返回值的各个位和字节?
我在网上找到的所有示例都没有在不直接进入 base64_encode() 或其他内容的情况下使用它。例如,我只想能够检查第五位或第三个字节。
I'm pretty inexperienced in PHP so this may be obvious to some of you, but if I call md5($mystring,true) in PHP it says it returns a "raw binary format with a length of 16". So what is that? Is it an array? An array of what? How do I read the individual bits and bytes of that return value?
None of the examples I can find online use it without going straight into base64_encode() or something. I just want to be able to check the fifth bit, or the third byte, for example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“原始二进制格式”是指字符串(因为字符串在 PHP 中是二进制安全的):
如果您想从中读取一个字节,请使用
$string[5]
偏移语法。或者从第五个字节中提取单个位,例如:"Raw binary format" means a string (as strings are binary-safe in PHP):
If you want to read a byte out of that, use the
$string[5]
offset syntax for that. Or to extract a single bit out of the fifth byte for example:它将它作为“字符串”返回,每个字符都是输出的一个字节。您可能会考虑使用不带第二个参数的 md5() 的十六进制输出,并使用以 16 作为基本参数的 intval() 将其子字符串转换为数字。就像这样:
It returns it as a "string" with each Character being a byte of the output. What you might consider instead is using the hex output of md5() without the second parameter and converting substrings of it to numbers using intval() with 16 as the base parameter. Like so:
MD5 散列是一个 128 位数字,例如 16 字节的原始二进制数据。为了便于阅读,PHP 的 md5() 函数默认将其输出为人类可读的字符串,其中这 16 个二进制字节被转换为 32 个字符的字母数字字符串。
当您为 MD5 指定第二个
true
参数时,您是在告诉 PHP 返回原始 128 位数字,而不是人类可读的版本。An MD5 hash is a 128bit number, e.g. 16 bytes of raw binary data. For legibility, PHP's md5() function defaults to outputting this as a human readable string, where those 16 binary bytes get converted into a 32 character alpha-numeric string.
When you specify that second
true
parameter for MD5, you're telling PHP to return that raw 128bit number instead of the human readable version.MD5 的原始输出是哈希值的纯二进制字符串。您无法将其打印到屏幕上,因为它不是编码为实际的 ASCII 字符,而是二进制数字。仅当您需要以二进制格式存储或传输哈希时,这才有用。
The RAW output of MD5 is the plain binary string of the hash. You cannot print it to the screen since it's not encoded as actual ASCII characters but binary numbers. This is only useful if you need to store or transfer the hash in a binary format.
为了获取各个位:
所以我们逐字节进行,由于它是字符串形式,我们需要通过
ord()
将其转换为 ASCII 数字。现在,逐位检查字节,查看哪些已打开,哪些已关闭,并连接到位串。转到下一个字节并冲洗并重复,直到读取完所有 128 位。读取第三个字节中的第三位(从左起):
如果该位打开则返回 1,否则返回 0
To get the individual bits:
So we go byte by byte, and since it is in string form, we need to convert it to ASCII number by
ord()
. Now go through the byte, bit by bit and see which are turned on and which are turned off and concatenate to the bit string. Go to next byte and rinse and repeat until all 128 bits are read.Read third bit ( from the left ) in third byte:
returns 1 if the bit is turned on, 0 otherwise