如何在PHP中模拟computeHash vb函数

如何在PHP中模拟computeHash vb函数,php,vb.net,encryption,md5,Php,Vb.net,Encryption,Md5,我有一个地狱的时间试图md5哈希与PHP这个。。。我尝试移植到PHP的VB代码使用ComputeHash,它接受字节[],并对整个数组执行哈希 Public Shared Function HashBytesMD5(ByVal strInput As String) As Guid Dim oHasher As Cryptography.MD5 = Cryptography.MD5.Create() Dim oEncoder As New System.T

我有一个地狱的时间试图md5哈希与PHP这个。。。我尝试移植到PHP的VB代码使用ComputeHash,它接受字节[],并对整个数组执行哈希

    Public Shared Function HashBytesMD5(ByVal strInput As String) As Guid
        Dim oHasher As Cryptography.MD5 = Cryptography.MD5.Create()
        Dim oEncoder As New System.Text.UTF8Encoding()
        Dim csData() As Byte

    csData = oEncoder.GetBytes(strInput)
    csData = oHasher.ComputeHash(oEncoder.GetBytes(strInput))
        Return New Guid(csData)
    End Function
[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
现在,我有以下内容,它创建了一个ascii值数组。现在我需要像VB.Net一样md5它。这似乎不像看上去那么直截了当

  $passHash = $this->ConvertToASCII('123456');
  $passHash = md5(serialize($passHash));


     /*
     * Converts a string to ascii (byte) array
     */
    function ConvertToASCII($password)
    {
        $byteArray = array();

        for ($i=0; $i < strlen($password); $i++)  {
            array_push($byteArray,ord(substr($password,$i)));
        }

        return $byteArray;
    }
[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
从VB computeHash函数返回的字节数组 索引

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62

我的VB.NET非常生锈,但它看起来像是
MD5.ComputeHash()
的输出可以通过运行
MD5()
输入,然后获取每对十六进制字符(字节)并转换为十进制来重新创建

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
$passHash = md5('123456');
$strlen = strlen($passHash) ;

$hashedBytes = array() ;
$i = 0 ;
while ($i < $strlen) {
    $pair = substr($passHash, $i, 2) ;
    $hashedBytes[] = hexdec($pair) ;
    $i = $i + 2 ;
}
$passHash=md5('123456');
$strlen=strlen($passHash);
$hashedBytes=array();
$i=0;
而($i<$strlen){
$pair=substr($passHash,$i,2);
$hashedBytes[]=hexdec($pair);
$i=$i+2;
}

我的VB.NET非常生锈,但它看起来像是
MD5.ComputeHash()
的输出可以通过运行
MD5()
输入,然后获取每对十六进制字符(字节)并转换为十进制来重新创建

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
$passHash = md5('123456');
$strlen = strlen($passHash) ;

$hashedBytes = array() ;
$i = 0 ;
while ($i < $strlen) {
    $pair = substr($passHash, $i, 2) ;
    $hashedBytes[] = hexdec($pair) ;
    $i = $i + 2 ;
}
$passHash=md5('123456');
$strlen=strlen($passHash);
$hashedBytes=array();
$i=0;
而($i<$strlen){
$pair=substr($passHash,$i,2);
$hashedBytes[]=hexdec($pair);
$i=$i+2;
}

借助魔法的力量,以下功能将发挥作用:

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
function get_VB_hash($text)
{
    $hash = md5($text);
    $hex = pack('H*', $hash);  // Pack as a hex string
    $int_arr = unpack('C*', $hex);  // Unpack as unsigned chars

    return $int_arr;
}
或作为一行:

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
unpack('C*',pack('H*',md5($text))

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
证明:

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
C:\>php -r "print_r( unpack('C*', pack('H*', md5('123456') )) );"
Array
(
    [1] => 225
    [2] => 10
    [3] => 220
    [4] => 57
    [5] => 73
    [6] => 186
    [7] => 89
    [8] => 171
    [9] => 190
    [10] => 86
    [11] => 224
    [12] => 87
    [13] => 242
    [14] => 15
    [15] => 136
    [16] => 62
)

借助魔法的力量,以下功能将发挥作用:

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
function get_VB_hash($text)
{
    $hash = md5($text);
    $hex = pack('H*', $hash);  // Pack as a hex string
    $int_arr = unpack('C*', $hex);  // Unpack as unsigned chars

    return $int_arr;
}
或作为一行:

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
unpack('C*',pack('H*',md5($text))

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
证明:

[0]           225                                     
[1]           10                    
[2]           220                
[3]           57                           
[4]           73                          
[5]           186                     
[6]           89      
[7]           171
[8]           190
[9]           86
[10]          224
[11]          87
[12]          242
[13]          15
[14]          136
[15]          62
C:\>php -r "print_r( unpack('C*', pack('H*', md5('123456') )) );"
Array
(
    [1] => 225
    [2] => 10
    [3] => 220
    [4] => 57
    [5] => 73
    [6] => 186
    [7] => 89
    [8] => 171
    [9] => 190
    [10] => 86
    [11] => 224
    [12] => 87
    [13] => 242
    [14] => 15
    [15] => 136
    [16] => 62
)

乍一看,我不确定这是否可以通过md5'序列化字节数组来实现。旁注:您使用
substr()
的方式需要从
$i
开始的所有字符串,而我认为您只需要一个字符:
substr($password,$i,1)
我从php手册中的注释中获得了php函数。我做了一个打印,看起来很有效。nvm你是对的:)捕捉得不错。是的,它之所以有效是因为
ord()
只关心第一个给定的字符:)我想我得到了答案,发布答案是因为itOn first look中有代码。我不确定这是否可以通过md5'序列化字节数组来完成。旁注:您使用
substr()
的方式需要从
$i
开始的所有字符串,而我认为您只需要一个字符:
substr($password,$i,1)
我从php手册中的注释中获得了php函数。我做了一个打印,看起来很有效。nvm你是对的:)捕捉得很好。是的,它之所以有效是因为
ord()
只关心第一个给定的字符:)我想我得到了,发布答案是因为里面有代码哇,干得好!我不认为我会很快得到它:)你知道如何将它转换成.net样式的GUID吗?哇,干得好!我认为我不会很快得到它:)你知道如何将它转换成.net样式的GUID吗?更好,性能更高。使用此选项并避免我所做的仅显示ComputeHash()功能的循环。更好,性能更高。使用此选项并避免我所做的仅显示ComputeHash()功能的循环。