egg-bcrypt
!NPM versionnpm-imagenpm-url
!build statustravis-imagetravis-url
!Test coveragecodecov-imagecodecov-url
!David depsdavid-imagedavid-url
!Known Vulnerabilitiessnyk-imagesnyk-url
!npm downloaddownload-imagedownload-urlbcrypt plugin for egg, based on bcrypt (Optimized bcrypt in JavaScript with zero dependencies.)
Install
$ npm i egg-bcrypt --save
Configuration
- config.default.js
exports.bcrypt = {
saltRounds: 10 // default 10
}
- config/plugin.js
// {app_root}/config/plugin.js
exports.bcrypt = {
enable: true,
package: 'egg-bcrypt'
}
Usage
- async To hash a password
@params plainText(string)
@return Promise
```js ctx.genHash(plainText) ```
- async To check a password
@params plainText (string)
@params hash (string)
@return Boolean true/false
```js ctx.compare(plainText, hash) ```
- example
```js
// {app_root}/app/contoller/user.js
...
async generate() {
const hash = await this.ctx.genHash(this.ctx.request.body.plainText);
// Store hash in your password DB
...
}
async compare() {
const { hash, plainText } = this.ctx.request.body;
const checked = await this.ctx.compare(plainText, hash);
this.ctx.body = { checked };
}
...
```