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 binary data",
35 example: "0x[deadbeef] | hash md5",
36 result: None,
37 },
38 Example {
39 description: "Return the md5 hash of a file's contents",
40 example: "open ./nu_0_24_1_windows.zip | hash md5",
41 result: None,
42 },
43 Example {
44 description: "Return the md5 hash of a list of strings",
45 example: "[abc def ghi] | hash md5",
46 result: Some(Value::list(
47 vec![
48 Value::string(
49 "900150983cd24fb0d6963f7d28e17f72".to_owned(),
50 Span::test_data(),
51 ),
52 Value::string(
53 "4ed9407630eb1000c0f6b63842defa7d".to_owned(),
54 Span::test_data(),
55 ),
56 Value::string(
57 "826bbc5d0522f5f20a1da4b60fa8c871".to_owned(),
58 Span::test_data(),
59 ),
60 ],
61 Span::test_data(),
62 )),
63 },
64 ]
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71 use crate::hash::generic_digest::{self, Arguments};
72
73 #[test]
74 fn test_examples() -> nu_test_support::Result {
75 nu_test_support::test().examples(HashMd5::default())
76 }
77
78 #[test]
79 fn hash_string() {
80 let binary = Value::string("abcdefghijklmnopqrstuvwxyz".to_owned(), Span::test_data());
81 let expected = Value::string(
82 "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
83 Span::test_data(),
84 );
85 let actual = generic_digest::action::<Md5>(
86 &binary,
87 &Arguments {
88 cell_paths: None,
89 binary: false,
90 },
91 Span::test_data(),
92 );
93 assert_eq!(actual, expected);
94 }
95
96 #[test]
97 fn hash_bytes() {
98 let binary = Value::binary(vec![0xC0, 0xFF, 0xEE], Span::test_data());
99 let expected = Value::string(
100 "5f80e231382769b0102b1164cf722d83".to_owned(),
101 Span::test_data(),
102 );
103 let actual = generic_digest::action::<Md5>(
104 &binary,
105 &Arguments {
106 cell_paths: None,
107 binary: false,
108 },
109 Span::test_data(),
110 );
111 assert_eq!(actual, expected);
112 }
113}