How to mimic computeHash vb function in PHP

吃可爱长大的小学妹 提交于 2019-12-07 14:52:26

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

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
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!