Rust
Rust
Example: Hash Text in Chunks
Shows how to generate a final hash, like SHA-256, for a large text by processing it in chunks.Chilkat Rust Downloads
let crypt = chilkat::Crypt2::new();
crypt.set_hash_algorithm("sha256");
crypt.set_charset("utf-8");
let bd = chilkat::BinData::new();
let s = "The quick brown fox jumped over the lazy dog.\r\n".to_string();
// Accumulate the text in a StringBuilder. We'll demonstrate hashing the text in chunks,
// and then also hashing the entire text at once to show the results are the same.
let _ = bd.append_string(&s, "utf-8");
let _ = crypt.hash_begin_string(&s).is_ok();
let mut i = 0;
while i < 200 {
let _ = bd.append_string(&s, "utf-8");
let _ = crypt.hash_more_string(&s);
i = i + 1;
}
// Get the hash in base64 format.
crypt.set_encoding_mode("base64");
let mut encoded_hash = crypt.hash_final_enc().unwrap_or_default();
println!("Hash computed in chunks: {}", encoded_hash);
// Let's alternatively compute the hash of the entire amount of data at once,
// to show the hash computation is the same:
encoded_hash = crypt.hash_bd_enc(&bd).unwrap_or_default();
println!("Hash computed in 1 step: {}", encoded_hash);
// Output:
// Hash computed in chunks: unwkVff61k40roRIJizaknreScHaL6frWe37kydXbZQ=
// Hash computed in 1 step: unwkVff61k40roRIJizaknreScHaL6frWe37kydXbZQ=