本文介绍了Node.js& Crypto:存储crypto.createHash实例的当前状态,以便稍后重用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何存储 crypto.createHash('sha1')(填充了后)的当前状态hash.update(buffer))在另一个 http请求使用它可能发生在不同的node.js进程?



我想象这样做:

  var crypto = require ('crypto'),
hash = someDatabase.read('hashstate')//继续填充散列
|| crypto.createHash('sha1'); // start a new hash

//更新散列
someObj.on('data',function(buffer){
hash.update(buffer);
});
someObj.on('end',function(){
//存储哈希的当前状态以后检索它(这将不起作用)
someDatabase.write('hashstate ',hash);

if(theEndOfAllRequests){
//创建多个http请求的结果
hash.digest('hex');
}
});


解决方案

有几个选项,具有不同的权衡。最值得注意的是, crypto 不会公开其散列函数的部分状态,因此没有办法直接实现将计划保存到db。



选项1涉及到一个哈希函数,这可能是棘手的。幸运的是,编写的。再次,它不暴露状态,但我不期望这将是一个非常困难的代码转换。我相信整个状态存储在定义在创建 - h0-4 顶部的变量中, block offset shift totalLength



选项2涉及使用 crypto ,并将数据传递到在进程之间进行散列。这是一个很容易使用,我认为,但也很慢。在几个快速测试中,它看起来像消息将以约2.5-3MB /秒的速度传递,所以每个3MB的块将需要约1.5秒(你只能传递字符串,所以我希望你需要一个Base64转换这需要额外的33%)。为此,您可以使用 process.send 发送数据以及标识id。主进程在每个worker上使用 worker.on 来获取消息,并保持ids到散列对象的映射。最后,你想在消息中有一个标志,告诉主机它正在接收最后一条消息,它会 worker.send 生成的散列(在工人 process.on )。



我很乐意详细说明这些最适合的声音。 / p>

How to store the current state of crypto.createHash('sha1') (after it got filled with hash.update(buffer)) to use it at another http request which might occur at a different process of node.js?

I imagine doing something like this:

var crypto = require('crypto'),
    hash   = someDatabase.read('hashstate')    // continue with filled hash
             || crypto.createHash('sha1');     // start a new hash

// update the hash
someObj.on('data', function(buffer){
    hash.update(buffer);
});
someObj.on('end', function(){
    // store the current state of hash to retrieve it later (this won't work:)
    someDatabase.write('hashstate', hash);

    if(theEndOfAllRequests){
        // create the result of multiple http requests
        hash.digest('hex');
    }
});
解决方案

There are a couple of options I can come up with, with varying trade-offs. The big thing to note is that crypto doesn't expose partial state of its hash functions, so there's no way to directly implement your plan of saving state to a db.

Option 1 involves diving into a hash function, which can be tricky. Fortunately, there already is one written in javascript. Again, it doesn't expose state, but I don't expect that would be a terribly difficult code transformation. I believe the entire state is stored in the variables defined at the top of create - h0-4, block, offset, shift, and totalLength. Then, you could save state in a db as you planned.

Options 2 involves using crypto and passing data to be hashed between processes. This is a lot easier to work with, I think, but also a lot slower. In a few quick tests, it looks like messages will pass around at a rate of about 2.5-3MB/sec, so each 3MB chunk will take about 1.5 seconds (you can only pass strings, so I expect you'll need a Base64 conversion which costs an extra 33%). To do this, you would use process.send to send the data along with identifying id. The master process would use worker.on on each worker to get the messages, and keep a mapping of ids to hashing objects. Finally, you would want to have a flag in the message that tells the master it is receiving the last message, and it would worker.send the resulting hash (received in the worker with process.on).

I'd be happy to elaborate on whichever of these sounds most suitable.

这篇关于Node.js& Crypto:存储crypto.createHash实例的当前状态,以便稍后重用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 01:56