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;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
let digest = ctx.finish();
|
2020-06-19 11:03:33 -04:00
|
|
|
let out: Vec<String> = digest
|
|
|
|
.as_ref()
|
|
|
|
.iter()
|
2023-01-27 10:43:16 -05:00
|
|
|
.map(|byte| format!("{byte:02x}"))
|
2020-06-19 11:03:33 -04:00
|
|
|
.collect();
|
|
|
|
out.join("")
|
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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|