Skip to content

Commit ef2d064

Browse files
author
Alexandr Mochalov
committed
Use crypto.randomBytes instead of Math.random;
1 parent a1bc3f8 commit ef2d064

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
var assert = require('assert')
3+
var crypto = require('crypto')
34

45
var base62 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
56
var base36 = 'abcdefghijklmnopqrstuvwxyz0123456789'
@@ -19,7 +20,10 @@ function create(chars) {
1920
len = len || 10
2021
assert(typeof len === 'number' && len >= 0, 'the length of the random string must be a number!')
2122
var salt = ''
22-
for (var i = 0; i < len; i++) salt += chars[Math.floor(length * Math.random())]
23+
for (var i = 0; i < len; i++) {
24+
var rndVal = (crypto.randomBytes(1).readIntBE(0,1) + 128) / 256;
25+
salt += chars[Math.floor(length * rndVal)]
26+
}
2327
return salt
2428
}
2529
}

0 commit comments

Comments
 (0)