-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbcryptHasher.js
40 lines (29 loc) · 959 Bytes
/
bcryptHasher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const HasherContract = require('@ostro/contracts/encryption/hasher');
const RuntimeException = require('@ostro/support/exceptions/runtimeException');
const bcrypt = require('bcrypt');
class BcryptHasher extends HasherContract {
$rounds = 0;
constructor($options = {}) {
super()
this.$rounds = $options['rounds'] || this.$rounds;
}
make($value, $options = {}) {
let salt = bcrypt.genSaltSync(this.cost());
let $hash = bcrypt.hashSync($value, salt);
if ($hash === false) {
throw new RuntimeException('Bcrypt hashing not supported.');
}
return $hash;
}
check($value, $hashedValue, $options = {}) {
return bcrypt.compareSync($value, $hashedValue);
}
setRounds($rounds) {
this.$rounds = $rounds;
return this;
}
cost($options = []) {
return $options['rounds'] || this.$rounds;
}
}
module.exports = BcryptHasher