- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
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-4
、block
、offset
、shift
和 totalLength
。然后,您可以按计划将状态保存在数据库中。
选项 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/
我正在编写一个插件,有一个ajax调用向用户显示数据。 如果用户想在ajax成功时添加一些js?他可以从他的 js 脚本中做到这一点吗,比如定位这个 ajax 成功事件。 例如: $(documen
我有 html 代码,例如 - x 最初插入 div 'insert_calendar_eda_form'。 Javascript代码 calendar_eda_add
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我已经使用命令 sudo start myservice 启动了一个 upstart 服务。我想要一种方法,以便稍后我(或任何人)可以检查该服务是否已启动并正在运行。检查它的命令是什么? 最佳答案 找
我是一名优秀的程序员,十分优秀!