📅  最后修改于: 2023-12-03 15:04:57.719000             🧑  作者: Mango
Rust SHA2 is a Rust language implementation of the SHA-256 and SHA-512 hashing algorithms. These algorithms are commonly used to ensure the integrity and confidentiality of data.
To use Rust SHA2 in your Rust project, you can simply add it to your Cargo.toml file:
[dependencies]
sha2 = "0.9.5"
Once you've added SHA2 to your project, you can start hashing data. Here's an example of how to hash a string using SHA-256:
use sha2::{Sha256, Digest};
fn main() {
let mut hasher = Sha256::new();
hasher.update(b"hello world");
let result = hasher.finalize();
println!("{:x}", result);
}
This will output the SHA-256 hash of the string "hello world" in hexadecimal format.
You can also use SHA-512 by replacing Sha256
with Sha512
in the example above.
Using Rust SHA2, you can easily add SHA-256 and SHA-512 hashing functionality to your Rust projects. This can be helpful in ensuring the integrity and confidentiality of data in various applications.