如何生成随机SHA1哈希以在node.js中用作ID? [英] How to generate random SHA1 hash to use as ID in node.js?
本文介绍了如何生成随机SHA1哈希以在node.js中用作ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此行为node.js生成sha1 id:
I am using this line to generate a sha1 id for node.js:
crypto.createHash('sha1').digest('hex');
问题在于它每次都返回相同的ID。
The problem is that it's returning the same id every time.
是否可以让它每次生成一个随机ID,这样我就可以将它用作数据库文档ID?
Is it possible to have it generate a random id each time so I can use it as a database document id?
推荐答案
看看这里:如何使用node.js加密来创建HMAC-SHA1哈希?
我创建当前时间戳的哈希值+随机数以确保哈希唯一性:
Have a look here: How do I use node.js Crypto to create a HMAC-SHA1 hash? I'd create a hash of the current timestamp + a random number to ensure hash uniqueness:
var current_date = (new Date()).valueOf().toString();
var random = Math.random().toString();
crypto.createHash('sha1').update(current_date + random).digest('hex');
这篇关于如何生成随机SHA1哈希以在node.js中用作ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文