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 ]
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50 use crate::hash::generic_digest::{self, Arguments};
51
52 #[test]
53 fn test_examples() -> nu_test_support::Result {
54 nu_test_support::test().examples(HashMd5::default())
55 }
56
57 #[test]
58 fn hash_string() {
59 let binary = Value::string("abcdefghijklmnopqrstuvwxyz".to_owned(), Span::test_data());
60 let expected = Value::string(
61 "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
62 Span::test_data(),
63 );
64 let actual = generic_digest::action::<Md5>(
65 &binary,
66 &Arguments {
67 cell_paths: None,
68 binary: false,
69 },
70 Span::test_data(),
71 );
72 assert_eq!(actual, expected);
73 }
74
75 #[test]
76 fn hash_bytes() {
77 let binary = Value::binary(vec![0xC0, 0xFF, 0xEE], Span::test_data());
78 let expected = Value::string(
79 "5f80e231382769b0102b1164cf722d83".to_owned(),
80 Span::test_data(),
81 );
82 let actual = generic_digest::action::<Md5>(
83 &binary,
84 &Arguments {
85 cell_paths: None,
86 binary: false,
87 },
88 Span::test_data(),
89 );
90 assert_eq!(actual, expected);
91 }
92}