Skip to content

Commit e7fbe50

Browse files
Add Doc comments for EcdhPrivateKey Class (#127)
* docs: add ecdh private key example * 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]>
1 parent 65df411 commit e7fbe50

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

lib/src/webcrypto/webcrypto.ecdh.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,43 @@
1414

1515
part of 'webcrypto.dart';
1616

17+
/// ECDH private key for deriving a shared secret.
18+
///
19+
/// Elliptic Curve Diffie-Hellman (ECDH) is a key agreement protocol that allows
20+
/// two parties to establish a shared secret over an insecure channel.
21+
/// An [EcdhPrivateKey] holds a private key that can be used to derive a
22+
/// shared secret given the public key from a different key pair.
23+
///
24+
/// Instances of [EcdhPrivateKey] can be imported from:
25+
/// * PKCS8 Key using [EcdhPrivateKey.importPkcs8Key], and,
26+
/// * JSON Web Key using [EcdhPrivateKey.importJsonWebKey].
27+
///
28+
/// A key pair can be generated using [EcdhPrivateKey.generateKey].
29+
///
30+
/// {@template EcdhPrivateKey:example}
31+
/// **Example**
32+
/// ```dart
33+
/// import 'dart:convert';
34+
/// import 'package:webcrypto/webcrypto.dart';
35+
///
36+
/// Future<void> main() async {
37+
/// // Alice generates a key-pair
38+
/// final kpA = await EcdhPrivateKey.generateKey(EllipticCurve.p256);
39+
///
40+
/// // Bob generates a key-pair
41+
/// final kpB = await EcdhPrivateKey.generateKey(EllipticCurve.p256);
42+
///
43+
/// // Alice can make a shared secret using Bob's public key
44+
/// final sharedSecretA = await kpA.privateKey.deriveBits(256, kpB.publicKey);
45+
///
46+
/// // Bob can make the same shared secret using Alice public key
47+
/// final sharedSecretB = await kpB.privateKey.deriveBits(256, kpA.publicKey);
48+
///
49+
/// // Alice and Bob should have the same shared secret
50+
/// assert(base64.encode(sharedSecretA) == base64.encode(sharedSecretB));
51+
/// }
52+
/// ```
53+
/// {@endtemplate}
1754
@sealed
1855
abstract class EcdhPrivateKey {
1956
EcdhPrivateKey._(); // keep the constructor private.

0 commit comments

Comments
 (0)