Created
February 29, 2020 00:12
-
-
Save mlmitch/7346fe1bcac7b85f4d345bca170ea576 to your computer and use it in GitHub Desktop.
Convert AWS KMS GenerateDataKeyPair Output to PEM in NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const AWS = require('aws-sdk') | |
| const Crypto = require('crypto') | |
| AWS.config.region = 'us-east-1' | |
| var kms = new AWS.KMS() | |
| keyArn = 'Put AWS KMS Key ARN Here' | |
| // Set the KeyPairSpec to the type of key you want to generate. | |
| const request = { | |
| KeyId: keyArn, | |
| KeyPairSpec: 'ECC_NIST_P256' | |
| } | |
| kms.generateDataKeyPair(request, (err, data) => { | |
| if(err) { | |
| console.log(err) | |
| } else { | |
| const privateKeyInput = { | |
| key: data.PrivateKeyPlaintext, | |
| format: 'der', | |
| type: 'pkcs8' | |
| } | |
| const privateKeyObject = Crypto.createPrivateKey(privateKeyInput) | |
| const publicKeyInput = { | |
| key: data.PublicKey, | |
| format: 'der', | |
| type: 'spki' | |
| } | |
| const publicKeyObject = Crypto.createPublicKey(publicKeyInput) | |
| const privateKeyExportOptions = { | |
| format: 'pem', | |
| type: 'sec1' | |
| } | |
| const pemPrivate = privateKeyObject.export(privateKeyExportOptions) | |
| const publicKeyExportOptions = { | |
| format: 'pem', | |
| type: 'spki' | |
| } | |
| const pemPublic = publicKeyObject.export(publicKeyExportOptions) | |
| console.log(pemPrivate) | |
| console.log(pemPublic) | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment