nu_command/hash/
sha256.rs

1use super::generic_digest::{GenericDigest, HashDigest};
2use ::sha2::Sha256;
3use nu_protocol::{Example, Span, Value};
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 a file's contents",
36                example: "open ./nu_0_24_1_windows.zip | hash sha256",
37                result: None,
38            },
39        ]
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use crate::hash::generic_digest::{self, Arguments};
47
48    #[test]
49    fn test_examples() {
50        crate::test_examples(HashSha256::default())
51    }
52
53    #[test]
54    fn hash_string() {
55        let binary = Value::string("abcdefghijklmnopqrstuvwxyz".to_owned(), Span::test_data());
56        let expected = Value::string(
57            "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73".to_owned(),
58            Span::test_data(),
59        );
60        let actual = generic_digest::action::<Sha256>(
61            &binary,
62            &Arguments {
63                cell_paths: None,
64                binary: false,
65            },
66            Span::test_data(),
67        );
68        assert_eq!(actual, expected);
69    }
70
71    #[test]
72    fn hash_bytes() {
73        let binary = Value::binary(vec![0xC0, 0xFF, 0xEE], Span::test_data());
74        let expected = Value::string(
75            "c47a10dc272b1221f0380a2ae0f7d7fa830b3e378f2f5309bbf13f61ad211913".to_owned(),
76            Span::test_data(),
77        );
78        let actual = generic_digest::action::<Sha256>(
79            &binary,
80            &Arguments {
81                cell_paths: None,
82                binary: false,
83            },
84            Span::test_data(),
85        );
86        assert_eq!(actual, expected);
87    }
88}