nodejs之bcrypt

安装

  • 不值为何,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)