Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.newKeySize = 3072;
// accepts either dir, filename or public key
if(obj.pubPem) {
// object has public Key
this.pubPem = obj.pubPem;
this.myPubKey = pki.publicKeyFromPem(this.pubPem);
this.keySize = this.myPubKey.n.bitLength();
} else {
// assume it's a dataDir and filename
let keyData = new DataFile(obj.path);
if(keyData.has('privPem')) {
// key already exists
this.myPrivKey = pki.privateKeyFromPem(keyData.get('privPem'));
this.pubPem = keyData.get('pubPem');
this.myPubKey = pki.publicKeyFromPem(this.pubPem);
this.keySize = this.myPubKey.n.bitLength();
console.log('[NoxiousCrypto] Existing Key Bits: ', this.keySize);
} else {
// key was not on disk, create a new one
// generate an RSA key pair in steps that attempt to run for a
// specified period of time on the main JS thread
var state = rsa.createKeyPairGenerationState(this.newKeySize, 0x10001);
var step = (function() {
if(!rsa.stepKeyPairGenerationState(state, 1000)) {
console.log('[NoxiousCrypto] Generating Key...');
process.nextTick(step);
}
else {
console.log('[NoxiousCrypto] Key Generation Complete.');
this.pubPem = pki.publicKeyToPem(state.keys.publicKey);
keyData.set('privPem', pki.privateKeyToPem(state.keys.privateKey));
constructor(obj) {
this.myPrivKey = null;
this.pubPem = null;
this.myPubKey = null;
this.keySize = 0;
// default size for new keys
this.newKeySize = 3072;
// accepts either dir, filename or public key
if(obj.pubPem) {
// object has public Key
this.pubPem = obj.pubPem;
this.myPubKey = pki.publicKeyFromPem(this.pubPem);
this.keySize = this.myPubKey.n.bitLength();
} else {
// assume it's a dataDir and filename
let keyData = new DataFile(obj.path);
if(keyData.has('privPem')) {
// key already exists
this.myPrivKey = pki.privateKeyFromPem(keyData.get('privPem'));
this.pubPem = keyData.get('pubPem');
this.myPubKey = pki.publicKeyFromPem(this.pubPem);
this.keySize = this.myPubKey.n.bitLength();
console.log('[NoxiousCrypto] Existing Key Bits: ', this.keySize);
} else {
// key was not on disk, create a new one
// generate an RSA key pair in steps that attempt to run for a
// specified period of time on the main JS thread
var state = rsa.createKeyPairGenerationState(this.newKeySize, 0x10001);
function plainRsaKeyToX509Key(key) {
try {
const startTag = '-----BEGIN RSA PUBLIC KEY-----';
const endTag = '-----END RSA PUBLIC KEY-----';
const startTagPos = key.indexOf(startTag);
const endTagPos = key.indexOf(endTag);
return startTagPos !== -1 && endTagPos !== -1 ?
pki.publicKeyToPem(pki.publicKeyFromPem(key)) :
key;
} catch(e) {
// If anything fails, it may not be a plain RSA key, so return the same key.
return key;
}
}
function generateFastKeyPair(bits = 2048, exponent = 65537) {
try {
const keyPair = require('ursa').generatePrivateKey(bits, exponent); // eslint-disable-line global-require
return Promise.resolve({
privateKey: pki.privateKeyFromPem(keyPair.toPrivatePem().toString()),
publicKey: pki.publicKeyFromPem(keyPair.toPublicPem().toString())
});
} catch (err) {
return pki.rsa.generateKeyPairAsync({bits, workers: -1});
}
}
function encryptKeyInfoWithScheme(symmetricKey, options, scheme, callback) {
try {
var rsa_pub = pki.publicKeyFromPem(options.rsa_pub);
var encrypted = rsa_pub.encrypt(symmetricKey.toString('binary'), scheme);
var base64EncodedEncryptedKey = Buffer.from(encrypted, 'binary').toString('base64');
var params = {
encryptedKey: base64EncodedEncryptedKey,
encryptionPublicCert: '' + utils.pemToCert(options.pem.toString()) + '',
keyEncryptionMethod: options.keyEncryptionAlgorighm
};
var result = utils.renderTemplate('keyinfo', params);
callback(null, result);
} catch (e) {
callback(e);
}
}
//If it wasn't in the network, reject the original call.
//Also reject if the pubkey does not match the target.
if (pubKey===null)
reject("[Shallot] - couldn't find pubKey for "+idStr);
else {
let hash = sha3["sha3_"+this.chord.config.idWidth].buffer(pubKey),
hashStr = ID.coerceString(new ID(hash));
if (ID.compare(hashStr, id)!==0) {
reject("[Shallot] - mismatch of obtained pubKey for "+idStr);
} else {
//Build a new item for future lookups, and then resolve the original call.
let item = {
id,
pubKey,
cryptor: pki.publicKeyFromPem(pubKey),
encrypt (msg) {
return this.cryptor.encrypt(msg, "RSA-OAEP");
},
verify (digest, signature) {
return this.cryptor.verify(digest, signature);
}
};
this.keyStore[idStr] = item;
resolve(item);
}
}
} )
} )