2017-11-01 08:40:43 +08:00
|
|
|
module.exports = class RandomKeyGenerator {
|
|
|
|
|
|
|
|
// Initialize a new generator with the given keySpace
|
|
|
|
constructor(options = {}) {
|
|
|
|
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
2012-01-14 00:16:42 +08:00
|
|
|
}
|
2012-01-08 00:35:11 +08:00
|
|
|
|
2017-11-01 08:40:43 +08:00
|
|
|
// Generate a key of the given length
|
|
|
|
createKey(keyLength) {
|
|
|
|
var text = '';
|
|
|
|
|
|
|
|
for (var i = 0; i < keyLength; i++) {
|
|
|
|
const index = Math.floor(Math.random() * this.keyspace.length);
|
|
|
|
text += this.keyspace.charAt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
2012-01-08 00:35:11 +08:00
|
|
|
}
|
|
|
|
|
2017-11-01 08:40:43 +08:00
|
|
|
};
|