Skip to content

Commit

Permalink
test: add blake3 crate example
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjiahan committed Apr 28, 2024
1 parent 29621cc commit a4d20c4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "a useful command line interface"
[dependencies]
anyhow = "1.0.82"
base64 = "0.22.0"
blake3 = "1.5.1"
clap = { version = "4.5.4", features = ["derive"] }
colored = "2.1.0"
csv = "1.3.0"
Expand Down
32 changes: 32 additions & 0 deletions examples/blake3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
fn main() {
let hash1 = blake3::hash(b"foobarbaz");

let mut hasher = blake3::Hasher::new();
hasher.update(b"foo");
hasher.update(b"bar");
hasher.update(b"baz");
let hash2 = hasher.finalize();
assert_eq!(hash1, hash2);

let mut output = [0; 1000];
let mut output_reader = hasher.finalize_xof();
output_reader.fill(&mut output);
assert_eq!(hash1, output[..32]);

println!("{}", hash1);

let example_key = [42u8; 32];
let mac1 = blake3::keyed_hash(&example_key, b"foobarbaz");

let mut hasher = blake3::Hasher::new_keyed(&example_key);
hasher.update(b"foobarbaz");
let mac2 = hasher.finalize();
assert_eq!(mac1, mac2);
println!("{}", mac1);

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 email_key = blake3::derive_key(EMAIL_CONTEXT, &example_key);
let api_key = blake3::derive_key(API_CONTEXT, &example_key);
assert_ne!(email_key, api_key);
}

0 comments on commit a4d20c4

Please sign in to comment.