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.

图片转代码服务由CSDN问答提供 功能建议

我有一个现有的NodeJS库,我试图在PHP中匹配哈希缓冲区功能(特别是“AFTER”值 下面),但到目前为止失败了。 什么是NodeJS在内部对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 ......] 
 ... 
   
 
 

鉴于只有字符串类型可以 hashed,Node如何以另一个缓冲区的形式到达上面的“AFTER”值?

在PHP中我尝试过:

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

我还写了一个程序,给了我一个 将数组摘要为字符串,然后对结果(及其各种排列)进行散列,而不能完全匹配Node在“AFTER”块中给出的内容。

  function buffer_digest_from(array $ dec):string 
 {
 $ hex =''; 
 
 foreach($ dec as $ int){
 //带有零的左键单个十六进制值,以匹配链点哈希值 
 $ hex。= str_pad(dechex($ int),2,'0',STR_PAD_LEFT); 
} 
 
返回$ hex; 
} 
 
hash('sha256',pack('H  *',buffer_digest_from([110,111,100,101 ......]))); 
hash('sha256',p  ack('c *',buffer_digest_from([110,111,100,101 ......]))); 
hash('sha256',buffer_digest_from([110,111,100,101 .....  。])); 
   
 
 

我当然意识到语言在实现上有所不同,但如果我想做的只是操纵整数,那么应该有一些方法 无论实施如何,都要达成相同的解决方案。

非常感谢您的阅读。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题