create a sha512 hash from a string in rust

To create a SHA-512 hash from a string in Rust, you can use the ring cryptography library. Here's an example code snippet that demonstrates how to do this:

main.rs
use ring::digest;

fn sha512_hash(input: &str) -> String {
    let digest = digest::digest(&digest::SHA512, input.as_bytes());
    format!("{:x}", digest)
}

fn main() {
    let hash = sha512_hash("Hello, world!");
    println!("{}", hash);
}
243 chars
12 lines

In this example, the sha512_hash function takes a reference to a string as input and returns a string representing the SHA-512 hash of the input. The digest::digest function is used to compute the hash using the sha512 algorithm from the ring library. Finally, the hash is formatted as a string using the format macro and printed to the console.

gistlibby LogSnag