create a sha3_512 hash from a string in rust

To create a SHA3_512 hash from a string in Rust, you'll need to use the crypto and hex crates. Here's an example function that takes a &str input and returns a SHA3_512 hash as a String:

main.rs
use crypto::digest::Digest;
use crypto::sha3::Sha3;
use hex;

fn sha3_512(input: &str) -> String {
    let mut hasher = Sha3::sha3_512();
    hasher.input_str(input);
    let output = hasher.result_str();
    hex::encode(output)
}
231 chars
11 lines

Here's how you can use the sha3_512 function to generate a hash for a given string:

main.rs
fn main() {
    let hash = sha3_512("Hello, world!");
    println!("{}", hash); // e5b1f906bc7898d597f6f91dc7e702c96f4108bae00c07da5f5aa5da9c5a8db546b981c2847ddbcf6c61beb627b46496ee1c35e820227ea92ec21d8f06a6e47
}
213 chars
5 lines

gistlibby LogSnag