-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthhash.js
44 lines (39 loc) · 1.13 KB
/
authhash.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
41
42
43
44
const crypto = require('crypto');
class AuthHash {
async generateRandomHash(hashType = 'sha512') {
const buf = await this.getRandomBytes();
return {
raw: buf,
digest: crypto
.createHash(hashType)
.update(buf)
.digest('base64'),
};
}
getRandomBytes() {
return new Promise((resolve, reject) =>
crypto.randomBytes(64, (err, buf) => {
if (err) return reject(err);
else resolve(buf);
})
);
}
calculateVerificationCode(rawHashInBase64) {
// hash raw input with SHA256
const sha256HashedInput = crypto
.createHash('sha256')
.update(Buffer.from(rawHashInBase64, 'base64'))
.digest();
// extract 2 rightmost bytes from it, interpret them as a big-endian unsigned integer
const integer = sha256HashedInput.readUIntBE(
sha256HashedInput.length - 2,
2
);
// take the last 4 digits in decimal for display, padded with zeros from the left
const verificationCode = String(
'0000' + (integer % 10000).toString().substr(-4)
).slice(-4);
return verificationCode;
}
}
module.exports = new AuthHash();