| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 456f76a commit 32e45b2
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1951,27 +1951,22 @@ changes: | |||
| 1951 | 1951 | - `publicExponent`: {number} Public exponent (RSA). **Default:** `0x10001`. | |
| 1952 | 1952 | - `divisorLength`: {number} Size of `q` in bits (DSA). | |
| 1953 | 1953 | - `namedCurve`: {string} Name of the curve to use (EC). | |
| 1954 | - - `publicKeyEncoding`: {Object} | ||
| 1955 | - - `type`: {string} Must be one of `'pkcs1'` (RSA only) or `'spki'`. | ||
| 1956 | - - `format`: {string} Must be `'pem'` or `'der'`. | ||
| 1957 | - - `privateKeyEncoding`: {Object} | ||
| 1958 | - - `type`: {string} Must be one of `'pkcs1'` (RSA only), `'pkcs8'` or | ||
| 1959 | - `'sec1'` (EC only). | ||
| 1960 | - - `format`: {string} Must be `'pem'` or `'der'`. | ||
| 1961 | - - `cipher`: {string} If specified, the private key will be encrypted with | ||
| 1962 | - the given `cipher` and `passphrase` using PKCS#5 v2.0 password based | ||
| 1963 | - encryption. | ||
| 1964 | - - `passphrase`: {string | Buffer} The passphrase to use for encryption, see | ||
| 1965 | - `cipher`. | ||
| 1954 | + - `publicKeyEncoding`: {Object} See [`keyObject.export()`][]. | ||
| 1955 | + - `privateKeyEncoding`: {Object} See [`keyObject.export()`][]. | ||
| 1966 | 1956 | * Returns: {Object} | |
| 1967 | 1957 | - `publicKey`: {string | Buffer | KeyObject} | |
| 1968 | 1958 | - `privateKey`: {string | Buffer | KeyObject} | |
| 1969 | 1959 | ||
| 1970 | 1960 | Generates a new asymmetric key pair of the given `type`. Only RSA, DSA and EC | |
| 1971 | 1961 | are currently supported. | |
| 1972 | 1962 | ||
| 1973 | - It is recommended to encode public keys as `'spki'` and private keys as | ||
| 1974 | - `'pkcs8'` with encryption: | ||
| 1963 | + If a `publicKeyEncoding` or `privateKeyEncoding` was specified, this function | ||
| 1964 | + behaves as if [`keyObject.export()`][] had been called on its result. Otherwise, | ||
| 1965 | + the respective part of the key is returned as a [`KeyObject`]. | ||
| 1966 | + | ||
| 1967 | + When encoding public keys, it is recommended to use `'spki'`. When encoding | ||
| 1968 | + private keys, it is recommended to use `'pks8'` with a strong passphrase, and to | ||
| 1969 | + keep the passphrase confidential. | ||
| 1975 | 1970 | ||
| 1976 | 1971 | ```js | |
| 1977 | 1972 | const { generateKeyPairSync } = require('crypto'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,7 +74,11 @@ function handleError(impl, wrap) { | |||
| 74 | 74 | if (err !== undefined) | |
| 75 | 75 | throw err; | |
| 76 | 76 | ||
| 77 | - return { publicKey, privateKey }; | ||
| 77 | + // If no encoding was chosen, return key objects instead. | ||
| 78 | + return { | ||
| 79 | + publicKey: wrapKey(publicKey, PublicKeyObject), | ||
| 80 | + privateKey: wrapKey(privateKey, PrivateKeyObject) | ||
| 81 | + }; | ||
| 78 | 82 | } | |
| 79 | 83 | ||
| 80 | 84 | function parseKeyEncoding(keyType, options) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,6 +95,21 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher); | |||
| 95 | 95 | testSignVerify(publicKey, privateKey); | |
| 96 | 96 | } | |
| 97 | 97 | ||
| 98 | + { | ||
| 99 | + // Test sync key generation with key objects. | ||
| 100 | + const { publicKey, privateKey } = generateKeyPairSync('rsa', { | ||
| 101 | + modulusLength: 512 | ||
| 102 | + }); | ||
| 103 | + | ||
| 104 | + assert.strictEqual(typeof publicKey, 'object'); | ||
| 105 | + assert.strictEqual(publicKey.type, 'public'); | ||
| 106 | + assert.strictEqual(publicKey.asymmetricKeyType, 'rsa'); | ||
| 107 | + | ||
| 108 | + assert.strictEqual(typeof privateKey, 'object'); | ||
| 109 | + assert.strictEqual(privateKey.type, 'private'); | ||
| 110 | + assert.strictEqual(privateKey.asymmetricKeyType, 'rsa'); | ||
| 111 | + } | ||
| 112 | + | ||
| 98 | 113 | { | |
| 99 | 114 | const publicKeyEncoding = { | |
| 100 | 115 | type: 'pkcs1', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments