1#include <gtest/gtest.h>
2
3#include <base/hash_ctxt.h>
4#include <base/system.h>
5
6template<size_t BufferSize = SHA256_MAXSTRSIZE>
7static void ExpectSha256(SHA256_DIGEST Actual, const char *pWanted)
8{
9 char aActual[BufferSize];
10 sha256_str(Actual, aActual, sizeof(aActual));
11 EXPECT_STREQ(aActual, pWanted);
12}
13
14TEST(Hash, Sha256)
15{
16 // https://en.wikipedia.org/w/index.php?title=SHA-2&oldid=840187620#Test_vectors
17 ExpectSha256(Actual: sha256(message: "", message_len: 0), pWanted: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
18 SHA256_CTX ctxt;
19
20 sha256_init(ctxt: &ctxt);
21 ExpectSha256(Actual: sha256_finish(ctxt: &ctxt), pWanted: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
22
23 // printf 'The quick brown fox jumps over the lazy dog.' | sha256sum
24 char QUICK_BROWN_FOX[] = "The quick brown fox jumps over the lazy dog.";
25 ExpectSha256(Actual: sha256(message: QUICK_BROWN_FOX, message_len: str_length(str: QUICK_BROWN_FOX)), pWanted: "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
26
27 sha256_init(ctxt: &ctxt);
28 sha256_update(ctxt: &ctxt, data: "The ", data_len: 4);
29 sha256_update(ctxt: &ctxt, data: "quick ", data_len: 6);
30 sha256_update(ctxt: &ctxt, data: "brown ", data_len: 6);
31 sha256_update(ctxt: &ctxt, data: "fox ", data_len: 4);
32 sha256_update(ctxt: &ctxt, data: "jumps ", data_len: 6);
33 sha256_update(ctxt: &ctxt, data: "over ", data_len: 5);
34 sha256_update(ctxt: &ctxt, data: "the ", data_len: 4);
35 sha256_update(ctxt: &ctxt, data: "lazy ", data_len: 5);
36 sha256_update(ctxt: &ctxt, data: "dog.", data_len: 4);
37 ExpectSha256(Actual: sha256_finish(ctxt: &ctxt), pWanted: "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
38}
39
40TEST(Hash, Sha256ToStringSmallBuffer)
41{
42 char QUICK_BROWN_FOX[] = "The quick brown fox jumps over the lazy dog.";
43 ExpectSha256<1>(Actual: sha256(message: QUICK_BROWN_FOX, message_len: str_length(str: QUICK_BROWN_FOX)), pWanted: "");
44 ExpectSha256<16 + 1>(Actual: sha256(message: QUICK_BROWN_FOX, message_len: str_length(str: QUICK_BROWN_FOX)), pWanted: "ef537f25c895bfa7");
45}
46
47TEST(Hash, Sha256ToStringLargeBuffer)
48{
49 char QUICK_BROWN_FOX[] = "The quick brown fox jumps over the lazy dog.";
50 ExpectSha256<SHA256_MAXSTRSIZE + 64>(Actual: sha256(message: QUICK_BROWN_FOX, message_len: str_length(str: QUICK_BROWN_FOX)), pWanted: "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
51}
52
53TEST(Hash, Sha256Eq)
54{
55 EXPECT_EQ(sha256_comp(sha256("", 0), sha256("", 0)), 0);
56 EXPECT_TRUE(sha256("", 0) == sha256("", 0));
57 EXPECT_NE(sha256_comp(sha256("a", 1), sha256("b", 1)), 0);
58 EXPECT_TRUE(sha256("a", 1) != sha256("b", 1));
59}
60
61TEST(Hash, Sha256FromStr)
62{
63 SHA256_DIGEST Expected = {.data: {
64 0x01,
65 0x23,
66 0x45,
67 0x67,
68 0x89,
69 0x01,
70 0x23,
71 0x45,
72 0x67,
73 0x89,
74 0x01,
75 0x23,
76 0x45,
77 0x67,
78 0x89,
79 0x01,
80 0x23,
81 0x45,
82 0x67,
83 0x89,
84 0x01,
85 0x23,
86 0x45,
87 0x67,
88 0x89,
89 0x01,
90 0x23,
91 0x45,
92 0x67,
93 0x89,
94 0x01,
95 0x23,
96 }};
97 SHA256_DIGEST Sha256;
98 EXPECT_FALSE(sha256_from_str(&Sha256, "0123456789012345678901234567890123456789012345678901234567890123"));
99 EXPECT_EQ(Sha256, Expected);
100 EXPECT_TRUE(sha256_from_str(&Sha256, "012345678901234567890123456789012345678901234567890123456789012"));
101 EXPECT_TRUE(sha256_from_str(&Sha256, "01234567890123456789012345678901234567890123456789012345678901234"));
102 EXPECT_TRUE(sha256_from_str(&Sha256, ""));
103 EXPECT_TRUE(sha256_from_str(&Sha256, "012345678901234567890123456789012345678901234567890123456789012x"));
104 EXPECT_TRUE(sha256_from_str(&Sha256, "x123456789012345678901234567890123456789012345678901234567890123"));
105}
106
107template<size_t BufferSize = MD5_MAXSTRSIZE>
108static void ExpectMd5(MD5_DIGEST Actual, const char *pWanted)
109{
110 char aActual[BufferSize];
111 md5_str(Actual, aActual, sizeof(aActual));
112 EXPECT_STREQ(aActual, pWanted);
113}
114
115TEST(Hash, Md5)
116{
117 // https://en.wikipedia.org/w/index.php?title=MD5&oldid=889664074#MD5_hashes
118 ExpectMd5(Actual: md5(message: "", message_len: 0), pWanted: "d41d8cd98f00b204e9800998ecf8427e");
119 MD5_CTX ctxt;
120
121 md5_init(ctxt: &ctxt);
122 ExpectMd5(Actual: md5_finish(ctxt: &ctxt), pWanted: "d41d8cd98f00b204e9800998ecf8427e");
123
124 char QUICK_BROWN_FOX[] = "The quick brown fox jumps over the lazy dog.";
125 ExpectMd5(Actual: md5(message: QUICK_BROWN_FOX, message_len: str_length(str: QUICK_BROWN_FOX)), pWanted: "e4d909c290d0fb1ca068ffaddf22cbd0");
126
127 md5_init(ctxt: &ctxt);
128 md5_update(ctxt: &ctxt, data: "The ", data_len: 4);
129 md5_update(ctxt: &ctxt, data: "quick ", data_len: 6);
130 md5_update(ctxt: &ctxt, data: "brown ", data_len: 6);
131 md5_update(ctxt: &ctxt, data: "fox ", data_len: 4);
132 md5_update(ctxt: &ctxt, data: "jumps ", data_len: 6);
133 md5_update(ctxt: &ctxt, data: "over ", data_len: 5);
134 md5_update(ctxt: &ctxt, data: "the ", data_len: 4);
135 md5_update(ctxt: &ctxt, data: "lazy ", data_len: 5);
136 md5_update(ctxt: &ctxt, data: "dog.", data_len: 4);
137 ExpectMd5(Actual: md5_finish(ctxt: &ctxt), pWanted: "e4d909c290d0fb1ca068ffaddf22cbd0");
138}
139
140TEST(Hash, Md5ToStringSmallBuffer)
141{
142 char QUICK_BROWN_FOX[] = "The quick brown fox jumps over the lazy dog.";
143 ExpectMd5<1>(Actual: md5(message: QUICK_BROWN_FOX, message_len: str_length(str: QUICK_BROWN_FOX)), pWanted: "");
144 ExpectMd5<16 + 1>(Actual: md5(message: QUICK_BROWN_FOX, message_len: str_length(str: QUICK_BROWN_FOX)), pWanted: "e4d909c290d0fb1c");
145}
146
147TEST(Hash, Md5ToStringLargeBuffer)
148{
149 char QUICK_BROWN_FOX[] = "The quick brown fox jumps over the lazy dog.";
150 ExpectMd5<MD5_MAXSTRSIZE + 64>(Actual: md5(message: QUICK_BROWN_FOX, message_len: str_length(str: QUICK_BROWN_FOX)), pWanted: "e4d909c290d0fb1ca068ffaddf22cbd0");
151}
152
153TEST(Hash, Md5Eq)
154{
155 EXPECT_EQ(md5_comp(md5("", 0), md5("", 0)), 0);
156 EXPECT_TRUE(md5("", 0) == md5("", 0));
157 EXPECT_NE(md5_comp(md5("a", 1), md5("b", 1)), 0);
158 EXPECT_TRUE(md5("a", 1) != md5("b", 1));
159}
160
161TEST(Hash, Md5FromStr)
162{
163 MD5_DIGEST Expected = {.data: {
164 0x01,
165 0x23,
166 0x45,
167 0x67,
168 0x89,
169 0x01,
170 0x23,
171 0x45,
172 0x67,
173 0x89,
174 0x01,
175 0x23,
176 0x45,
177 0x67,
178 0x89,
179 0x01,
180 }};
181 MD5_DIGEST Md5;
182 EXPECT_FALSE(md5_from_str(&Md5, "01234567890123456789012345678901"));
183 EXPECT_EQ(Md5, Expected);
184 EXPECT_TRUE(md5_from_str(&Md5, "0123456789012345678901234567890"));
185 EXPECT_TRUE(md5_from_str(&Md5, "012345678901234567890123456789012"));
186 EXPECT_TRUE(md5_from_str(&Md5, ""));
187 EXPECT_TRUE(md5_from_str(&Md5, "0123456789012345678901234567890x"));
188 EXPECT_TRUE(md5_from_str(&Md5, "x1234567890123456789012345678901"));
189}
190