Generate a Hash from string in Javascript

前端 未结 22 1276
不知归路
不知归路 2020-11-22 03:34

I need to convert strings to some form of hash. Is this possible in JavaScript?

I\'m not utilizing a server-side language so I can\'t do it that way.

22条回答
  •  孤城傲影
    2020-11-22 04:19

    I'm kinda late to the party, but you can use this module: crypto:

    const crypto = require('crypto');
    
    const SALT = '$ome$alt';
    
    function generateHash(pass) {
      return crypto.createHmac('sha256', SALT)
        .update(pass)
        .digest('hex');
    }
    

    The result of this function is always is 64 characters string; something like this: "aa54e7563b1964037849528e7ba068eb7767b1fab74a8d80fe300828b996714a"

提交回复
热议问题