|
14 | 14 |
|
15 | 15 | part of 'webcrypto.dart';
|
16 | 16 |
|
| 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} |
17 | 54 | @sealed
|
18 | 55 | abstract class EcdhPrivateKey {
|
19 | 56 | EcdhPrivateKey._(); // keep the constructor private.
|
|
0 commit comments