美文网首页
使用crypto生成md5码的时候报“Digest alread

使用crypto生成md5码的时候报“Digest alread

作者: 小m_up | 来源:发表于2019-06-28 20:25 被阅读0次

最近node层写一个接口的时候,想做安全校验,访问的时候加token,然后就想到使用crypto库,先加密一串字符串,然后再次加密,如下所示:

const md5 = crypto.createHash('md5');
const a = md5.update('12345').digest('hex');
const b = md5.update(`${a}6789`).digest('hex');

但是发现报了下面这个错:


image.png

原来是一个crypto实例只能调用digest一次,所以想用两次的话,必须实例化两个实例:

const md51 = crypto.createHash('md5');
const a = md51.update('12345').digest('hex');
const md52 = crypto.createHash('md5');
const b = md52.update(`${a}6789`).digest('hex');

看着好繁琐啊,我只要加一次就得实例化一次
然后发现还有一个库md5也可以直接使用,比这个方便也简单多了

const a = md5('12345');
const b = md5(`${a}6789`)

相关文章

网友评论

      本文标题:使用crypto生成md5码的时候报“Digest alread

      本文链接:https://www.haomeiwen.com/subject/yzvtzqtx.html