简体   繁体   English

二进制文件的 php hash 如何工作

[英]how the php hash of binary works

looking at PHP tutorials and have this看 PHP 教程并拥有这个

hash ( string $algo , string $data , bool $binary = false )

i use other language and do hash and get the results, if i do not use hash bool as binary = true, if the hash binary = true, the results does not match.我使用其他语言并执行 hash 并获得结果,如果我不使用 hash 布尔作为二进制 = true,如果 hash 二进制 = true,则结果不匹配。 Here is my try for what I am doing example here这是我在这里做的例子的尝试

PHP Version PHP版本

var_dump(hash('sha256', '1612466079678052nonce=1612466079678052', false)); 

CF Version CF版

lcase(hash('1612466079678052nonce=1612466079678052', "SHA-256", "UTF-8"));

produces this result Results of PHP & CF产生这个结果PHP & CF 的结果

c0363f560d8df85b1f24d3f88e6f32a78370f55b0501a5b0c6d18d0009de2460 

now i will be setting the boolean value of hash of PHP to true现在我将 PHP 的 hash 的 boolean 值设置为 true

PHP Version: PHP 版本:

var_dump(hash('sha256', '1612466079678052nonce=1612466079678052', true)); 

Results of PHP PHP 的结果

�6?V ��[$���o2��p�[���э �$` 

Now Coldfusion现Coldfusion

dump(toString(binaryDecode(lcase(hash('1612466079678052nonce=1612466079678052', "SHA-256", "UTF-8")),"base64"))); 

Results of CF CF的结果

sM���z���[���ww����f��~��[ӝ5k��s�u��4��^ێ�

Can anyone spot the issue here what i am doing wrong to get the exact results as like PHP is doing谁能在这里发现我做错了什么以获得确切的结果,就像 PHP 正在做的那样

First: You are trying to output raw BINARY values as STRINGS ( printable characters ).首先:您正在尝试将 output 原始二进制值作为字符串(可打印字符)。 If you you want to output raw BINARY data as STRING I suggest to use a proper encoding to do that (in both languages).如果您想将 output 原始二进制数据作为字符串,我建议使用适当的编码来做到这一点(两种语言)。 Just printing/echoing/outputting raw BINARY data will always result with many unmapped characters or unidentified UTF-8 character '�'.仅打印/回显/输出原始 BINARY 数据总是会导致许多未映射的字符或无法识别的 UTF-8 字符“�”。 Thus the data is not a readable printable character.因此,数据不是可读的可打印字符。

Second: Using the functions in PHP:二:使用PHP中的函数:

hash('sha256', '1612466079678052nonce=1612466079678052', false); 

and in CFML:在 CFML 中:

hash('1612466079678052nonce=1612466079678052', "SHA-256");

are the same because both output/echo a STRING representation of the binary HASH as HEX (hexadecimal data):是相同的,因为都将二进制 HASH 的字符串表示形式输出/回显为 HEX(十六进制数据):

c0363f560d8df85b1f24d3f88e6f32a78370f55b0501a5b0c6d18d0009de2460

The raw BINARY format of that same data would be (here that data is a STRING represenstation of 0s and 1s):相同数据的原始 BINARY 格式将是(此处数据是 0 和 1 的 STRING 表示):

1100000000110110001111110101011000001101100011011111100001011011000111110010010011010011111110001000111001101111001100101010011110000011011100001111010101011011000001010000000110100101101100001100011011010001100011010000000000001001110111100010010001100000  

If you set the 'set as binary' flag of the PHP hash function to 'true', you will output the above mentioned binary data plain to the output stream, while in CFML you are trying to "base64" decode the HEX string to obtain the raw binary data. If you set the 'set as binary' flag of the PHP hash function to 'true', you will output the above mentioned binary data plain to the output stream, while in CFML you are trying to "base64" decode the HEX string to obtain原始二进制数据。 That assumption is wrong.这种假设是错误的。 What you need to do is to convert the HASH to binary decode from HEX to binary first with:您需要做的是首先将 HASH 转换为从十六进制到二进制的二进制解码:

binaryDecode(hash("1612466079678052nonce=1612466079678052", "SHA-256"),"hex");

If you use cfdump of Adobe's or Lucee's CFML to output the HASH as binary:如果您使用cfdump的 cfdump 或 Lucee 的 CFML 到 output HASH 作为二进制:

<cfscript>
    dump(binaryDecode(hash("1612466079678052nonce=1612466079678052", "SHA-256"),"hex"));
</cfscript>

, the engines will try to convert the raw binary data as STRING representation on it's own assumption. ,引擎将尝试根据自己的假设将原始二进制数据转换为 STRING 表示形式。 In Lucee you will see a base64 representation of the same binary data.在 Lucee 中,您将看到相同二进制数据的 base64 表示。

在此处输入图像描述

Because with your PHP var_dump function you are outputting the same raw binary data directly to the output stream, you need just to writeoutput that binary data in CFML with: Because with your PHP var_dump function you are outputting the same raw binary data directly to the output stream, you need just to writeoutput that binary data in CFML with:

<cfscript>
writeoutput(binaryDecode(hash("1612466079678052nonce=1612466079678052", "SHA-256"),"hex"));
</cfscript>

What will have the same result:什么会产生相同的结果:

�6?V ��[$���o2��p�[���э �$`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM