How to generate random SHA1 hash to use as ID in node.js?

后端 未结 4 1606
有刺的猬
有刺的猬 2020-12-04 04:45

I am using this line to generate a sha1 id for node.js:

crypto.createHash(\'sha1\').digest(\'hex\');

The problem is that it\'s returning th

4条回答
  •  离开以前
    2020-12-04 05:19

    Using crypto is a good approach cause it's native and stable module, but there are cases where you can use bcrypt if you want to create a really strong and secure hash. I use it for passwords it has a lot of techniques for hashing, creating salt and comparing passwords.

    Technique 1 (generate a salt and hash on separate function calls)

    const salt = bcrypt.genSaltSync(saltRounds);
    const hash = bcrypt.hashSync(myPlaintextPassword, salt);
    

    Technique 2 (auto-gen a salt and hash):

    const hash = bcrypt.hashSync(myPlaintextPassword, saltRounds);
    

    For more examples you can check here: https://www.npmjs.com/package/bcrypt

提交回复
热议问题