doue2666 2018-09-05 10:08
浏览 55
已采纳

NodeJS如何散列缓冲区?

I have an existing NodeJS lib whose hashed Buffer functionality I am trying to match in PHP (specifically the "AFTER" value below), but have so far failed. What is NodeJS doing to a Buffer internally such that it can be hashed and returned as a new Buffer?

...
let concatValue = isHex(currentbranchOps[o].r) ? Buffer.from(currentbranchOps[o].r, 'hex') : Buffer.from(currentbranchOps[o].r, 'utf8')
currentHashValue = Buffer.concat([currentHashValue, concatValue])

console.log('BEFORE: ', currentHashValue); // Uint8Array(76) [110, 111, 100, 101......]
currentHashValue = crypto.createHash('sha256').update(currentHashValue).digest()
console.log('AFTER: ', currentHashValue); // Uint8Array(32) [123, 67, 203, 88......]
...

Given that only stringy types can be hashed, how is Node arriving at the "AFTER" value above in the form of another Buffer?

In PHP I have tried:

hash('sha256', pack('H*', implode('', [110, 111, 100, 101......] )))
hash('sha256', pack('c*', implode('', [110, 111, 100, 101......] )))

I also wrote a routine that gives me a digest of the array as a string, and then hashing the result (and various permutations thereof), without being able to match exactly what Node gives me in the "AFTER" block.

function buffer_digest_from(array $dec) : string
{
    $hex = '';

    foreach($dec as $int) {
        // Left pad single hex values with zeroes, to match chainpoint hashes
        $hex .= str_pad(dechex($int), 2, '0', STR_PAD_LEFT);
    }

    return $hex;
}

hash('sha256', pack('H*', buffer_digest_from([110, 111, 100, 101......] )));
hash('sha256', pack('c*', buffer_digest_from([110, 111, 100, 101......] )));
hash('sha256', buffer_digest_from([110, 111, 100, 101......] ));

I realise of course that languages vary in their implementations, but if all I wish to do is manipulate integers, then there should be some way to arrive at the same solution, regardless of implementation.

Many thanks for reading.

  • 写回答

1条回答 默认 最新

  • douci2516 2018-09-05 11:05
    关注

    I think this question might stem from a misunderstanding of what Node.js buffers are. They are really just a sequence of bytes. The default way to output them in the console and interact with them looks like an array of numbers.

    PHP also has a similar data type that stores lists of bytes, but it's interface looks less like an array. It's actually the humble string.

    PHP's string is not like Javascript's string. Javascript's string is more like a 'series of characters', but PHP's string is just a list of bytes.

    So if you have a (PHP) array like:

    $input = [110, 111, 100, 101.....];
    

    To convert that into a string, you would actually just do:

    $buffer = '';
    foreach($input as $byte) {
      $buffer .= chr($byte);
    }
    

    The reason Node.js Buffer exist, is because Javascript didn't originally have a 'series of bytes' data structure, and you can't really use javascript's string to store any list of bytes.

    So even though their interfaces are different, the following 3 data types are more or less equivalent:

    • PHP's string
    • Node.js' Buffer
    • C's array of char (there is no native C string type)

    PHP was going to have a data structure similar to Javascript's string in PHP 6, but PHP 6 was canned.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?