nu_command/hash/
md5.rs

1use super::generic_digest::{GenericDigest, HashDigest};
2use ::md5::Md5;
3use nu_protocol::{Example, Span, Value};
4
5pub type HashMd5 = GenericDigest<Md5>;
6
7impl HashDigest for Md5 {
8    fn name() -> &'static str {
9        "md5"
10    }
11
12    fn examples() -> Vec<Example<'static>> {
13        vec![
14            Example {
15                description: "Return the md5 hash of a string, hex-encoded",
16                example: "'abcdefghijklmnopqrstuvwxyz' | hash md5",
17                result: Some(Value::string(
18                    "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
19                    Span::test_data(),
20                )),
21            },
22            Example {
23                description: "Return the md5 hash of a string, as binary",
24                example: "'abcdefghijklmnopqrstuvwxyz' | hash md5 --binary",
25                result: Some(Value::binary(
26                    vec![
27                        0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c,
28                        0xca, 0x67, 0xe1, 0x3b,
29                    ],
30                    Span::test_data(),
31                )),
32            },
33            Example {
34                description: "Return the md5 hash of a file's contents",
35                example: "open ./nu_0_24_1_windows.zip | hash md5",
36                result: None,
37            },
38        ]
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::hash::generic_digest::{self, Arguments};
46
47    #[test]
48    fn test_examples() {
49        crate::test_examples(HashMd5::default())
50    }
51
52    #[test]
53    fn hash_string() {
54        let binary = Value::string("abcdefghijklmnopqrstuvwxyz".to_owned(), Span::test_data());
55        let expected = Value::string(
56            "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
57            Span::test_data(),
58        );
59        let actual = generic_digest::action::<Md5>(
60            &binary,
61            &Arguments {
62                cell_paths: None,
63                binary: false,
64            },
65            Span::test_data(),
66        );
67        assert_eq!(actual, expected);
68    }
69
70    #[test]
71    fn hash_bytes() {
72        let binary = Value::binary(vec![0xC0, 0xFF, 0xEE], Span::test_data());
73        let expected = Value::string(
74            "5f80e231382769b0102b1164cf722d83".to_owned(),
75            Span::test_data(),
76        );
77        let actual = generic_digest::action::<Md5>(
78            &binary,
79            &Arguments {
80                cell_paths: None,
81                binary: false,
82            },
83            Span::test_data(),
84        );
85        assert_eq!(actual, expected);
86    }
87}