From ae2d1f0e05449221ee770a393e5c967b359d9b1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Tue, 25 Dec 2018 13:13:52 +0100 Subject: [PATCH] crypto: always accept private keys as public keys Some APIs already accept private keys instead of public keys. This changes all relevant crypto APIs to do so. PR-URL: https://github.com/nodejs/node/pull/25217 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Sam Roberts --- doc/api/crypto.md | 16 ++++++++++++++++ lib/internal/crypto/keys.js | 7 +------ lib/internal/crypto/sig.js | 9 +++++---- src/node_crypto.cc | 28 ++-------------------------- test/parallel/test-crypto-keygen.js | 16 ++++++++++------ 5 files changed, 34 insertions(+), 42 deletions(-) diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 95b1e9114ab536..2605393878ef5d 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -1379,6 +1379,9 @@ This can be called many times with new data as it is streamed. * `key` {Object | string | Buffer} - `key`: {string | Buffer} @@ -1843,6 +1853,12 @@ must be an object with the properties described above. If the format is `'pem'`, the `'key'` may also be an X.509 certificate. +Because public keys can be derived from private keys, a private key may be +passed instead of a public key. In that case, this function behaves as if +[`crypto.createPrivateKey()`][] had been called, except that the type of the +returned `KeyObject` will be `public` and that the private key cannot be +extracted from the returned `KeyObject`. + ### crypto.createSecretKey(key)