Service for Sails framework with Hash features.
- bcrypt
Install this module.
npm install sails-service-hash
Then require it in your service and create Hash instance.
// api/services/HashService.js
import HashService from 'sails-service-hash';
export default HashService('bcrypt');
// api/controllers/HashController.js
export default {
compare: function(req, res) {
let myPasswordInHash = HashService.hashSync('MY_PASSWORD');
let isCorrectPassword = HashService.compareSync('MY_PASSWORD', myPasswordInHash);
res.ok(isCorrectPassword);
}
};
Each of Hash instances has 4 methods:
data
- Plain data that you want to hash.
config
- Additional configuration for override pre-defined config.
Hash your data and returns Promise.
data
- Plain data that you want to hash.
config
- Additional configuration for override pre-defined config.
Hash your data in sync mode and returns hash.
plainData
- Plain data that you want to compare with.
hash
- Hash that was got from hash methods.
config
- Additional configuration for override pre-defined config.
Compare plainData
with hash
and returns Promise.
plainData
- Plain data that you want to compare with.
hash
- Hash that was got from hash methods.
config
- Additional configuration for override pre-defined config.
Compare plainData
with hash
and returns Boolean.
let bcrypt = HashService('bcrypt', {
saltLength: 10 // Optional argument if you want to specify rounds for of auto-generated salt
});
bcrypt.hash('MY_PASSWORD').then(hash => {
bcrypt.compare('MY_PASSWORD', hash).then(console.log.bind(console));
});
let hash = bcrypt.hashSync('MY_PASSWORD');
let isEqual = bcrypt.compareSync('MY_PASSWORD', hash);
-
You MUST inherits from
BaseHash
. -
Your class MUST implement all the methods described above in API section.
The MIT License (MIT)
Copyright (c) 2015 Eugene Obrezkov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.