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