diff --git a/index.js b/index.js index 24b0ebb..e00f040 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var assert = require('assert') +var crypto = require('crypto') var base62 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' var base36 = 'abcdefghijklmnopqrstuvwxyz0123456789' @@ -19,7 +20,10 @@ function create(chars) { len = len || 10 assert(typeof len === 'number' && len >= 0, 'the length of the random string must be a number!') var salt = '' - for (var i = 0; i < len; i++) salt += chars[Math.floor(length * Math.random())] + for (var i = 0; i < len; i++) { + var rndVal = (crypto.randomBytes(1).readIntBE(0,1) + 128) / 256; + salt += chars[Math.floor(length * rndVal)] + } return salt } }