Skip to content

Commit

Permalink
Moved unique identifier generation to lib/security
Browse files Browse the repository at this point in the history
refs #9178
  • Loading branch information
kirrg001 committed Dec 14, 2017
1 parent 411ce69 commit 7291186
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions ghost/security/lib/identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

let _private = {};

// @TODO: replace with crypto.randomBytes
_private.getRandomInt = function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};

/**
* Return a unique identifier with the given `len`.
*
* @param {Number} maxLength
* @return {String}
* @api private
*/
module.exports.uid = function uid(maxLength) {
var buf = [],
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
charLength = chars.length,
i;

for (i = 0; i < maxLength; i = i + 1) {
buf.push(chars[_private.getRandomInt(0, charLength - 1)]);
}

return buf.join('');
};
4 changes: 4 additions & 0 deletions ghost/security/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ module.exports = {

get string() {
return require('./string');
},

get identifier() {
return require('./identifier');
}
};

0 comments on commit 7291186

Please sign in to comment.