Skip to content

Commit 513aa6b

Browse files
committed
doc: use doc examples which gets run by test
1 parent 3b5315a commit 513aa6b

File tree

4 files changed

+129
-57
lines changed

4 files changed

+129
-57
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v1.0.2 - Doc Examples
2+
Use executing examples
3+
14
## v1.0.1 - Package Metadata
25
Add package metadata such as repository etc.
36

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
[package]
22
name = "webcryptobox"
33
description = "Convenient wrappers around OpenSSL to use WebCrypto compatible cryptography."
4-
version = "1.0.1"
4+
version = "1.0.2"
55
edition = "2021"
66
documentation = "https://docs.rs/webcryptobox/latest/webcryptobox/"
77
homepage = "https://github.com/jo/webcryptobox-rs"
88
repository = "https://github.com/jo/webcryptobox-rs"
99
readme = "README.md"
1010
license = "Apache-2.0"
11-
license-file = "LICENSE"
1211
authors = ["Johannes J. Schmidt <[email protected]>"]
1312
keywords = ["WebCrypto", "crypto", "ec", "aes"]
1413

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Webcryptobox
22
Webcryptobox provides convenient wrappers around OpenSSL to use WebCrypto compatible cryptography.
33

4-
It works nicely together with the [JavaScript Webcryptobox](https://github.com/jo/webcryptobox-js).
4+
It works nicely together with the [JavaScript Webcryptobox](https://github.com/jo/webcryptobox-js) and [Bash Webcryptobox](https://github.com/jo/webcryptobox-sh).
55

66
Webcryptobox provides functions for elliptic curve key generation, derivation, import and export as well as AES encryption and decryption.
77

src/lib.rs

Lines changed: 124 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ impl Webcryptobox {
3434
/// This example uses the curve `P-256`, AES in `CBC` mode with a key length of 128.
3535
///
3636
/// **Supported Curves:**
37-
/// * `P-256` (Nid::X9_62_PRIME256V1) aka `secp256r1` or `prime256v1`
38-
/// * `P-384` (Nid::SECP384R1) aka `secp384r1` or `ansip384r1`
39-
/// * `P-521` (Nid::SECP521R1) aka `secp521r1` or `ansip521r1`
37+
/// * `P-256` [openssl::Nid::X9_62_PRIME256V1`] aka `secp256r1` or `prime256v1`
38+
/// * `P-384` [openssl::Nid::SECP384R1`] aka `secp384r1` or `ansip384r1`
39+
/// * `P-521` [openssl::Nid::SECP521R1`] aka `secp521r1` or `ansip521r1`
4040
///
4141
/// **Supported AES Modes:**
4242
/// * `CBC`: Cipher Block Chaining Mode
@@ -81,8 +81,10 @@ impl Webcryptobox {
8181

8282
/// Creates a Webcryptobox with defaults (`curve=P-521, mode=GCM, length=256`).
8383
///
84-
/// ```rs
85-
/// let wcb = Webcryptobox::default();
84+
/// # Example:
85+
///
86+
/// ```rust
87+
/// let wcb = webcryptobox::Webcryptobox::default();
8688
/// ```
8789
pub fn default() -> Webcryptobox {
8890
let curve = Nid::SECP521R1;
@@ -98,7 +100,10 @@ impl Webcryptobox {
98100

99101
/// Generate an EC private key.
100102
///
101-
/// ```rs
103+
/// # Example:
104+
///
105+
/// ```rust
106+
/// let wcb = webcryptobox::Webcryptobox::default();
102107
/// let key = wcb.generate_key_pair();
103108
/// ```
104109
pub fn generate_key_pair(&self) -> Result<EcKey<Private>, ErrorStack> {
@@ -107,8 +112,11 @@ impl Webcryptobox {
107112

108113
/// Given a private EC key, derives the public EC key.
109114
///
110-
/// ```rs
111-
/// let key = wcb.generate_key_pair();
115+
/// # Example:
116+
///
117+
/// ```rust
118+
/// let wcb = webcryptobox::Webcryptobox::default();
119+
/// let key = wcb.generate_key_pair().unwrap();
112120
/// let public_key = wcb.derive_public_key(&key);
113121
/// ```
114122
pub fn derive_public_key(
@@ -122,40 +130,49 @@ impl Webcryptobox {
122130

123131
/// Import a private key PEM.
124132
///
125-
/// ```rs
126-
/// let pem = b"-----BEGIN PRIVATE KEY-----
133+
/// # Example:
134+
///
135+
/// ```rust
136+
/// let wcb = webcryptobox::Webcryptobox::default();
137+
/// let pem = (b"-----BEGIN PRIVATE KEY-----
127138
/// MIHuAgEAMBAGByqGSM49AgEGBSuBBAAjBIHWMIHTAgEBBEIBcf8zEjlssqn4aTEB
128139
/// RR43ofwH/4BAXDAAd83Kz1Dyd+Ko0pit4ESgqSu/bJMdnDrpiGYuz0Klarwip8LD
129140
/// rYd9mEahgYkDgYYABAF2Nu9XKPs2CVFocuqCfaX5FzDUt6/nT/3Evqq8jBhK/ziN
130141
/// TrEs4wkZjuei5TS25aabX6iMex3etoN/GOw1KYpI4QBtIUnWudG8FT8N+USHSL9G
131142
/// h9fi+Yofeq4Io9DxPU1ChCKPIoQ6ORAMWoOCk9bTdIy6yqx33+RIM04wub4QAgDo
132143
/// LQ==
133-
/// -----END PRIVATE KEY-----";
134-
/// let key = wcb.import_private_key_pem(&pem)
144+
/// -----END PRIVATE KEY-----").to_vec();
145+
/// let key = wcb.import_private_key_pem(&pem);
135146
/// ```
136147
pub fn import_private_key_pem(&self, pem: &[u8]) -> Result<EcKey<Private>, ErrorStack> {
137148
EcKey::private_key_from_pem(pem)
138149
}
139150

140151
/// Import a public key PEM.
141152
///
142-
/// ```rs
143-
/// let pem = b"-----BEGIN PUBLIC KEY-----
153+
/// # Example:
154+
///
155+
/// ```rust
156+
/// let wcb = webcryptobox::Webcryptobox::default();
157+
/// let pem = (b"-----BEGIN PUBLIC KEY-----
144158
/// MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBdjbvVyj7NglRaHLqgn2l+Rcw1Lev
145159
/// 50/9xL6qvIwYSv84jU6xLOMJGY7nouU0tuWmm1+ojHsd3raDfxjsNSmKSOEAbSFJ
146160
/// 1rnRvBU/DflEh0i/RofX4vmKH3quCKPQ8T1NQoQijyKEOjkQDFqDgpPW03SMusqs
147161
/// d9/kSDNOMLm+EAIA6C0=
148-
/// -----END PUBLIC KEY-----";
149-
/// let key = wcb.import_public_key_pem(&pem)
162+
/// -----END PUBLIC KEY-----").to_vec();
163+
/// let key = wcb.import_public_key_pem(&pem);
150164
/// ```
151165
pub fn import_public_key_pem(&self, pem: &[u8]) -> Result<EcKey<Public>, ErrorStack> {
152166
EcKey::public_key_from_pem(pem)
153167
}
154168

155169
/// Export a private EC key in PEM format.
156170
///
157-
/// ```rs
158-
/// let key = wcb.generate_key_pair();
171+
/// # Example:
172+
///
173+
/// ```rust
174+
/// let wcb = webcryptobox::Webcryptobox::default();
175+
/// let key = wcb.generate_key_pair().unwrap();
159176
/// let pem = wcb.export_private_key_pem(key);
160177
/// ```
161178
pub fn export_private_key_pem(
@@ -168,9 +185,12 @@ impl Webcryptobox {
168185

169186
/// Export a public EC key in PEM format.
170187
///
171-
/// ```rs
172-
/// let key = wcb.generate_key_pair();
173-
/// let public_key = wcb.derive_public_key(&key);
188+
/// # Example:
189+
///
190+
/// ```rust
191+
/// let wcb = webcryptobox::Webcryptobox::default();
192+
/// let key = wcb.generate_key_pair().unwrap();
193+
/// let public_key = wcb.derive_public_key(&key).unwrap();
174194
/// let pem = wcb.export_public_key_pem(&public_key);
175195
/// ```
176196
pub fn export_public_key_pem(&self, public_key: &EcKey<Public>) -> Result<Vec<u8>, ErrorStack> {
@@ -179,10 +199,14 @@ impl Webcryptobox {
179199

180200
/// Calculate a SHA-1 fingeprint from a private key.
181201
///
182-
/// This hashes the DER data of the public key part of the key.
202+
/// This hashes the DER data of the public key part of the key. Note this does not create a
203+
/// hash of the private key, but the public key.
183204
///
184-
/// ```rs
185-
/// let key = wcb.generate_key_pair();
205+
/// # Example:
206+
///
207+
/// ```rust
208+
/// let wcb = webcryptobox::Webcryptobox::default();
209+
/// let key = wcb.generate_key_pair().unwrap();
186210
/// let fingerprint = wcb.sha1_fingerprint_from_private_key(&key);
187211
/// ```
188212
pub fn sha1_fingerprint_from_private_key(
@@ -202,9 +226,12 @@ impl Webcryptobox {
202226
///
203227
/// This hashes the DER data of the key.
204228
///
205-
/// ```rs
206-
/// let key = wcb.generate_key_pair();
207-
/// let public_key = wcb.derive_public_key(&key);
229+
/// # Example:
230+
///
231+
/// ```rust
232+
/// let wcb = webcryptobox::Webcryptobox::default();
233+
/// let key = wcb.generate_key_pair().unwrap();
234+
/// let public_key = wcb.derive_public_key(&key).unwrap();
208235
/// let fingerprint = wcb.sha1_fingerprint_from_public_key(&public_key);
209236
/// ```
210237
pub fn sha1_fingerprint_from_public_key(
@@ -222,10 +249,14 @@ impl Webcryptobox {
222249

223250
/// Calculate a SHA-256 fingeprint from a private key.
224251
///
225-
/// This hashes the DER data of the public key part of the key.
252+
/// This hashes the DER data of the public key part of the key. Note this does not create a
253+
/// hash of the private key, but the public key.
226254
///
227-
/// ```rs
228-
/// let key = wcb.generate_key_pair();
255+
/// # Example:
256+
///
257+
/// ```rust
258+
/// let wcb = webcryptobox::Webcryptobox::default();
259+
/// let key = wcb.generate_key_pair().unwrap();
229260
/// let fingerprint = wcb.sha256_fingerprint_from_private_key(&key);
230261
/// ```
231262
pub fn sha256_fingerprint_from_private_key(
@@ -245,9 +276,12 @@ impl Webcryptobox {
245276
///
246277
/// This hashes the DER data of the key.
247278
///
248-
/// ```rs
249-
/// let key = wcb.generate_key_pair();
250-
/// let public_key = wcb.derive_public_key(&key);
279+
/// # Example:
280+
///
281+
/// ```rust
282+
/// let wcb = webcryptobox::Webcryptobox::default();
283+
/// let key = wcb.generate_key_pair().unwrap();
284+
/// let public_key = wcb.derive_public_key(&key).unwrap();
251285
/// let fingerprint = wcb.sha256_fingerprint_from_public_key(&public_key);
252286
/// ```
253287
pub fn sha256_fingerprint_from_public_key(
@@ -265,7 +299,10 @@ impl Webcryptobox {
265299

266300
/// Generate AES key material to be used with `encrypt` and `decrypt`.
267301
///
268-
/// ```rs
302+
/// # Example:
303+
///
304+
/// ```rust
305+
/// let wcb = webcryptobox::Webcryptobox::default();
269306
/// let key = wcb.generate_key();
270307
/// ```
271308
pub fn generate_key(&self) -> Result<Vec<u8>, ErrorStack> {
@@ -287,10 +324,13 @@ impl Webcryptobox {
287324
/// Derives AES key material to be used with `encrypt` and `decrypt` from given private and
288325
/// public key.
289326
///
290-
/// ```rs
291-
/// let alice = wcb.generate_key_pair();
292-
/// let bob = wcb.generate_key_pair();
293-
/// let bobs_public_key = wcb.derive_public_key(&bob);
327+
/// # Example:
328+
///
329+
/// ```rust
330+
/// let wcb = webcryptobox::Webcryptobox::default();
331+
/// let alice = wcb.generate_key_pair().unwrap();
332+
/// let bob = wcb.generate_key_pair().unwrap();
333+
/// let bobs_public_key = wcb.derive_public_key(&bob).unwrap();
294334
/// let key = wcb.derive_key(alice, bobs_public_key);
295335
/// ```
296336
pub fn derive_key(
@@ -321,7 +361,10 @@ impl Webcryptobox {
321361

322362
/// Generate AES initialization vector to be used with `encrypt` and `decrypt`.
323363
///
324-
/// ```rs
364+
/// # Example:
365+
///
366+
/// ```rust
367+
/// let wcb = webcryptobox::Webcryptobox::default();
325368
/// let iv = wcb.generate_iv();
326369
/// ```
327370
pub fn generate_iv(&self) -> Result<Vec<u8>, ErrorStack> {
@@ -342,9 +385,12 @@ impl Webcryptobox {
342385

343386
/// Encrypts data with key and iv.
344387
///
345-
/// ```rs
346-
/// let key = wcb.generate_key();
347-
/// let iv = wcb.generate_iv();
388+
/// # Example:
389+
///
390+
/// ```rust
391+
/// let wcb = webcryptobox::Webcryptobox::default();
392+
/// let key = wcb.generate_key().unwrap();
393+
/// let iv = wcb.generate_iv().unwrap();
348394
/// let data = (b"a secret message").to_vec();
349395
/// let encrypted_message = wcb.encrypt(&key, &iv, &data);
350396
/// ```
@@ -363,8 +409,16 @@ impl Webcryptobox {
363409

364410
/// Decrypts data with key and iv.
365411
///
366-
/// To decrypt the message from above:
367-
/// ```rs
412+
/// # Example:
413+
///
414+
/// ```rust
415+
/// let wcb = webcryptobox::Webcryptobox::default();
416+
///
417+
/// let key = wcb.generate_key().unwrap();
418+
/// let iv = wcb.generate_iv().unwrap();
419+
/// let data = (b"a secret message").to_vec();
420+
/// let encrypted_message = wcb.encrypt(&key, &iv, &data).unwrap();
421+
///
368422
/// let message = wcb.decrypt(&key, &iv, &encrypted_message);
369423
/// ```
370424
pub fn decrypt(&self, key: &[u8], iv: &Vec<u8>, data: &[u8]) -> Result<Vec<u8>, ErrorStack> {
@@ -381,13 +435,18 @@ impl Webcryptobox {
381435

382436
/// Derives AES key from given private and public key and encrypts message.
383437
///
384-
/// ```rs
385-
/// let alice = wcb.generate_key_pair();
386-
/// let bob = wcb.generate_key_pair();
387-
/// let bobs_public_key = wcb.derive_public_key(&bob);
388-
/// let iv = wcb.generate_iv();
438+
/// # Example:
439+
///
440+
/// ```rust
441+
/// let wcb = webcryptobox::Webcryptobox::default();
442+
///
443+
/// let alice = wcb.generate_key_pair().unwrap();
444+
/// let bob = wcb.generate_key_pair().unwrap();
445+
/// let bobs_public_key = wcb.derive_public_key(&bob).unwrap();
446+
///
447+
/// let iv = wcb.generate_iv().unwrap();
389448
/// let data = (b"a secret message").to_vec();
390-
/// let encrypted_message = wcb.derive_and_encrypt(&alice, &bobs_public_key, &iv, &data);
449+
/// let encrypted_message = wcb.derive_and_encrypt(alice, bobs_public_key, &iv, &data);
391450
/// ```
392451
pub fn derive_and_encrypt(
393452
&self,
@@ -403,10 +462,21 @@ impl Webcryptobox {
403462

404463
/// Derives AES key from given private and public key and decrypts message.
405464
///
406-
/// To decrypt the message from above:
407-
/// ```rs
408-
/// let alice_public_key = wcb.derive_public_key(&alice);
409-
/// let message = wcb.derive_and_decrypt(&bob, &alice_public_key, &iv, &encrypted_message);
465+
/// # Example:
466+
///
467+
/// ```rust
468+
/// let wcb = webcryptobox::Webcryptobox::default();
469+
///
470+
/// let alice = wcb.generate_key_pair().unwrap();
471+
/// let bob = wcb.generate_key_pair().unwrap();
472+
/// let bobs_public_key = wcb.derive_public_key(&bob).unwrap();
473+
/// let alice_public_key = wcb.derive_public_key(&alice).unwrap();
474+
///
475+
/// let iv = wcb.generate_iv().unwrap();
476+
/// let data = (b"a secret message").to_vec();
477+
/// let encrypted_message = wcb.derive_and_encrypt(alice, bobs_public_key, &iv, &data).unwrap();
478+
///
479+
/// let message = wcb.derive_and_decrypt(bob, alice_public_key, &iv, &encrypted_message);
410480
/// ```
411481
pub fn derive_and_decrypt(
412482
&self,

0 commit comments

Comments
 (0)