gatsbyz
gatsbyz

Reputation: 1075

How to create a public key as a binary Distinguished Encoding Rules (DER)-encoded object in Node.js?

I'm creating a AWS KMS nock version and specifically working on getPublicKey. Here, I need to create a DER-encoded public key. Is there a Node.js way to do this? Thanks.

Upvotes: 0

Views: 186

Answers (1)

gatsbyz
gatsbyz

Reputation: 1075

KeyUsage === 'ENCRYPT_DECRYPT'
        ? // eslint-disable-next-line no-sync
        crypto.generateKeyPairSync('rsa', {
          modulusLength: 2048,
          publicKeyEncoding: {
            type: 'spki',
            format: 'der'
          },
          privateKeyEncoding: {
            type: 'pkcs8',
            format: 'der',
            cipher: 'aes-256-cbc',
            passphrase: 'passphrase'
          }
        })
        : // eslint-disable-next-line no-sync
        crypto.generateKeyPairSync('ec', {
          namedCurve: 'P-256',
          publicKeyEncoding: {
            type: 'spki',
            format: 'der'
          },
          privateKeyEncoding: {
            type: 'pkcs8',
            format: 'der',
            cipher: 'aes-256-cbc',
            passphrase: 'passphrase'
          }
        });

Upvotes: 0

Related Questions