1use super::generic_digest::{GenericDigest, HashDigest};
2use nu_protocol::{Example, Span, Value};
3use sha2::Sha256;
4
5pub type HashSha256 = GenericDigest<Sha256>;
6
7impl HashDigest for Sha256 {
8 fn name() -> &'static str {
9 "sha256"
10 }
11
12 fn examples() -> Vec<Example<'static>> {
13 vec![
14 Example {
15 description: "Return the sha256 hash of a string, hex-encoded",
16 example: "'abcdefghijklmnopqrstuvwxyz' | hash sha256",
17 result: Some(Value::string(
18 "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73".to_owned(),
19 Span::test_data(),
20 )),
21 },
22 Example {
23 description: "Return the sha256 hash of a string, as binary",
24 example: "'abcdefghijklmnopqrstuvwxyz' | hash sha256 --binary",
25 result: Some(Value::binary(
26 vec![
27 0x71, 0xc4, 0x80, 0xdf, 0x93, 0xd6, 0xae, 0x2f, 0x1e, 0xfa, 0xd1, 0x44,
28 0x7c, 0x66, 0xc9, 0x52, 0x5e, 0x31, 0x62, 0x18, 0xcf, 0x51, 0xfc, 0x8d,
29 0x9e, 0xd8, 0x32, 0xf2, 0xda, 0xf1, 0x8b, 0x73,
30 ],
31 Span::test_data(),
32 )),
33 },
34 Example {
35 description: "Return the sha256 hash of binary data",
36 example: "0x[deadbeef] | hash sha256",
37 result: None,
38 },
39 Example {
40 description: "Return the sha256 hash of a file's contents",
41 example: "open ./nu_0_24_1_windows.zip | hash sha256",
42 result: None,
43 },
44 Example {
45 description: "Return the sha256 hash of a list of strings",
46 example: "[abc def ghi] | hash sha256",
47 result: Some(Value::list(
48 vec![
49 Value::string(
50 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
51 .to_owned(),
52 Span::test_data(),
53 ),
54 Value::string(
55 "cb8379ac2098aa165029e3938a51da0bcecfc008fd6795f401178647f96c5b34"
56 .to_owned(),
57 Span::test_data(),
58 ),
59 Value::string(
60 "50ae61e841fac4e8f9e40baf2ad36ec868922ea48368c18f9535e47db56dd7fb"
61 .to_owned(),
62 Span::test_data(),
63 ),
64 ],
65 Span::test_data(),
66 )),
67 },
68 ]
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75 use crate::hash::generic_digest::{self, Arguments};
76
77 #[test]
78 fn test_examples() -> nu_test_support::Result {
79 nu_test_support::test().examples(HashSha256::default())
80 }
81
82 #[test]
83 fn hash_string() {
84 let binary = Value::string("abcdefghijklmnopqrstuvwxyz".to_owned(), Span::test_data());
85 let expected = Value::string(
86 "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73".to_owned(),
87 Span::test_data(),
88 );
89 let actual = generic_digest::action::<Sha256>(
90 &binary,
91 &Arguments {
92 cell_paths: None,
93 binary: false,
94 },
95 Span::test_data(),
96 );
97 assert_eq!(actual, expected);
98 }
99
100 #[test]
101 fn hash_bytes() {
102 let binary = Value::binary(vec![0xC0, 0xFF, 0xEE], Span::test_data());
103 let expected = Value::string(
104 "c47a10dc272b1221f0380a2ae0f7d7fa830b3e378f2f5309bbf13f61ad211913".to_owned(),
105 Span::test_data(),
106 );
107 let actual = generic_digest::action::<Sha256>(
108 &binary,
109 &Arguments {
110 cell_paths: None,
111 binary: false,
112 },
113 Span::test_data(),
114 );
115 assert_eq!(actual, expected);
116 }
117}