From 65df4112e29c6b640defe0e98f3af2ba44b53988 Mon Sep 17 00:00:00 2001 From: HamdaanAliQuatil <96776914+HamdaanAliQuatil@users.noreply.github.com> Date: Thu, 30 May 2024 18:36:48 +0530 Subject: [PATCH] Document importJsonWebKey in the EcdhPrivateKey Class (#122) * docs: document importJsonWebKey in EcdhPrivateKey * Update lib/src/webcrypto/webcrypto.ecdh.dart Co-authored-by: Jonas Finnemann Jensen * Update lib/src/webcrypto/webcrypto.ecdh.dart Co-authored-by: Jonas Finnemann Jensen * Update lib/src/webcrypto/webcrypto.ecdh.dart Co-authored-by: Jonas Finnemann Jensen --------- Co-authored-by: Jonas Finnemann Jensen --- lib/src/webcrypto/webcrypto.ecdh.dart | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lib/src/webcrypto/webcrypto.ecdh.dart b/lib/src/webcrypto/webcrypto.ecdh.dart index a78c91de..f9403860 100644 --- a/lib/src/webcrypto/webcrypto.ecdh.dart +++ b/lib/src/webcrypto/webcrypto.ecdh.dart @@ -58,6 +58,58 @@ abstract class EcdhPrivateKey { return impl.ecdhPrivateKey_importPkcs8Key(keyData, curve); } + /// Import ECDH private key in [JSON Web Key][1] format. + /// + /// {@macro importJsonWebKey:jwk} + /// + /// JSON Web Keys imported using [EcdhPrivateKey.importJsonWebKey] must + /// have the following parameters: + /// * `"kty"`: The key type must be `"EC"`. + /// * `"crv"`: The curve used with the key. This MUST match the curve + /// parameter. + /// * `"x"`: The x coordinate for the Elliptic Curve point represented + /// as a [base64Url] encoded string. The length of this octet string MUST + /// be the full size of a coordinate for the curve specified in the `"crv"` + /// parameter. + /// * `"y"`: The y coordinate for the Elliptic Curve point represented + /// as a base64url encoded string. The length of this octet string MUST + /// be the full size of a coordinate for the curve specified in the `"crv"` + /// parameter. + /// * `"d"`: The private key for the Elliptic Curve point represented as a + /// base64url encoded string. + /// + /// For importing a JWK with: + /// * `"crv": "P-256"`, use [EllipticCurve.p256], + /// * `"crv": "P-384"`, use [EllipticCurve.p384], and, + /// * `"crv": "P-521"`, use [EllipticCurve.p521]. + /// + /// **Example** + /// ```dart + /// import 'package:webcrypto/webcrypto.dart'; + /// + /// // JSON Web Key as map representing the decoded JSON. + /// final jwk = { + /// 'kty': 'EC', + /// 'crv': 'P-256', + /// 'x': 'kgR_PqO07L8sZOBbw6rvv7O_f7clqDeiE3WnMkb5EoI', + /// 'y': 'djI-XqCqSyO9GFk_QT_stROMCAROIvU8KOORBgQUemE', + /// 'd': '5aPFSt0UFVXYGu-ZKyC9FQIUOAMmnjzdIwkxCMe3Iok', + /// }; + /// + /// Future main() async { + /// // Import secret key from decoded JSON. + /// final jsonWebKey = await EcdhPrivateKey.importJsonWebKey( + /// jwk, + /// EllipticCurve.p256, + /// ); + /// + /// // Export the key (print it in same format as it was given). + /// final exportedJsonWebKey = await jsonWebKey.exportJsonWebKey(); + /// print(exportedJsonWebKey); + /// } + /// ``` + /// + /// [1]: https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2 static Future importJsonWebKey( Map jwk, EllipticCurve curve,