You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/lib/pwhash.js

28 lines
798 B
JavaScript

5 years ago
const crypto = require('crypto');
5 years ago
5 years ago
module.exports = {
5 years ago
_b64encode: (str) => Buffer.from(str).toString('base64'),
_b64decode: (str) => Buffer.from(str, 'base64').toString(),
_md5: (str) => crypto.createHash('md5').update(str).digest('hex'),
_sha1: (str) => crypto.createHash('sha1').update(str).digest('hex'),
5 years ago
/**
* @param {string} password
* @param {string} salt
* @param {string} hash
*/
check(password, salt, hash) {
5 years ago
return hash === this.hash(password, salt);
5 years ago
},
/**
* @param {string} password
* @param {string} salt
*/
hash(password, salt) {
5 years ago
const res = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha256');
5 years ago
return res.toString('Hex');
},
salt() {
return String.random();
5 years ago
},
};