Skip to content

Commit

Permalink
update README examples
Browse files Browse the repository at this point in the history
  • Loading branch information
oconnor663-zoom authored and oconnor663 committed Jul 25, 2021
1 parent 037de38 commit 5aef684
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ output_reader.fill(&mut output);
assert_eq!(&output[..32], hash1.as_bytes());

// Print a hash as hex.
println!("{}", hash1.to_hex());
println!("{}", hash1);
```

Besides `hash`, BLAKE3 provides two other modes, `keyed_hash` and
Expand All @@ -151,21 +151,18 @@ let mac2 = hasher.finalize();
assert_eq!(mac1, mac2);
```

The `derive_key` mode takes a context string of any length and key material of
any length (not a password), and it outputs a derived key of any length. The
context string should be hardcoded, globally unique, and application-specific.
A good default format for the context string is `"[application] [commit
timestamp] [purpose]"`:
The `derive_key` mode takes a context string and some key material (not a
password). The context string should be hardcoded, globally unique, and
application-specific. A good default format for the context string is
`"[application] [commit timestamp] [purpose]"`:

```rust
// Derive a couple of subkeys for different purposes.
const EMAIL_CONTEXT: &str = "BLAKE3 example 2020-01-07 17:10:44 email key";
const API_CONTEXT: &str = "BLAKE3 example 2020-01-07 17:11:21 API key";
let input_key_material = b"usually at least 32 random bytes, not a password!";
let mut email_key = [0; 32];
blake3::derive_key(EMAIL_CONTEXT, input_key_material, &mut email_key);
let mut api_key = [0; 32];
blake3::derive_key(API_CONTEXT, input_key_material, &mut api_key);
let input_key_material = b"usually at least 32 random bytes, not a password";
let email_key = blake3::derive_key(EMAIL_CONTEXT, input_key_material);
let api_key = blake3::derive_key(API_CONTEXT, input_key_material);
assert!(email_key != api_key);
```

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! # }
//!
//! // Print a hash as hex.
//! println!("{}", hash1.to_hex());
//! println!("{}", hash1);
//! # Ok(())
//! # }
//! ```
Expand Down

0 comments on commit 5aef684

Please sign in to comment.