Skip to content

Commit 65df411

Browse files
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 <[email protected]> * Update lib/src/webcrypto/webcrypto.ecdh.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> * Update lib/src/webcrypto/webcrypto.ecdh.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> --------- Co-authored-by: Jonas Finnemann Jensen <[email protected]>
1 parent c6393ba commit 65df411

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

lib/src/webcrypto/webcrypto.ecdh.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,58 @@ abstract class EcdhPrivateKey {
5858
return impl.ecdhPrivateKey_importPkcs8Key(keyData, curve);
5959
}
6060

61+
/// Import ECDH private key in [JSON Web Key][1] format.
62+
///
63+
/// {@macro importJsonWebKey:jwk}
64+
///
65+
/// JSON Web Keys imported using [EcdhPrivateKey.importJsonWebKey] must
66+
/// have the following parameters:
67+
/// * `"kty"`: The key type must be `"EC"`.
68+
/// * `"crv"`: The curve used with the key. This MUST match the curve
69+
/// parameter.
70+
/// * `"x"`: The x coordinate for the Elliptic Curve point represented
71+
/// as a [base64Url] encoded string. The length of this octet string MUST
72+
/// be the full size of a coordinate for the curve specified in the `"crv"`
73+
/// parameter.
74+
/// * `"y"`: The y coordinate for the Elliptic Curve point represented
75+
/// as a base64url encoded string. The length of this octet string MUST
76+
/// be the full size of a coordinate for the curve specified in the `"crv"`
77+
/// parameter.
78+
/// * `"d"`: The private key for the Elliptic Curve point represented as a
79+
/// base64url encoded string.
80+
///
81+
/// For importing a JWK with:
82+
/// * `"crv": "P-256"`, use [EllipticCurve.p256],
83+
/// * `"crv": "P-384"`, use [EllipticCurve.p384], and,
84+
/// * `"crv": "P-521"`, use [EllipticCurve.p521].
85+
///
86+
/// **Example**
87+
/// ```dart
88+
/// import 'package:webcrypto/webcrypto.dart';
89+
///
90+
/// // JSON Web Key as map representing the decoded JSON.
91+
/// final jwk = {
92+
/// 'kty': 'EC',
93+
/// 'crv': 'P-256',
94+
/// 'x': 'kgR_PqO07L8sZOBbw6rvv7O_f7clqDeiE3WnMkb5EoI',
95+
/// 'y': 'djI-XqCqSyO9GFk_QT_stROMCAROIvU8KOORBgQUemE',
96+
/// 'd': '5aPFSt0UFVXYGu-ZKyC9FQIUOAMmnjzdIwkxCMe3Iok',
97+
/// };
98+
///
99+
/// Future<void> main() async {
100+
/// // Import secret key from decoded JSON.
101+
/// final jsonWebKey = await EcdhPrivateKey.importJsonWebKey(
102+
/// jwk,
103+
/// EllipticCurve.p256,
104+
/// );
105+
///
106+
/// // Export the key (print it in same format as it was given).
107+
/// final exportedJsonWebKey = await jsonWebKey.exportJsonWebKey();
108+
/// print(exportedJsonWebKey);
109+
/// }
110+
/// ```
111+
///
112+
/// [1]: https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2
61113
static Future<EcdhPrivateKey> importJsonWebKey(
62114
Map<String, dynamic> jwk,
63115
EllipticCurve curve,

0 commit comments

Comments
 (0)