ts_Sha2.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 
24 #include <icl_core_crypt/Sha2.h>
25 
26 #include <boost/test/unit_test.hpp>
27 
32 
33 BOOST_AUTO_TEST_SUITE(ts_Sha2)
34 
35 //----------------------------------------------------------------------
36 // Empty strings
37 //----------------------------------------------------------------------
38 
39 BOOST_AUTO_TEST_CASE(EmptyStringDigestSha224)
40 {
41  Sha224 sha;
42  sha.finalize();
43  BOOST_CHECK_EQUAL(sha.getHexDigest(), "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f");
44 }
45 
46 BOOST_AUTO_TEST_CASE(EmptyStringDigestSha256)
47 {
48  Sha256 sha;
49  sha.finalize();
50  BOOST_CHECK_EQUAL(sha.getHexDigest(), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
51 }
52 
53 BOOST_AUTO_TEST_CASE(EmptyStringDigestSha384)
54 {
55  Sha384 sha;
56  sha.finalize();
57  BOOST_CHECK_EQUAL(sha.getHexDigest(), "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b");
58 }
59 
60 BOOST_AUTO_TEST_CASE(EmptyStringDigestSha512)
61 {
62  Sha512 sha;
63  sha.finalize();
64  BOOST_CHECK_EQUAL(sha.getHexDigest(), "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
65 }
66 
67 //----------------------------------------------------------------------
68 // The quick brown fox
69 //----------------------------------------------------------------------
70 
71 const char *teststring1 = "The quick brown fox jumps over the lazy dog";
72 const char *teststring2 = "The quick brown fox jumps over the lazy dog.";
73 
74 BOOST_AUTO_TEST_CASE(SimpleStringDigestSha224)
75 {
76  Sha224 sha;
77  sha.process(teststring1);
78  sha.finalize();
79  BOOST_CHECK_EQUAL(sha.getHexDigest(), "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525");
80  sha.clear();
81  sha.process(teststring2);
82  sha.finalize();
83  BOOST_CHECK_EQUAL(sha.getHexDigest(), "619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c");
84 }
85 
86 BOOST_AUTO_TEST_CASE(SimpleStringDigestSha256)
87 {
88  Sha256 sha;
89  sha.process(teststring1);
90  sha.finalize();
91  BOOST_CHECK_EQUAL(sha.getHexDigest(), "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
92  sha.clear();
93  sha.process(teststring2);
94  sha.finalize();
95  BOOST_CHECK_EQUAL(sha.getHexDigest(), "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
96 }
97 
98 BOOST_AUTO_TEST_CASE(SimpleStringDigestSha384)
99 {
100  Sha384 sha;
101  sha.process(teststring1);
102  sha.finalize();
103  BOOST_CHECK_EQUAL(sha.getHexDigest(), "ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1");
104  sha.clear();
105  sha.process(teststring2);
106  sha.finalize();
107  BOOST_CHECK_EQUAL(sha.getHexDigest(), "ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7");
108 }
109 
110 BOOST_AUTO_TEST_CASE(SimpleStringDigestSha512)
111 {
112  Sha512 sha;
113  sha.process(teststring1);
114  sha.finalize();
115  BOOST_CHECK_EQUAL(sha.getHexDigest(), "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6");
116  sha.clear();
117  sha.process(teststring2);
118  sha.finalize();
119  BOOST_CHECK_EQUAL(sha.getHexDigest(), "91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed");
120 }
121 
122 //----------------------------------------------------------------------
123 // Longer string consisting of more than one message block
124 //----------------------------------------------------------------------
125 
126 const char *longstring =
127  "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam "
128  "lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam "
129  "viverra nec consectetur ante hendrerit. Donec et mollis "
130  "dolor. Praesent et diam eget libero egestas mattis sit amet vitae "
131  "augue. Nam tincidunt congue enim, ut porta lorem lacinia "
132  "consectetur. Donec ut libero sed arcu vehicula ultricies a non "
133  "tortor. Lorem ipsum dolor sit amet, consectetur adipiscing "
134  "elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper "
135  "sed, adipiscing id dolor. Pellentesque auctor nisi id magna "
136  "consequat sagittis. Curabitur dapibus enim sit amet elit pharetra "
137  "tincidunt feugiat nisl imperdiet. Ut convallis libero in urna "
138  "ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam "
139  "pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et "
140  "magnis dis parturient montes, nascetur ridiculus mus. In rutrum "
141  "accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in "
142  "est.\n"
143  "Vivamus fermentum semper porta. Nunc diam velit, adipiscing ut "
144  "tristique vitae, sagittis vel odio. Maecenas convallis ullamcorper "
145  "ultricies. Curabitur ornare, ligula semper consectetur sagittis, "
146  "nisi diam iaculis velit, id fringilla sem nunc vel mi. Nam dictum, "
147  "odio nec pretium volutpat, arcu ante placerat erat, non tristique "
148  "elit urna et turpis. Quisque mi metus, ornare sit amet fermentum "
149  "et, tincidunt et orci. Fusce eget orci a orci congue vestibulum. Ut "
150  "dolor diam, elementum et vestibulum eu, porttitor vel "
151  "elit. Curabitur venenatis pulvinar tellus gravida ornare. Sed et "
152  "erat faucibus nunc euismod ultricies ut id justo. Nullam cursus "
153  "suscipit nisi, et ultrices justo sodales nec. Fusce venenatis "
154  "facilisis lectus ac semper. Aliquam at massa ipsum. Quisque "
155  "bibendum purus convallis nulla ultrices ultricies. Nullam aliquam, "
156  "mi eu aliquam tincidunt, purus velit laoreet tortor, viverra "
157  "pretium nisi quam vitae mi. Fusce vel volutpat elit. Nam sagittis "
158  "nisi dui.\n";
159 
160 BOOST_AUTO_TEST_CASE(LongStringDigestSha224)
161 {
162  Sha224 sha;
163  sha.process(longstring);
164  sha.finalize();
165  BOOST_CHECK_EQUAL(sha.getHexDigest(), "9319b8ec0551910b30009ba504103bbdbb1e02ed8e9247320e01dccf");
166 }
167 
168 BOOST_AUTO_TEST_CASE(LongStringDigestSha256)
169 {
170  Sha256 sha;
171  sha.process(longstring);
172  sha.finalize();
173  BOOST_CHECK_EQUAL(sha.getHexDigest(), "93ba6b27717ba945b8f43e9ad1d44dc02123b13f9000eb4b25b8a153422b6677");
174 }
175 
176 BOOST_AUTO_TEST_CASE(LongStringDigestSha384)
177 {
178  Sha384 sha;
179  sha.process(longstring);
180  sha.finalize();
181  BOOST_CHECK_EQUAL(sha.getHexDigest(), "866e65e74170721a59ced427e884d1e25a1d62862387af2004d30549e3e05a34875283c2d1fd2aa13989fff8b6ecad9f");
182 }
183 
184 BOOST_AUTO_TEST_CASE(LongStringDigestSha512)
185 {
186  Sha512 sha;
187  sha.process(longstring);
188  sha.finalize();
189  BOOST_CHECK_EQUAL(sha.getHexDigest(), "aff2b51a8c5555603914381005f6f46bec85793610e89cf9a3517ff3b9e98c7e5e1a636f0b46b1288478d30ec83c434d5e07f86ca6ed8507a9a8b03d63d1f5c7");
190 }
191 
192 //----------------------------------------------------------------------
193 // String ending exactly one byte before the padding boundary
194 //----------------------------------------------------------------------
195 
196 const char *padstring256_440bit = "....:....|....:....|....:....|....:....|....:....|....:";
197 const char *padstring512_888bit = "....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|.";
198 
199 BOOST_AUTO_TEST_CASE(Pad440Sha224)
200 {
201  Sha224 sha;
202  sha.process(padstring256_440bit);
203  sha.finalize();
204  BOOST_CHECK_EQUAL(sha.getHexDigest(), "a526362dae0b4aa0fdd4966ed84d51f6ab3dbff574f75613e8f21ba2");
205 }
206 
207 BOOST_AUTO_TEST_CASE(Pad440Sha256)
208 {
209  Sha256 sha;
210  sha.process(padstring256_440bit);
211  sha.finalize();
212  BOOST_CHECK_EQUAL(sha.getHexDigest(), "d0c06dbf5fec4c438d6cacf1113435e7e504ed45c04efb5cb70776f1d391e4d9");
213 }
214 
215 BOOST_AUTO_TEST_CASE(Pad888Sha384)
216 {
217  Sha384 sha;
218  sha.process(padstring512_888bit);
219  sha.finalize();
220  BOOST_CHECK_EQUAL(sha.getHexDigest(), "ac807c831aa023556137b6aeaf0b1ec33ff24c99a858ffe97a1926dfd99f3f4cb81b0afd61ff29f01ea5f8851a22ee28");
221 }
222 
223 BOOST_AUTO_TEST_CASE(Pad888Sha512)
224 {
225  Sha512 sha;
226  sha.process(padstring512_888bit);
227  sha.finalize();
228  BOOST_CHECK_EQUAL(sha.getHexDigest(), "d3818ef0619071407cab75933bee598cf8b26976d3c51cade7bd86ebc951e5ce53cd18d3784f152bb2536622c6538331f42c2f44f811dd9a75ae345ec7dd060e");
229 }
230 
231 //----------------------------------------------------------------------
232 // String ending exactly on the padding boundary
233 //----------------------------------------------------------------------
234 
235 const char *padstring256_448bit = "....:....|....:....|....:....|....:....|....:....|....:.";
236 const char *padstring512_896bit = "....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|..";
237 
238 BOOST_AUTO_TEST_CASE(Pad448Sha224)
239 {
240  Sha224 sha;
241  sha.process(padstring256_448bit);
242  sha.finalize();
243  BOOST_CHECK_EQUAL(sha.getHexDigest(), "a95081d421a9576d6507b21bab994a139dab99241cc70a1bc4db8763");
244 }
245 
246 BOOST_AUTO_TEST_CASE(Pad448Sha256)
247 {
248  Sha256 sha;
249  sha.process(padstring256_448bit);
250  sha.finalize();
251  BOOST_CHECK_EQUAL(sha.getHexDigest(), "5f237fc93336874639ea7fe7a2f55e58b954946f90ab3254f6a93ad33562279f");
252 }
253 
254 BOOST_AUTO_TEST_CASE(Pad896Sha384)
255 {
256  Sha384 sha;
257  sha.process(padstring512_896bit);
258  sha.finalize();
259  BOOST_CHECK_EQUAL(sha.getHexDigest(), "c2343fcfe20cedf0ff1314b03002d710d963928687bcc29ca66ed4891bc8ccecb9601771fa8119241f756a7d55b4117b");
260 }
261 
262 BOOST_AUTO_TEST_CASE(Pad896Sha512)
263 {
264  Sha512 sha;
265  sha.process(padstring512_896bit);
266  sha.finalize();
267  BOOST_CHECK_EQUAL(sha.getHexDigest(), "ada0d27f302ed4fec585ee259b0590454e159d023befd97782c6fad093284463bc0c366acfed361359d6f63de07291e8039abbe0366c89ee4ad3d67e244fdd04");
268 }
269 
270 //----------------------------------------------------------------------
271 // String ending exactly one byte after the padding boundary
272 //----------------------------------------------------------------------
273 
274 const char *padstring256_456bit = "....:....|....:....|....:....|....:....|....:....|....:..";
275 const char *padstring512_904bit = "....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|....:....|...";
276 
277 BOOST_AUTO_TEST_CASE(Pad456Sha224)
278 {
279  Sha224 sha;
280  sha.process(padstring256_456bit);
281  sha.finalize();
282  BOOST_CHECK_EQUAL(sha.getHexDigest(), "d6c17098edf2463cd98f9f97c58fca8a881d0e83802193c81742129a");
283 }
284 
285 BOOST_AUTO_TEST_CASE(Pad456Sha256)
286 {
287  Sha256 sha;
288  sha.process(padstring256_456bit);
289  sha.finalize();
290  BOOST_CHECK_EQUAL(sha.getHexDigest(), "a1d509b8f4d8fac578e40b57273c8d665e2716fe2446b32e25d2d10eb218bd0e");
291 }
292 
293 BOOST_AUTO_TEST_CASE(Pad904Sha384)
294 {
295  Sha384 sha;
296  sha.process(padstring512_904bit);
297  sha.finalize();
298  BOOST_CHECK_EQUAL(sha.getHexDigest(), "392bc91f4da2a2f545f19a911fcf5072c8fa7a8909ed1ee9175d17af587f213a02c3a0acf8ee6868129b383c70bb2cc6");
299 }
300 
301 BOOST_AUTO_TEST_CASE(Pad904Sha512)
302 {
303  Sha512 sha;
304  sha.process(padstring512_904bit);
305  sha.finalize();
306  BOOST_CHECK_EQUAL(sha.getHexDigest(), "73ce0c347018a80cb9781cc0eb302e43b35384db671c512f31c369c89e2b4ad6ea1cfe59abcc3698b7c7bc01c776e67e804f93823730244a9620d6526baee3da");
307 }
308 
309 BOOST_AUTO_TEST_SUITE_END()
const char * longstring
Definition: ts_Sha2.cpp:126
Sha2< uint32_t, 0xc1059ed8ul, 0x367cd507ul, 0x3070dd17ul, 0xf70e5939ul, 0xffc00b31ul, 0x68581511ul, 0x64f98fa7ul, 0xbefa4fa4ul, 7 > Sha224
The SHA-224 hash algorithm.
Definition: Sha2.h:113
const char * teststring1
Definition: ts_Sha2.cpp:71
const char * padstring512_888bit
Definition: ts_Sha2.cpp:197
Sha2< uint64_t, 0x6a09e667f3bcc908ull, 0xbb67ae8584caa73bull, 0x3c6ef372fe94f82bull, 0xa54ff53a5f1d36f1ull, 0x510e527fade682d1ull, 0x9b05688c2b3e6c1full, 0x1f83d9abfb41bd6bull, 0x5be0cd19137e2179ull, 8 > Sha512
The SHA-512 hash algorithm.
Definition: Sha2.h:125
Sha2< uint32_t, 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul, 0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul, 8 > Sha256
The SHA-256 hash algorithm.
Definition: Sha2.h:117
const char * padstring512_904bit
Definition: ts_Sha2.cpp:275
const char * padstring512_896bit
Definition: ts_Sha2.cpp:236
BOOST_AUTO_TEST_CASE(EmptyStringDigestSha224)
Definition: ts_Sha2.cpp:39
const char * padstring256_456bit
Definition: ts_Sha2.cpp:274
const char * padstring256_448bit
Definition: ts_Sha2.cpp:235
const char * teststring2
Definition: ts_Sha2.cpp:72
const char * padstring256_440bit
Definition: ts_Sha2.cpp:196
Sha2< uint64_t, 0xcbbb9d5dc1059ed8ull, 0x629a292a367cd507ull, 0x9159015a3070dd17ull, 0x152fecd8f70e5939ull, 0x67332667ffc00b31ull, 0x8eb44a8768581511ull, 0xdb0c2e0d64f98fa7ull, 0x47b5481dbefa4fa4ull, 6 > Sha384
The SHA-384 hash algorithm.
Definition: Sha2.h:121


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58