2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-21 08:26:41 -04:00
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
use ring::digest::Context;
|
|
|
|
use ring::digest::SHA256;
|
|
|
|
|
2024-03-07 12:00:43 -05:00
|
|
|
/// Generate a SHA256 checksum of a slice of byte-slice-like things.
|
2020-10-13 19:52:49 -04:00
|
|
|
pub fn gen(v: &[impl AsRef<[u8]>]) -> String {
|
|
|
|
let mut ctx = Context::new(&SHA256);
|
2020-06-29 08:17:37 -04:00
|
|
|
for src in v {
|
2020-10-13 19:52:49 -04:00
|
|
|
ctx.update(src.as_ref());
|
2019-11-03 10:39:27 -05:00
|
|
|
}
|
2024-03-07 12:00:43 -05:00
|
|
|
faster_hex::hex_string(ctx.finish().as_ref())
|
2019-11-03 10:39:27 -05:00
|
|
|
}
|
2020-10-13 19:52:49 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gen() {
|
|
|
|
let actual = gen(&[b"hello world"]);
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|