rsa-compat.js/lib/generate-privkey.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-12-16 05:31:17 +00:00
'use strict';
var oldver = false;
module.exports = function (bitlen, exp) {
bitlen = parseInt(bitlen, 10);
exp = parseInt(exp, 10);
try {
return require('./generate-privkey-node')(bitlen, exp);
} catch(e) {
if (!/generateKeyPairSync is not a function/.test(e.message)) {
throw e;
}
try {
return require('./generate-privkey-ursa')(bitlen, exp);
} catch(e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
if (!oldver) {
oldver = true;
console.warn("[WARN] rsa-compat: Your version of node does not have crypto.generateKeyPair()");
console.warn("[WARN] rsa-compat: Please update to node >= v10.12 or 'npm install ursa'");
console.warn("[WARN] rsa-compat: Using node-forge as a fallback, but it may be unacceptably slow.");
}
try {
return require('./generate-privkey-forge')(bitlen, exp);
} catch(e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
console.error("[ERROR] rsa-compat: could not generate a private key.");
console.error("None of crypto.generateKeyPair, ursa, nor node-forge are present");
}
}
}
};
if (require.main === module) {
console.log(module.exports(2048, 0x10001));
}