Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
var step = (function() {
if(!rsa.stepKeyPairGenerationState(state, 1000)) {
console.log('[NoxiousCrypto] Generating Key...');
process.nextTick(step);
}
else {
console.log('[NoxiousCrypto] Key Generation Complete.');
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});
}
}
export function generateKeyPair({ email }) {
if (!email) {
Log.error('An email is required to generate a keypair');
process.exit(1);
}
const pair = keypair();
const pub = pki.publicKeyFromPem(pair.public);
const priv = pki.privateKeyFromPem(pair.private);
const publicKey = ssh.publicKeyToOpenSSH(pub, email);
const privateKey = ssh.privateKeyToOpenSSH(priv);
const userHome = os.homedir();
const title = uuid.v1();
const publicKeyFile = path.resolve(`${userHome}/.reaction/keys/${title}.pub`);
const privateKeyFile = path.resolve(`${userHome}/.reaction/keys/${title}`);
fs.ensureFileSync(publicKeyFile);
fs.ensureFileSync(privateKeyFile);
fs.writeFileSync(publicKeyFile, publicKey);
fs.writeFileSync(privateKeyFile, privateKey);
.then(() => {
if (existingPrivateKey) {
const privateKey = pki.privateKeyFromPem(existingPrivateKey.toString());
return {
privateKey,
publicKey: pki.rsa.setPublicKey(privateKey.n, privateKey.e)
};
}
return generateFastKeyPair(cfg.keysize);
})
.then(({privateKey, publicKey}) => {
function decryptKeyInfoWithScheme(encryptedKey, options, scheme) {
var key = Buffer.from(encryptedKey.textContent, 'base64').toString('binary');
var private_key = pki.privateKeyFromPem(options.key);
var decrypted = private_key.decrypt(key, scheme);
return Buffer.from(decrypted, 'binary');
}
doc.decryptSecureConfiguration(req.body.uuid||req.query.uuid, function (decErr, config) {
var pk = pki.privateKeyFromPem(config.privateKey);
var rsak = new RSAKey();
rsak.setPrivate(pk.n.toString(16), '10001', pk.d.toString(16));
var cleartext = rsak.decrypt(req.body.blob,'hex');
cb(null, cleartext);
});
});
const attrs = [{
name: 'commonName',
value: domain
}, {
name: 'organizationName',
value: 'do_not_trust_phoenixmatrix'
}, {
name: 'countryName',
value: 'US'
}];
csr.setSubject(attrs);
csr.sign(keys.privateKey, md.sha256.create());
const caKeyPem = yield fs.readFileAsync(caKeyPath);
const caKey = pki.privateKeyFromPem(caKeyPem);
const caCertPem = yield fs.readFileAsync(caCertPath);
const caCert = pki.certificateFromPem(caCertPem);
var certificate = pki.createCertificate();
certificate.serialNumber = getSerial();
certificate.validity.notBefore = new Date();
const expiration = moment(certificate.validity.notBefore);
expiration.add(config.certificateExpiration, 'days');
certificate.validity.notAfter = expiration.toDate();
certificate.setSubject(csr.subject.attributes);
certificate.setIssuer(caCert.subject.attributes);
certificate.publicKey = csr.publicKey;
certificate.sign(caKey, md.sha256.create());
export const getRsaPublicPemFromPrivatePem = (privateKey: string): string =>
pki.publicKeyToPem(getRsaPublicKeyFromPrivateKey(pki.privateKeyFromPem(privateKey)))