2012-01-08 00:35:11 +08:00
|
|
|
var RandomKeyGenerator = function(options) {
|
2012-01-14 00:16:42 +08:00
|
|
|
if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
2012-01-08 00:35:11 +08:00
|
|
|
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate a random key
|
|
|
|
RandomKeyGenerator.prototype.createKey = function(keyLength) {
|
|
|
|
var text = '';
|
2012-01-22 04:19:55 +08:00
|
|
|
var index;
|
2012-01-08 00:35:11 +08:00
|
|
|
for (var i = 0; i < keyLength; i++) {
|
2012-01-22 04:19:55 +08:00
|
|
|
index = Math.floor(Math.random() * this.keyspace.length);
|
|
|
|
text += this.keyspace.charAt(index);
|
2012-01-08 00:35:11 +08:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RandomKeyGenerator;
|