【问题标题】:How to mimic computeHash vb function in PHP如何在 PHP 中模拟 computeHash vb 函数
【发布时间】:2011-04-04 22:05:30
【问题描述】:

我在尝试用 PHP 对它进行 md5 哈希处理时遇到了麻烦……我试图移植到 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

现在我有以下内容,它创建了一个 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;
    }

注意:第一个值是字符123456的acii值

computeHash md5之前的字节数组

  **index**     **Value**                  
      [0]          49                           
      [1]          50            
      [2]          51               
      [3]          52                  
      [4]          53                 
      [5]          54

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

【问题讨论】:

  • 乍一看,我不确定这是否可以通过 md5'ing 字节序列化数组来完成。旁注:您使用substr() 的方式采用从$i 开始的所有字符串,而我认为您只需要那个单个字符:substr($password, $i, 1)
  • 我从 php 手册中的注释中得到了 php 函数。我做了一个 print_r,它似乎工作。
  • nvm 你是对的 :) 不错。
  • 是的,因为ord() 只关心第一个给定的字符 :) 我想我明白了,发布答案是因为里面有代码

标签: php vb.net encryption md5


【解决方案1】:

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

$passHash = md5('123456');
$strlen = strlen($passHash) ;

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

【讨论】:

  • 哇,干得好!我想我不会很快得到那个:) 你有没有机会知道如何将它转换为 .net 样式的 GUID?
【解决方案2】:

通过魔法的力量,以下将起作用:

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;
}

或一行:

unpack('C*', pack('H*', md5($text)) );

证明:

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
)

【讨论】:

  • 更好,性能明智。使用它并避免我所做的仅显示 ComputeHash() 所做的循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
  • 2013-06-18
相关资源
最近更新 更多