src/crypto/forge.js
async function createPrivateKey(size = 2048) {
    let pemKey;

    /* Native implementation */
    if (nativeGenKeyPair) {
        const result = await nativeGenKeyPair('rsa', {
            modulusLength: size,
            publicKeyEncoding: { type: 'spki', format: 'pem' },
            privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
        });

        pemKey = result[1];
    }
    /* Forge implementation */
    else {
        const keyPair = await forgeGenKeyPair({ bits: size });
        pemKey = forge.pki.privateKeyToPem(keyPair.privateKey);
    }

    return Buffer.from(pemKey);
}
async function createPrivateKey(size = 2048) {
    let pemKey;

    /* Native implementation */
    if (nativeGenKeyPair) {
        const result = await nativeGenKeyPair('rsa', {
            modulusLength: size,
            publicKeyEncoding: { type: 'spki', format: 'pem' },
            privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
        });

        pemKey = result[1];
    }
    /* Forge implementation */
    else {
        const keyPair = await forgeGenKeyPair({ bits: size });
        pemKey = forge.pki.privateKeyToPem(keyPair.privateKey);
    }

    return Buffer.from(pemKey);
}
async function createPrivateKey ( size = 2048 ) { let pemKey ; /* Native implementation */ if ( nativeGenKeyPair ) { const result = await nativeGenKeyPair ( 'rsa' , { modulusLength : size , publicKeyEncoding : { type : 'spki' , format : 'pem' } , privateKeyEncoding : { type : 'pkcs8' , format : 'pem' } } ) ; pemKey = result [ 1 ] ; } /* Forge implementation */ else { const keyPair = await forgeGenKeyPair ( { bits : size } ) ; pemKey = forge . pki . privateKeyToPem ( keyPair . privateKey ) ; } return Buffer . from ( pemKey ) ; }
Generate a private RSA key

@param {number} [size] Size of the key, default: `2048`
@returns {Promise<buffer>} Private RSA key
Generate a private RSA key