简体   繁体   中英

Using Node.js crypto generate multiple pairs of public/private key

How to generate multi pairs of the key using crypto in Node.js

In node.js there is this code to generate pairs. but if I change the name to {key, public_key} then trying console.log them, it prints undefined . However, I need two pairs, can't run it twice with the same names, or it tells they are defined before. is there another way to generate another pair with crypto?

const { publicKey, privateKey } = generateKeyPairSync('rsa', {
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret'
  }
});

key: undefined pub_key: undefined

or:

{ privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
                      ^

SyntaxError: Unexpected token =

You can't use two sink functions in a one row because we have to wait for both to come up and then print. So it is better to use callback functions to get answers and throw in external variables

let public1, public2;
let private1, private2;

const { generateKeyPair } = require('crypto');
generateKeyPair('rsa', {
    modulusLength: 4096,
    publicKeyEncoding: {
        type: 'spki',
        format: 'pem'
    },
    privateKeyEncoding: {
        type: 'pkcs8',
        format: 'pem',
        cipher: 'aes-256-cbc',
        passphrase: 'top secret'
    }
}, (err, publicKey, privateKey) => {
    public1 = publicKey;
    private1 = privateKey;
    generateKeyPair('rsa', {
        modulusLength: 4096,
        publicKeyEncoding: {
            type: 'spki',
            format: 'pem'
        },
        privateKeyEncoding: {
            type: 'pkcs8',
            format: 'pem',
            cipher: 'aes-256-cbc',
            passphrase: 'top secret'
        }
    }, (err, publicKey1, privateKey1) => {
        public2 = publicKey1;
        private2 = privateKey1;
    });
});
console.log("public1 : ", public1)
console.log("private1 : ", private1)
console.log("public2 : ", public2)
console.log("private2 : ", private2)

You can assign publicKey, privateKey properties to different variables during the destructing assignment as follows:

const { generateKeyPairSync } = require('crypto');

const keyOptions = [{
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret 1'
  }
}, {
  modulusLength: 2048,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret 2'
  }
}]

const [
  { publicKey: publicKey1, privateKey: privateKey1 },
  { publicKey: publicKey2, privateKey: privateKey2 }
] = keyOptions.map(options => generateKeyPairSync('rsa', options))

console.log(
  publicKey1,
  privateKey1,
  publicKey2,
  privateKey2
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM