| 59 | } |
| 60 | |
| 61 | function main({ n, keyFormat, keyType }) { |
| 62 | const keyPair = { |
| 63 | publicKey: crypto.createPublicKey(keyFixtures[keyType].publicKey), |
| 64 | privateKey: crypto.createPrivateKey(keyFixtures[keyType].privateKey), |
| 65 | }; |
| 66 | |
| 67 | let key, fn; |
| 68 | switch (keyFormat) { |
| 69 | case 'spki': |
| 70 | key = keyPair.publicKey.export({ format: 'pem', type: 'spki' }); |
| 71 | fn = crypto.createPublicKey; |
| 72 | break; |
| 73 | case 'pkcs8': |
| 74 | key = keyPair.privateKey.export({ format: 'pem', type: 'pkcs8' }); |
| 75 | fn = crypto.createPrivateKey; |
| 76 | break; |
| 77 | case 'der-spki': { |
| 78 | const options = { format: 'der', type: 'spki' }; |
| 79 | key = { ...options, key: keyPair.publicKey.export(options) }; |
| 80 | fn = crypto.createPublicKey; |
| 81 | break; |
| 82 | } |
| 83 | case 'der-pkcs8': { |
| 84 | const options = { format: 'der', type: 'pkcs8' }; |
| 85 | key = { ...options, key: keyPair.privateKey.export(options) }; |
| 86 | fn = crypto.createPrivateKey; |
| 87 | break; |
| 88 | } |
| 89 | case 'jwk-public': { |
| 90 | const options = { format: 'jwk' }; |
| 91 | key = { ...options, key: keyPair.publicKey.export(options) }; |
| 92 | fn = crypto.createPublicKey; |
| 93 | break; |
| 94 | } |
| 95 | case 'jwk-private': { |
| 96 | const options = { format: 'jwk' }; |
| 97 | key = { ...options, key: keyPair.privateKey.export(options) }; |
| 98 | fn = crypto.createPrivateKey; |
| 99 | break; |
| 100 | } |
| 101 | case 'raw-public': { |
| 102 | const exportedKey = keyPair.publicKey.export({ format: 'raw-public' }); |
| 103 | key = { key: exportedKey, format: 'raw-public', asymmetricKeyType: keyType }; |
| 104 | if (keyType === 'ec') key.namedCurve = keyPair.publicKey.asymmetricKeyDetails.namedCurve; |
| 105 | fn = crypto.createPublicKey; |
| 106 | break; |
| 107 | } |
| 108 | case 'raw-private': { |
| 109 | const exportedKey = keyPair.privateKey.export({ format: 'raw-private' }); |
| 110 | key = { key: exportedKey, format: 'raw-private', asymmetricKeyType: keyType }; |
| 111 | if (keyType === 'ec') key.namedCurve = keyPair.privateKey.asymmetricKeyDetails.namedCurve; |
| 112 | fn = crypto.createPrivateKey; |
| 113 | break; |
| 114 | } |
| 115 | case 'raw-seed': { |
| 116 | key = { |
| 117 | key: keyPair.privateKey.export({ format: 'raw-seed' }), |
| 118 | format: 'raw-seed', |