2021-01-11 17:13:41 +00:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-21 12:26:41 +00:00
|
|
|
|
2020-10-13 23:52:49 +00: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 12:17:37 +00:00
|
|
|
for src in v {
|
2020-10-13 23:52:49 +00:00
|
|
|
ctx.update(src.as_ref());
|
2019-11-03 15:39:27 +00:00
|
|
|
}
|
|
|
|
let digest = ctx.finish();
|
2020-06-19 15:03:33 +00:00
|
|
|
let out: Vec<String> = digest
|
|
|
|
.as_ref()
|
|
|
|
.iter()
|
|
|
|
.map(|byte| format!("{:02x}", byte))
|
|
|
|
.collect();
|
|
|
|
out.join("")
|
2019-11-03 15:39:27 +00:00
|
|
|
}
|
2020-10-13 23:52:49 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gen() {
|
|
|
|
let actual = gen(&[b"hello world"]);
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|