add ring::sha256

This commit is contained in:
2020-05-15 01:04:23 +08:00
parent b4c30ec23c
commit ce657011e8

View File

@@ -1,15 +1,26 @@
use ring::{hmac, rand, error::Unspecified};
use ring::{
hmac, rand, error::Unspecified,
digest,
};
fn main() -> Result<(), Unspecified> {
let rng = rand::SystemRandom::new();
let key = hmac::Key::generate(hmac::HMAC_SHA256, &rng)?;
{
println!("{} HHmac {}", "-".repeat(10), "-".repeat(10));
let rng = rand::SystemRandom::new();
let key = hmac::Key::generate(hmac::HMAC_SHA256, &rng)?;
let msg = "hello, world";
let tag = hmac::sign(&key, msg.as_bytes());
let msg = "hello, world";
let tag = hmac::sign(&key, msg.as_bytes());
println!("{:?}", tag);
hmac::verify(&key, msg.as_bytes(), tag.as_ref())?;
println!("Verify success");
println!("{:?}", tag);
hmac::verify(&key, msg.as_bytes(), tag.as_ref())?;
println!("Verify success");
}
{
println!("{} SHA256 {}", "-".repeat(10), "-".repeat(10));
let sha256 = digest::digest(&digest::SHA256, b"hello, world");
println!("{:?}", sha256);
}
Ok(())
}