Skip to content

Commit c6393ba

Browse files
Document importPkcs8Key in the EcdhPrivateKey Class (#121)
* docs: document importPkcs8Key in EcdhPrivateKey * docs: add review edit * Update lib/src/webcrypto/webcrypto.ecdh.dart --------- Co-authored-by: Jonas Finnemann Jensen <[email protected]>
1 parent 2b1d99e commit c6393ba

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lib/src/webcrypto/webcrypto.ecdh.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@ part of 'webcrypto.dart';
1818
abstract class EcdhPrivateKey {
1919
EcdhPrivateKey._(); // keep the constructor private.
2020

21+
/// Import [EcdhPrivateKey] in the [PKCS #8][1] format.
22+
///
23+
/// Creates an [EcdhPrivateKey] from [keyData] given as the DER encodeding _PrivateKeyInfo structure_ specified in [RFC 5208][1].
24+
/// The [curve] specified must match the curved used in [keyData].
25+
///
26+
/// **Example**
27+
/// ```dart
28+
/// import 'package:pem/pem.dart';
29+
/// import 'package:webcrypto/webcrypto.dart';
30+
///
31+
/// // Read key data from a PEM encoded block. This will remove the
32+
/// // the padding, decode base64 and return the encoded bytes.
33+
/// List<int> keyData = PemCodec(PemLabel.privateKey).decode('''
34+
/// -----BEGIN PRIVATE KEY-----
35+
/// MIGHAgEAMBMGByqGSM4.....
36+
/// -----END PRIVATE KEY-----
37+
/// ''');
38+
///
39+
///
40+
/// Future<void> main() async {
41+
/// // Import the Private Key from a Binary PEM decoded data.
42+
/// final privateKey = await EcdhPrivateKey.importPkcs8Key(
43+
/// keyData,
44+
/// EllipticCurve.p256,
45+
/// );
46+
///
47+
/// // Export the private key (print it in same format as it was given).
48+
/// final exportedPkcs8Key = await privateKey.exportPkcs8Key();
49+
/// print(PemCodec(PemLabel.privateKey).encode(exportedPkcs8Key));
50+
/// }
51+
/// ```
52+
///
53+
/// [1]: https://datatracker.ietf.org/doc/html/rfc5208
2154
static Future<EcdhPrivateKey> importPkcs8Key(
2255
List<int> keyData,
2356
EllipticCurve curve,

0 commit comments

Comments
 (0)