SHA-256: Difference between revisions
Use the sha2 crate and split out the hex_string() function, which was misleading in a sha2() function.
(Added AWK solution) |
(Use the sha2 crate and split out the hex_string() function, which was misleading in a sha2() function.) |
||
Line 2,152:
=={{header|Rust}}==
<lang rust>
}
fn main() {
// create a Sha256 object
println!("{}", sha_256("Rosetta code"));▼
let mut hasher = Sha256::new();
// write input message
// read hash digest and consume hasher
let hex = hex_string(&result);
assert_eq!(
hex,
"764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf"
);
println!("{}", hex)
}
</lang>
{{out}}
▲fn sha_256(input: &str) -> String {
<pre>
▲ let result = digest(&SHA256, input.as_bytes());
764faf5c61ac315f1497f9dfa542713965b785e5cc2f707d6468d7d1124cdfcf
▲ result.as_ref().iter().map(|b| format!("{:x}", b)).collect()
=={{header|Scala}}==
|