如何在PHP中模拟computeHash vb函数

发布于 2024-10-29 18:54:19 字数 1879 浏览 1 评论 0原文

我在尝试使用 PHP 进行 md5 散列时遇到了很大的麻烦...我尝试移植到 PHP 的 VB 代码使用 ComputeHash,它接受一个 byte[] 并对整个数组执行散列。

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

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

之前的字节数组computeHash md5

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

VBcomputeHash函数返回的字节数组 指数 价值

[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

I am having a hell of a time trying to md5 hash this with PHP... The VB code I am trying to port to PHP uses ComputeHash which takes in a byte[] and performs a hash on the whole array.

    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

Right now I have the following which creates an array of ascii values. Now I need to md5 it like VB.Net does. It doesn't seem to be as straightforward as it may seem.

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

Note: the values in the first are the acii values for the characters 123456

Byte array before computeHash md5

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

Byte array returned from VB computeHash function
index value

[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

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

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

发布评论

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

评论(2

飘逸的'云 2024-11-05 18:54:19

我的 VB.NET 非常生疏,但似乎可以通过 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 ;
}

My VB.NET is very rusty but it seems like MD5.ComputeHash()'s output could be recreated by running your input through md5() and then taking each pair of hex characters (byte) and converting into decimal.

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

$hashedBytes = array() ;
$i = 0 ;
while ($i < $strlen) {
    $pair = substr($passHash, $i, 2) ;
    $hashedBytes[] = hexdec($pair) ;
    $i = $i + 2 ;
}
若有似无的小暗淡 2024-11-05 18:54:19

借助魔法的力量,以下内容将起作用:

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
)

By the power of magic, the following will work:

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

or as one line:

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

Proof:

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