Skip to content

Commit 7ca62ee

Browse files
committed
perf: random tokens do not need to be hashed
There's no need to generate extra random bytes only to hash them. A random input will lead to a random hash being generated, but the random input is enough in its own right and does not need to be hashed to make it any more or less secure. The amount of entropy is capped at 32 bytes when hashed, so we may as well just provide 32 random bytes.
1 parent 6627e87 commit 7ca62ee

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/utils/token-util.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
const randomBytes = require('crypto').randomBytes;
8-
const { createHash } = require('../utils/crypto-util');
98

109
/**
1110
* Export `TokenUtil`.
@@ -17,8 +16,15 @@ module.exports = {
1716
* Generate random token.
1817
*/
1918

20-
generateRandomToken: async function() {
21-
const buffer = randomBytes(256);
22-
return createHash({ data: buffer, encoding: 'hex' });
19+
generateRandomToken: function() {
20+
return new Promise((resolve, reject) => {
21+
randomBytes(32, (err, data) => {
22+
if (err) {
23+
reject(err);
24+
} else {
25+
resolve(data.toString('hex'));
26+
}
27+
});
28+
});
2329
}
2430
};

0 commit comments

Comments
 (0)