gpt4 book ai didi

Node.js & Crypto : Store the current state of a crypto. createHash 实例稍后重用

转载 作者:搜寻专家 更新时间:2023-10-31 23:35:56 27 4
gpt4 key购买 nike

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

我想象做这样的事情:

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

最佳答案

我可以想出几个选项,权衡取舍各不相同。需要注意的重要一点是,crypto 不会公开其散列函数的部分状态,因此无法直接实现将状态保存到数据库的计划。

选项 1 涉及深入研究哈希函数,这可能很棘手。还好有already is one written in javascript .同样,它不会公开状态,但我不认为这会是一个非常困难的代码转换。我相信整个状态都存储在 create 顶部定义的变量中 - h0-4blockoffsetshifttotalLength。然后,您可以按计划将状态保存在数据库中。

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

我很乐意详细说明这些声音中最合适的那个。

关于Node.js & Crypto : Store the current state of a crypto. createHash 实例稍后重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16755738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com