1
+ #include < MD5Builder.h>
2
+
3
+ // Occasionally it is useful to compare a password that the user
4
+ // has entered to a build in string. However this means that the
5
+ // password ends up `in the clear' in the firmware and in your
6
+ // source code.
7
+ //
8
+ // MD5Builder helps you obfuscate this (it is not terribly secure, MD5
9
+ // has been phased out as insecure eons ago) by letting you create an
10
+ // MD5 of the data the user entered; and then compare this to an MD5
11
+ // string that you have put in your code.
12
+ //
13
+ void setup () {
14
+ Serial.begin (115200 );
15
+ delay (100 );
16
+ Serial.println (" \n\n\n Start." );
17
+
18
+ // Check if a password obfuscated in an MD5 actually
19
+ // matches the original string.
20
+ //
21
+ // echo -n "Hello World" | openssl md5
22
+ //
23
+ {
24
+ String md5 = " b10a8db164e0754105b7a99be72e3fe5" ;
25
+ String password = " Hello World" ;
26
+
27
+ MD5Builder md;
28
+
29
+ md.begin ();
30
+ md.add (password);
31
+ md.calculate ();
32
+
33
+ String result = md.toString ();
34
+
35
+ if (!md5.equalsIgnoreCase (result))
36
+ Serial.println (" Odd - failing MD5 on String" );
37
+ else
38
+ Serial.println (" OK!" );
39
+ }
40
+ // Check that this also work if we add the password not as
41
+ // a normal string - but as a string with the HEX values.
42
+ {
43
+ String passwordAsHex = " 48656c6c6f20576f726c64" ;
44
+ String md5 = " b10a8db164e0754105b7a99be72e3fe5" ;
45
+
46
+ MD5Builder md;
47
+
48
+ md.begin ();
49
+ md.addHexString (passwordAsHex);
50
+ md.calculate ();
51
+
52
+ String result = md.toString ();
53
+
54
+ if (!md5.equalsIgnoreCase (result)) {
55
+ Serial.println (" Odd - failing MD5 on hex string" );
56
+ Serial.println (md5);
57
+ Serial.println (result);
58
+ }
59
+ else
60
+ Serial.println (" OK!" );
61
+
62
+ }
63
+ // Check that this also work if we add the password as
64
+ // an unsigned byte array.
65
+ {
66
+ uint8_t password[] = { 0x48 , 0x65 , 0x6c , 0x6c , 0x6f , 0x20 , 0x57 , 0x6f , 0x72 , 0x6c , 0x64 };
67
+ String md5 = " b10a8db164e0754105b7a99be72e3fe5" ;
68
+ MD5Builder md;
69
+
70
+ md.begin ();
71
+ md.add (password, sizeof (password));
72
+ md.calculate ();
73
+
74
+ String result = md.toString ();
75
+
76
+ if (!md5.equalsIgnoreCase (result))
77
+ Serial.println (" Odd - failing MD5 on byte array" );
78
+ else
79
+ Serial.println (" OK!" );
80
+
81
+ // And also check that we can compare this as pure, raw, bytes
82
+ //
83
+ uint8_t raw[16 ] = { 0xb1 , 0x0a , 0x8d , 0xb1 , 0x64 , 0xe0 , 0x75 , 0x41 ,
84
+ 0x05 , 0xb7 , 0xa9 , 0x9b , 0xe7 , 0x2e , 0x3f , 0xe5
85
+ };
86
+ uint8_t res[16 ];
87
+ md.getBytes (res);
88
+ if (memcmp (raw, res, 16 ))
89
+ Serial.println (" Odd - failing MD5 on byte array when compared as bytes" );
90
+ else
91
+ Serial.println (" OK!" );
92
+
93
+ }
94
+ }
95
+
96
+ void loop () {
97
+ }
0 commit comments