Skip to content

Commit a5857ed

Browse files
Document importJsonWebKey in the EcdhPublicKey Class (#139)
* docs: document importJsonWebKey in EcdhPublicKey * 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 cc8a9f8 commit a5857ed

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

lib/src/webcrypto/webcrypto.ecdh.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,57 @@ abstract class EcdhPublicKey {
287287
return impl.ecdhPublicKey_importSpkiKey(keyData, curve);
288288
}
289289

290+
/// Import ECDH public key in [JSON Web Key][1] format.
291+
///
292+
/// {@macro importJsonWebKey:jwk}
293+
///
294+
/// JSON Web Keys imported using [EcdhPublicKey.importJsonWebKey] must
295+
/// have the following parameters:
296+
/// * `"kty"`: The key type must be `"EC"`.
297+
/// * `"crv"`: The curve used with the key. This MUST match the curve
298+
/// parameter.
299+
/// * `"x"`: The x coordinate for the Elliptic Curve point represented
300+
/// as a [base64Url] encoded string. The length of this octet string MUST
301+
/// be the full size of a coordinate for the curve specified in the `"crv"`
302+
/// parameter.
303+
/// * `"y"`: The y coordinate for the Elliptic Curve point represented
304+
/// as a [base64url] encoded string. The length of this octet string MUST
305+
/// be the full size of a coordinate for the curve specified in the `"crv"`
306+
/// parameter.
307+
///
308+
/// For importing a JWK with:
309+
/// * `"crv": "P-256"`, use [EllipticCurve.p256],
310+
/// * `"crv": "P-384"`, use [EllipticCurve.p384], and,
311+
/// * `"crv": "P-521"`, use [EllipticCurve.p521].
312+
///
313+
/// **Example**
314+
/// ```dart
315+
/// import 'dart:convert';
316+
/// import 'package:webcrypto/webcrypto.dart';
317+
///
318+
/// // JSON Web Key as map representing the decoded JSON.
319+
/// final jwk = {
320+
/// 'kty': 'EC',
321+
/// 'crv': 'P-256',
322+
/// 'x': 'kgR_PqO07L8sZOBbw6rvv7O_f7clqDeiE3WnMkb5EoI',
323+
/// 'y': 'djI-XqCqSyO9GFk_QT_stROMCAROIvU8KOORBgQUemE'
324+
/// };
325+
///
326+
/// Future<void> main() async{
327+
/// // Import public key from decoded JSON.
328+
/// final jsonWebKey = await EcdhPublicKey.importJsonWebKey(
329+
/// jwk,
330+
/// EllipticCurve.p256,
331+
/// );
332+
///
333+
/// // Export the key (print it in same format as it was given).
334+
/// final exportedJsonWebKey = await jsonWebKey.exportJsonWebKey();
335+
///
336+
/// print(jsonEncode(exportedJsonWebKey));
337+
/// }
338+
/// ```
339+
///
340+
/// [1]: https://www.rfc-editor.org/rfc/rfc7518.html#section-6.2
290341
static Future<EcdhPublicKey> importJsonWebKey(
291342
Map<String, dynamic> jwk,
292343
EllipticCurve curve,

0 commit comments

Comments
 (0)