diff --git a/Cargo.lock b/Cargo.lock index 470d600..be447f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,6 +65,18 @@ version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" +[[package]] +name = "arrayref" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" + +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + [[package]] name = "base64" version = "0.22.0" @@ -86,12 +98,31 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" +[[package]] +name = "blake3" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30cca6d3674597c30ddf2c587bf8d9d65c9a84d2326d941cc79c9842dfe0ef52" +dependencies = [ + "arrayref", + "arrayvec", + "cc", + "cfg-if", + "constant_time_eq", +] + [[package]] name = "bumpalo" version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "cc" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" + [[package]] name = "cfg-if" version = "1.0.0" @@ -154,6 +185,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "constant_time_eq" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" + [[package]] name = "csv" version = "1.3.0" @@ -449,6 +486,7 @@ version = "0.1.0" dependencies = [ "anyhow", "base64", + "blake3", "clap", "colored", "csv", diff --git a/Cargo.toml b/Cargo.toml index b8c88ac..c0ed779 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/blake3.rs b/examples/blake3.rs new file mode 100644 index 0000000..b7328fd --- /dev/null +++ b/examples/blake3.rs @@ -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); +}