Load crypto functionality on node.js.

This commit is contained in:
Tony Garnock-Jones 2016-03-18 17:01:00 -04:00
parent 3785cebdf2
commit dea733911d
1 changed files with 12 additions and 6 deletions

View File

@ -8,13 +8,19 @@ if ((typeof window !== 'undefined') &&
window.crypto.getRandomValues(buf);
return btoa(String.fromCharCode.apply(null, buf)).replace(/=/g,'');
};
} else if ((typeof crypto !== 'undefined') &&
(typeof crypto.randomBytes !== 'undefined')) {
randomId = function (byteCount) {
return crypto.randomBytes(byteCount).base64Slice().replace(/=/g,'');
};
} else {
console.warn('No suitable implementation for RandomID.randomId available.');
var crypto;
try {
crypto = require('crypto');
} catch (e) {}
if ((typeof crypto !== 'undefined') &&
(typeof crypto.randomBytes !== 'undefined')) {
randomId = function (byteCount) {
return crypto.randomBytes(byteCount).base64Slice().replace(/=/g,'');
};
} else {
console.warn('No suitable implementation for RandomID.randomId available.');
}
}
module.exports.randomId = randomId;