Files
simple-rust-tests/__crypto/crypto/src/main.rs
2020-10-17 11:47:07 +08:00

32 lines
686 B
Rust

use sha2::{Sha256, Sha512, Digest};
fn main() {
println!("{}", to_hex(&sha256(b"hello world")));
println!("{}", to_hex(&sha512(b"hello world")));
}
fn sha256(data: &[u8]) -> Vec<u8> {
let mut hasher = Sha256::new();
hasher.input(data);
let result = hasher.result();
result[..].to_vec()
}
fn sha512(data: &[u8]) -> Vec<u8> {
let mut hasher = Sha512::new();
hasher.input(data);
let result = hasher.result();
result[..].to_vec()
}
fn to_hex(bs: &[u8]) -> String {
bs.iter().map(|u| {
let x = format!("{:x}", u);
if x.len() == 1 {
"0".to_owned() + &x
} else {
x
}
}).collect()
}