### SHA-2 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/README.md Demonstrates how to use the SHA-256 hash function from the `sha2` crate. It covers initializing the hasher, updating it with data, and finalizing to get the hash. It also shows how to serialize the hash to Base64 and Hex formats. ```rust use sha2::{Sha256, Digest}; let mut hasher = Sha256::new(); let data = b"Hello world!"; hasher.update(data); // `update` can be called repeatedly and is generic over `AsRef<[u8]>` hasher.update("String data"); // Note that calling `finalize()` consumes hasher let hash = hasher.finalize(); println!("Binary hash: {:?}", hash); ``` ```rust use base64ct::{Base64, Encoding}; let base64_hash = Base64::encode_string(&hash); println!("Base64-encoded hash: {}", base64_hash); let hex_hash = base16ct::lower::encode_string(&hash); println!("Hex-encoded hash: {}", hex_hash); ``` ```rust use sha2::{Sha256, Digest}; let hash = Sha256::new() .chain_update(b"Hello world!") .chain_update("String data") .finalize(); ``` ```rust use sha2::{Sha256, Digest}; let hash = Sha256::digest(b"my message"); ``` ```rust use sha2::{Sha256, Digest}; use std::{fs, io}; let mut file = fs::File::open(&path)?; let mut hasher = Sha256::new(); let n = io::copy(&mut file, &mut hasher)?; let hash = hasher.finalize(); ``` -------------------------------- ### MD5 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/md5/README.md Demonstrates how to compute an MD5 hash for a given byte string using the `md5` crate. It shows initializing the hasher, updating it with data, finalizing the hash, and comparing it to an expected value. It also includes an example of hex-encoding the resulting hash. ```rust use md5::{Md5, Digest}; use hex_literal::hex; let mut hasher = Md5::new(); hasher.update(b"hello world"); let hash = hasher.finalize(); assert_eq!(hash, hex!("5eb63bbbe01eeed093cb22bb8f5acdc3")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "5eb63bbbe01eeed093cb22bb8f5acdc3"); ``` -------------------------------- ### Skein Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/skein/README.md Demonstrates how to use the Skein512_256 hash function to hash data and verify the output. It includes updating the hasher with multiple byte slices and finalizing the hash. The example also shows how to hex-encode the resulting hash using the `base16ct` crate. ```rust use hex_literal::hex; use skein::{Digest, Skein512_256}; let mut hasher = Skein512_256::new(); hasher.update(b"The quick brown fox "); hasher.update(b"jumps over the lazy dog"); let hash = hasher.finalize(); assert_eq!(hash, hex!("b3250457e05d3060b1a4bbc1428bc75a3f525ca389aeab96cfa34638d96e492a")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "b3250457e05d3060b1a4bbc1428bc75a3f525ca389aeab96cfa34638d96e492a"); ``` -------------------------------- ### MD4 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/md4/README.md Demonstrates how to create an MD4 hasher instance, process input data, and finalize the hash digest. It also shows how to hex-encode the resulting hash using the `base16ct` crate. ```rust use md4::{Md4, Digest}; use hex_literal::hex; // create a Md4 hasher instance let mut hasher = Md4::new(); // process input message hasher.update(b"hello world"); // acquire hash digest in the form of Array, // which in this case is equivalent to [u8; 16] let hash = hasher.finalize(); assert_eq!(hash, hex!("aa010fbc1d14c795d86ef98c95479d17")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "aa010fbc1d14c795d86ef98c95479d17"); ``` -------------------------------- ### FSB-256 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/fsb/README.md Demonstrates how to use the FSB-256 hash function via the `Digest` trait. It shows how to initialize a hasher, update it with data, finalize the hash, and assert its correctness. It also includes an example of hex-encoding the resulting hash. ```rust use fsb::{Digest, Fsb256}; use hex_literal::hex; let mut hasher = Fsb256::new(); hasher.update(b"hello"); let hash = hasher.finalize(); assert_eq!(hash, hex!("0f036dc3761aed2cba9de586a85976eedde6fa8f115c0190763decc02f28edbc")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "0f036dc3761aed2cba9de586a85976eedde6fa8f115c0190763decc02f28edbc"); ``` -------------------------------- ### SM3 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/sm3/README.md Demonstrates how to use the SM3 hasher to compute the hash of a given input. It includes updating the hasher with data and finalizing the hash. The example also shows how to encode the resulting hash into a hexadecimal string using the `base16ct` crate. ```rust use sm3::{Digest, Sm3}; use hex_literal::hex; let mut hasher = Sm3::new(); hasher.update(b"hello world"); let hash = hasher.finalize(); assert_eq!(hash, hex!("44f0061e69fa6fdfc290c494654a05dc0c053da7e5c52b84ef93a9d67d3fff88")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "44f0061e69fa6fdfc290c494654a05dc0c053da7e5c52b84ef93a9d67d3fff88"); ``` -------------------------------- ### Streebog Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/streebog/README.md Demonstrates how to use Streebog256 and Streebog512 hash functions to hash a string and verify the output. It also shows how to hex-encode the resulting hash using the base16ct crate. ```rust use streebog::{Digest, Streebog256, Streebog512}; use hex_literal::hex; let mut hasher = Streebog256::new(); hasher.update("The quick brown fox jumps over the lazy dog"); let hash256 = hasher.finalize(); assert_eq!(hash256, hex!("3e7dea7f2384b6c5a3d0e24aaa29c05e89ddd762145030ec22c71a6db8b2c1f4")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash256 = base16ct::lower::encode_string(&hash256); assert_eq!(hex_hash256, "3e7dea7f2384b6c5a3d0e24aaa29c05e89ddd762145030ec22c71a6db8b2c1f4"); // Same example for Streebog-512 let mut hasher = Streebog512::new(); hasher.update("The quick brown fox jumps over the lazy dog."); let hash512 = hasher.finalize(); assert_eq!(hash512, hex!( "fe0c42f267d921f940faa72bd9fcf84f9f1bd7e9d055e9816e4c2ace1ec83be8" "2d2957cd59b86e123d8f5adee80b3ca08a017599a9fc1a14d940cf87c77df070" )); ``` -------------------------------- ### BLAKE2 Fixed Output Size Hashing Source: https://github.com/rustcrypto/hashes/blob/master/blake2/README.md Demonstrates hashing with Blake2b512 and Blake2s256 with fixed output sizes. It shows how to create a hasher, update it with data, and finalize the hash. Also includes an example of hex-encoding the resulting hash. ```rust use blake2::{Blake2b512, Blake2s256, Digest}; use hex_literal::hex; // create a Blake2b512 object let mut hasher = Blake2b512::new(); // write input message hasher.update(b"hello world"); // read hash digest and consume hasher let hash = hasher.finalize(); assert_eq!(hash, hex!( "021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbc" "c05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0" )); // same example for Blake2s256: let mut hasher = Blake2s256::new(); hasher.update(b"hello world"); let hash = hasher.finalize(); assert_eq!(hash, hex!("9aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "9aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b"); ``` -------------------------------- ### MD2 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/md2/README.md Demonstrates how to use the MD2 hash algorithm to hash a string and compare the result with an expected hexadecimal value. It also shows how to encode the hash using the base16ct crate. ```rust use md2::{Md2, Digest}; use hex_literal::hex; let mut hasher = Md2::new(); hasher.update(b"hello world"); let hash = hasher.finalize(); assert_eq!(hash, hex!("d9cce882ee690a5c1ce70beff3a78c77")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "d9cce882ee690a5c1ce70beff3a78c77"); ``` -------------------------------- ### SHAKE128 Extendable Output Function (XOF) Example Source: https://github.com/rustcrypto/hashes/blob/master/sha3/README.md Illustrates the usage of SHAKE128, an extendable-output function (XOF), which allows reading arbitrary lengths of output. This example uses the `Update` and `ExtendableOutput` traits, and demonstrates reading a fixed-size buffer from the XOF reader. ```rust use sha3::{Shake128, digest::{Update, ExtendableOutput, XofReader}}; use hex_literal::hex; let mut hasher = Shake128::default(); hasher.update(b"abc"); let mut reader = hasher.finalize_xof(); let mut buf = [0u8; 10]; reader.read(&mut buf); assert_eq!(buf, hex!("5881092dd818bf5cf8a3")); ``` -------------------------------- ### RIPEMD-160 and RIPEMD-320 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/ripemd/README.md Demonstrates how to use the Ripemd160 and Ripemd320 hash functions from the ripemd crate. It shows how to initialize a hasher, update it with data, finalize the hash, and compare it with expected values. It also includes an example of hex-encoding the resulting hash using the base16ct crate. ```rust use ripemd::{Ripemd160, Ripemd320, Digest}; use hex_literal::hex; let mut hasher = Ripemd160::new(); hasher.update(b"Hello world!"); let hash160 = hasher.finalize(); assert_eq!(hash160, hex!("7f772647d88750add82d8e1a7a3e5c0902a346a3")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash160 = base16ct::lower::encode_string(&hash160); assert_eq!(hex_hash160, "7f772647d88750add82d8e1a7a3e5c0902a346a3"); // Same example for RIPEMD-320 let mut hasher = Ripemd320::new(); hasher.update(b"Hello world!"); let hash320 = hasher.finalize(); assert_eq!(hash320, hex!( "f1c1c231d301abcf2d7daae0269ff3e7bc68e623" "ad723aa068d316b056d26b7d1bb6f0cc0f28336d" )); ``` -------------------------------- ### Shabal256 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/shabal/README.md Demonstrates how to use the Shabal256 hash algorithm. It initializes a hasher, updates it with input data, finalizes the hash, and asserts the expected output. It also shows how to hex-encode the resulting hash using the `base16ct` crate. ```rust use shabal::{Shabal256, Digest}; use hex_literal::hex; let mut hasher = Shabal256::new(); hasher.update(b"helloworld"); let hash = hasher.finalize(); assert_eq!(hash, hex!("d945dee21ffca23ac232763aa9cac6c15805f144db9d6c97395437e01c8595a8")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "d945dee21ffca23ac232763aa9cac6c15805f144db9d6c97395437e01c8595a8"); ``` -------------------------------- ### GOST94 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/gost94/README.md Demonstrates how to use the Gost94CryptoPro hasher to compute the hash of a given string and compare it with the expected hex-encoded hash. It also shows how to hex-encode the resulting hash. ```rust use gost94::{Gost94CryptoPro, Digest}; use hex_literal::hex; let mut hasher = Gost94CryptoPro::new(); hasher.update("The quick brown fox jumps over the lazy dog"); let hash = hasher.finalize(); assert_eq!(hash, hex!("9004294a361a508c586fe53d1f1b02746765e71b765472786e4770d565830a76")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "9004294a361a508c586fe53d1f1b02746765e71b765472786e4770d565830a76"); ``` -------------------------------- ### BelT Hash Example Source: https://github.com/rustcrypto/hashes/blob/master/belt-hash/README.md Demonstrates how to use the `BeltHash` struct to compute the hash of a given byte slice. It also shows how to finalize the hash and encode it into a hexadecimal string using the `base16ct` crate. ```rust use belt_hash::{BeltHash, Digest}; use hex_literal::hex; let mut hasher = BeltHash::new(); hasher.update(b"hello world"); let hash = hasher.finalize(); assert_eq!(hash, hex!("afb175816416fbadad4629ecbd78e1887789881f2d2e5b80c22a746b7ac7ba88")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "afb175816416fbadad4629ecbd78e1887789881f2d2e5b80c22a746b7ac7ba88"); ``` -------------------------------- ### Whirlpool Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/whirlpool/README.md Demonstrates how to use the `Whirlpool` struct to compute the hash of a byte slice. It initializes a new hasher, updates it with data, finalizes the hash, and asserts the result against a known hexadecimal value. It also shows how to encode the hash to a hex string using the `base16ct` crate. ```rust use whirlpool::{Whirlpool, Digest}; use hex_literal::hex; let mut hasher = Whirlpool::new(); hasher.update(b"Hello Whirlpool"); let hash = hasher.finalize(); assert_eq!(hash, hex!( "8eaccdc136903c458ea0b1376be2a5fc9dc5b8ce8892a3b4f43366e2610c206c" "a373816495e63db0fff2ff25f75aa7162f332c9f518c3036456502a8414d300a" )); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!( hex_hash, "8eaccdc136903c458ea0b1376be2a5fc9dc5b8ce8892a3b4f43366e2610c206c" "a373816495e63db0fff2ff25f75aa7162f332c9f518c3036456502a8414d300a", ); ``` -------------------------------- ### Kupyna256 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/kupyna/README.md Demonstrates how to use the Kupyna256 hasher to compute the hash of a message. It initializes the hasher, updates it with the message bytes, finalizes the hash computation, and asserts the resulting hash value against a known constant. It also shows how to hex-encode the hash using the `base16ct` crate. ```rust use hex_literal::hex; use kupyna::{Digest, Kupyna256}; let mut hasher = Kupyna256::default(); hasher.update(b"my message"); let hash = hasher.finalize(); assert_eq!(hash, hex!("538e2e238142df05e954702aa75d6942cebe30d44bd514df365d13bdcb6b1458")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "538e2e238142df05e954702aa75d6942cebe30d44bd514df365d13bdcb6b1458"); ``` -------------------------------- ### SHA3-256 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/sha3/README.md Demonstrates how to use the Sha3_256 hasher from the sha3 crate to compute the hash of a byte string. It utilizes the `Digest` trait for updating and finalizing the hash. The output hash is then compared against a known hex literal. ```rust use hex_literal::hex; use sha3::{Digest, Sha3_256}; let mut hasher = Sha3_256::new(); hasher.update(b"abc"); let hash = hasher.finalize(); assert_eq!(hash, hex!("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"); ``` -------------------------------- ### Grøstl256 Hashing Example Source: https://github.com/rustcrypto/hashes/blob/master/groestl/README.md Demonstrates how to use the Groestl256 hasher to compute the hash of a message. It includes updating the hasher with data, finalizing the hash, and comparing it with an expected hex value. It also shows how to hex-encode the resulting hash using the base16ct crate. ```rust use groestl::{Digest, Groestl256}; use hex_literal::hex; let mut hasher = Groestl256::default(); hasher.update(b"my message"); let hash = hasher.finalize(); assert_eq!(hash, hex!("dc0283ca481efa76b7c19dd5a0b763dff0e867451bd9488a9c59f6c8b8047a86")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "dc0283ca481efa76b7c19dd5a0b763dff0e867451bd9488a9c59f6c8b8047a86"); ``` -------------------------------- ### AsconXof128 Extendable Output Hashing Source: https://github.com/rustcrypto/hashes/blob/master/ascon-hash/README.md Demonstrates how to use AsconXof128 for extendable output hashing (XOF). It initializes an XOF object, updates it with input bytes, finalizes it to get a reader, and reads a specified number of bytes from the reader. ```rust use ascon_hash::{AsconXof128, ExtendableOutput, Update, XofReader}; use hex_literal::hex; let mut xof = AsconXof128::default(); xof.update(b"some bytes"); let mut reader = xof.finalize_xof(); let mut dst = [0u8; 5]; reader.read(&mut dst); assert_eq!(dst, hex!("8c7dd114a0")); ``` -------------------------------- ### Generic Hashing with `Digest` Trait Source: https://github.com/rustcrypto/hashes/blob/master/README.md This example shows how to create a generic hashing function that works with any type implementing the `Digest` trait. It takes a password, salt, and an output buffer, updating the hasher with the provided data and finalizing the hash. Dependencies include `sha2` and the `digest` crate. ```rust use sha2::{Sha256, Sha512, Digest}; // Toy example, do not use it in practice! // Instead use crates from: https://github.com/RustCrypto/password-hashing fn hash_password(password: &str, salt: &str, output: &mut [u8]) { let mut hasher = D::new(); hasher.update(password.as_bytes()); hasher.update(b"$"); hasher.update(salt.as_bytes()); output.copy_from_slice(&hasher.finalize()) } let mut buf1 = [0u8; 32]; hash_password::("my_password", "abcd", &mut buf1); let mut buf2 = [0u8; 64]; hash_password::("my_password", "abcd", &mut buf2); ``` -------------------------------- ### SHA-1 Checked: Incremental API Example Source: https://github.com/rustcrypto/hashes/blob/master/sha1-checked/README.md Illustrates the incremental hashing API provided by the sha1_checked crate. This allows hashing data in chunks by updating the hasher iteratively and then finalizing the hash computation. It also includes collision detection. ```rust use hex_literal::hex; use sha1_checked::{Sha1, Digest}; let mut hasher = Sha1::new(); hasher.update(b"hello world"); let result = hasher.try_finalize(); assert_eq!(result.hash().as_ref(), hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")); assert!(!result.has_collision()); ``` -------------------------------- ### Dynamic Hashing with `DynDigest` Trait Source: https://github.com/rustcrypto/hashes/blob/master/README.md This example demonstrates using the `DynDigest` trait for dynamic hash function selection. The `use_hasher` function updates and finalizes a given hasher, while `select_hasher` allows choosing a specific hash algorithm (MD5, SHA1, SHA256, etc.) based on a string input. This is useful for scenarios with user-defined or dynamically determined hash algorithms. It requires the `digest` crate. ```rust use digest::DynDigest; // Dynamic hash function fn use_hasher(hasher: &mut dyn DynDigest, data: &[u8]) -> Box<[u8]> { hasher.update(data); hasher.finalize_reset() } // You can use something like this when parsing user input, CLI arguments, etc. // DynDigest needs to be boxed here, since function return should be sized. fn select_hasher(s: &str) -> Box { match s { "md5" => Box::new(md5::Md5::default()), "sha1" => Box::new(sha1::Sha1::default()), "sha224" => Box::new(sha2::Sha224::default()), "sha256" => Box::new(sha2::Sha256::default()), "sha384" => Box::new(sha2::Sha384::default()), "sha512" => Box::new(sha2::Sha512::default()), _ => unimplemented!("unsupported digest: {}", s), } } let mut hasher1 = select_hasher("md5"); let mut hasher2 = select_hasher("sha512"); // the `&mut *hasher` is to DerefMut the value out of the Box // this is equivalent to `DerefMut::deref_mut(&mut hasher)` // can be reused due to `finalize_reset()` let hash1_1 = use_hasher(&mut *hasher1, b"foo"); let hash1_2 = use_hasher(&mut *hasher1, b"bar"); let hash2_1 = use_hasher(&mut *hasher2, b"foo"); ``` -------------------------------- ### SHA-1 Checked: One-shot API Example Source: https://github.com/rustcrypto/hashes/blob/master/sha1-checked/README.md Demonstrates the one-shot hashing API of the sha1_checked crate. It computes the SHA-1 hash of a given byte slice and checks for collisions. The resulting hash can be encoded to a hexadecimal string. ```rust use hex_literal::hex; use sha1_checked::Sha1; let result = Sha1::try_digest(b"hello world"); assert_eq!(result.hash().as_ref(), hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")); assert!(!result.has_collision()); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(result.hash().as_ref()); assert_eq!(hex_hash, "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"); ``` -------------------------------- ### BLAKE2 Variable Output Size Hashing (Runtime) Source: https://github.com/rustcrypto/hashes/blob/master/blake2/README.md Demonstrates hashing with BLAKE2 where the output size is determined at runtime using `Blake2bVar`. It shows how to initialize the hasher with a specific byte size and finalize it into a provided buffer. ```rust use blake2::Blake2bVar; use blake2::digest::{Update, VariableOutput}; use hex_literal::hex; let mut hasher = Blake2bVar::new(10).unwrap(); hasher.update(b"my_input"); let mut buf = [0u8; 10]; hasher.finalize_variable(&mut buf).unwrap(); assert_eq!(buf, hex!("2cc55c84e416924e6400")); ``` -------------------------------- ### RustCrypto Hashes Algorithms Overview Source: https://github.com/rustcrypto/hashes/blob/master/README.md This section provides an overview of the cryptographic hash algorithms supported by the RustCrypto project, with links to their respective Wikipedia pages or specifications. ```Rust [Ascon] [BelT] [BLAKE2] [FSB] [GOST94] [Grøstl] [JH] [KangarooTwelve] [Kupyna] [MD2] [MD4] [MD5] [RIPEMD] [SHA-1] [SHA-1 Checked] [SHA-2] [SHA-3] [SHABAL] [Skein] [SM3] [Streebog] [Whirlpool] [Tiger] ``` -------------------------------- ### RustCrypto Hashes Crate List Source: https://github.com/rustcrypto/hashes/blob/master/README.md This section lists the various hash algorithm crates available within the RustCrypto hashes project. Each entry links to the specific crate for that algorithm. ```Rust `ascon‑hash` `belt‑hash` `blake2` `fsb` `gost94` `groestl` `jh` `k12` `kupyna` `md2` `md4` `md5` `ripemd` `sha1` `sha1-checked` `sha2` `sha3` `shabal` `skein` `sm3` `streebog` `tiger` `whirlpool` ``` -------------------------------- ### BLAKE2 Variable Output Size Hashing (Compile Time) Source: https://github.com/rustcrypto/hashes/blob/master/blake2/README.md Demonstrates hashing with BLAKE2 where the output size is determined at compile time using a type alias. This approach uses `Blake2b` to specify a 10-byte output size. ```rust use blake2::{Blake2b, Digest, digest::consts::U10}; use hex_literal::hex; type Blake2b80 = Blake2b; let mut hasher = Blake2b80::new(); hasher.update(b"my_input"); let res = hasher.finalize(); assert_eq!(res, hex!("2cc55c84e416924e6400")); ``` -------------------------------- ### SHA-1 Incremental API Source: https://github.com/rustcrypto/hashes/blob/master/sha1/README.md Calculates the SHA-1 hash of data incrementally. This API is useful for hashing large amounts of data or data that arrives in chunks. It requires initializing a hasher, updating it with data, and then finalizing to get the hash. It depends on the `sha1` and `hex-literal` crates, and optionally `base16ct` for hex encoding. ```rust use hex_literal::hex; use sha1::{Sha1, Digest}; let mut hasher = Sha1::new(); hasher.update(b"hello world"); let hash = hasher.finalize(); assert_eq!(hash, hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"); ``` -------------------------------- ### Cargo.toml Configuration for SHA-2 Source: https://github.com/rustcrypto/hashes/blob/master/README.md Shows how to add the `sha2` crate to your project's `Cargo.toml` file. It also demonstrates how to disable the default `std` feature for `no_std` environments. ```toml [dependencies] sha2 = "0.10" ``` ```toml [dependencies] sha2 = { version = "0.10", default-features = false } ``` -------------------------------- ### Incremental Hashing with SHA-256 and SHA-512 Source: https://github.com/rustcrypto/hashes/blob/master/sha2/README.md Illustrates the incremental hashing API for both SHA-256 and SHA-512. It shows how to create a hasher, update it with data in chunks, and finalize the hash computation. Assertions are included to verify the results. ```rust use sha2::{Sha256, Sha512, Digest}; use hex_literal::hex; let mut hasher = Sha256::new(); hasher.update(b"hello "); hasher.update(b"world"); let hash256 = hasher.finalize(); assert_eq!(hash256, hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")); let mut hasher = Sha512::new(); hasher.update(b"hello world"); let hash512 = hasher.finalize(); assert_eq!(hash512, hex!( "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f" "989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f" )); ``` -------------------------------- ### JH Hash Function Usage Source: https://github.com/rustcrypto/hashes/blob/master/jh/README.md Demonstrates how to use the JH hash function in Rust. It includes initializing a hasher, updating it with data, finalizing the hash, and comparing it with an expected value. It also shows how to hex-encode the resulting hash. ```rust use jh::{Digest, Jh256}; use hex_literal::hex; let mut hasher = Jh256::new(); hasher.update(b"hello"); let hash = hasher.finalize(); assert_eq!(hash, hex!("94fd3f4c564957c6754265676bf8b244c707d3ffb294e18af1f2e4f9b8306089")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "94fd3f4c564957c6754265676bf8b244c707d3ffb294e18af1f2e4f9b8306089"); ``` -------------------------------- ### BLAKE2 Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the BLAKE2 algorithm. Recommended for new applications. ```Rust use blake2::Blake2; let mut hasher = Blake2::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### RustCrypto Hashes Dependencies Source: https://github.com/rustcrypto/hashes/blob/master/README.md This section highlights key dependencies used across the RustCrypto hashes crates, such as the `digest` trait for hashing operations and `generic-array` for fixed-size arrays. ```Rust [`base16ct`] [`base64ct`] [`digest`] [`Digest`] [`Digest::digest`] [`DynDigest`] [`generic-array`] [`hmac`] [`Read`] [`Write`] ``` -------------------------------- ### Ascon Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the Ascon algorithm. Recommended for new applications. ```Rust use ascon_hash::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### One-shot Hashing with SHA-256 Source: https://github.com/rustcrypto/hashes/blob/master/sha2/README.md Demonstrates how to compute a SHA-256 hash of a byte slice using the one-shot API. It includes an assertion to verify the computed hash against a known value and shows how to hex-encode the result. ```rust use sha2::{Sha256, Digest}; use hex_literal::hex; let hash = Sha256::digest(b"hello world"); assert_eq!(hash, hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"); ``` -------------------------------- ### AsconHash256 Fixed Output Hashing Source: https://github.com/rustcrypto/hashes/blob/master/ascon-hash/README.md Demonstrates how to use AsconHash256 for fixed-size hashing. It initializes a hasher, updates it with input bytes, finalizes the hash, and asserts the expected output. It also shows how to hex-encode the resulting hash. ```rust use ascon_hash::{AsconHash256, Digest}; use hex_literal::hex; let mut hasher = AsconHash256::new(); hasher.update(b"some bytes"); let hash = hasher.finalize(); assert_eq!(hash, hex!("e909c2f6da9cb3028423265c8f23fc2d26bfc0f3db704683ef16b787a945ed68")); // Hex-encode hash using https://docs.rs/base16ct let hex_hash = base16ct::lower::encode_string(&hash); assert_eq!(hex_hash, "e909c2f6da9cb3028423265c8f23fc2d26bfc0f3db704683ef16b787a945ed68"); ``` -------------------------------- ### RIPEMD Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the RIPEMD algorithm. ```Rust use ripemd::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### Kupyna Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the Kupyna algorithm. ```Rust use kupyna::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### HMAC Usage Source: https://github.com/rustcrypto/hashes/blob/master/README.md Explains how to calculate Hash-based Message Authentication Code (HMAC) using the generic implementation from the `hmac` crate, which is part of the RustCrypto/MACs repository. ```rust use sha2::{Sha256, Digest}; use hmac::{Hmac, Mac}; type HmacSha256 = Hmac; let key = b"secret key"; let mut mac = HmacSha256::new_from_slice(key) .expect("HMAC can take key of any size"); mac.update(b"very secret data"); let result = mac.finalize(); let tag = result.into_bytes(); ``` -------------------------------- ### FSB Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the FSB algorithm. ```Rust use fsb::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### BelT Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the BelT algorithm. ```Rust use belt_hash::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### SHA-2 Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the SHA-2 algorithm. Recommended for new applications. ```Rust use sha2::Sha2; let mut hasher = Sha2::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### KangarooTwelve Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the KangarooTwelve algorithm. ```Rust use k12::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### SHA-3 Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the SHA-3 (Keccak) algorithm. Recommended for new applications. ```Rust use sha3::Sha3; let mut hasher = Sha3::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### MD5 Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the MD5 algorithm. Marked as broken. ```Rust use md_5::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### Grøstl Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the Grøstl (Groestl) algorithm. ```Rust use groestl::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### MD2 Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the MD2 algorithm. Marked as broken. ```Rust use md2::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ``` -------------------------------- ### GOST R 34.11-94 Hash Source: https://github.com/rustcrypto/hashes/blob/master/README.md Provides hashing functionality for the GOST R 34.11-94 algorithm. ```Rust use gost94::Hash; let mut hasher = Hash::new(); hasher.update(b"hello world"); let result = hasher.finalize(); ```