美文网首页
nodejs之bcrypt

nodejs之bcrypt

作者: 简爱WindMan | 来源:发表于2020-03-21 00:24 被阅读0次

    安装

    • 不值为何,node_modules/bcrypt/examples/forever_gen_salt.js 其他版本一直报错,只有3.0.0版本可用,如下
    node forever_gen_salt.js 
    dyld: lazy symbol binding failed: Symbol not found: __ZN4node19GetCurrentEventLoopEPN2v87IsolateE
      Referenced from: /xxxx/wmNode/node_modules/bcrypt/lib/binding/bcrypt_lib.node
      Expected in: flat namespace
    
    dyld: Symbol not found: __ZN4node19GetCurrentEventLoopEPN2v87IsolateE
      Referenced from: /Users/developer/Desktop/Life/MyCode/wmNode/node_modules/bcrypt/lib/binding/bcrypt_lib.node
      Expected in: flat namespace
    
    Abort trap: 6
    
    • bcrypt文档介绍,安装需要node的稳定版本;
    • 用nvm切换到node的10.0版本,即可安装

    bcrypt使用

    // 获取密钥
    bcrypt.genSalt(10, function(err, salt) {
      console.log('salt: ' + salt);
      console.log('salt cb end: ' + (Date.now() - start) + 'ms');
      // 用密钥salt,加密字符串test
      bcrypt.hash('test', salt, function(err, crypted) {
        console.log('crypted: ' + crypted);
        console.log('crypted cb end: ' + (Date.now() - start) + 'ms');
        console.log('rounds used from hash:', bcrypt.getRounds(crypted));
      // 比较,成功
        bcrypt.compare('test', crypted, function(err, res) {
          console.log('compared true: ' + res);
          console.log('compared true cb end: ' + (Date.now() - start) + 'ms');
        });
      // 比较,失败
        bcrypt.compare('bacon', crypted, function(err, res) {
          console.log('compared false: ' + res);
          console.log('compared false cb end: ' + (Date.now() - start) + 'ms');
        });
      });
    })
    

    相关文章

      网友评论

          本文标题:nodejs之bcrypt

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