1 // This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
2 // the main distribution directory for license terms and copyright or visit
3 // https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
4 
5 #define CAF_SUITE detail.ripemd_160
6 
7 #include "caf/detail/ripemd_160.hpp"
8 
9 #include "caf/test/unit_test.hpp"
10 
11 #include <iomanip>
12 #include <iostream>
13 
14 using caf::detail::ripemd_160;
15 
16 namespace {
17 
str_hash(const std::string & what)18 std::string str_hash(const std::string& what) {
19   std::array<uint8_t, 20> hash;
20   ripemd_160(hash, what);
21   std::ostringstream oss;
22   oss << std::setfill('0') << std::hex;
23   for (auto i : hash) {
24     oss << std::setw(2) << static_cast<int>(i);
25   }
26   return oss.str();
27 }
28 
29 } // namespace
30 
31 // verify ripemd implementation with example hash results from
32 // http://homes.esat.kuleuven.be/~bosselae/ripemd160.html
CAF_TEST(hash_results)33 CAF_TEST(hash_results) {
34   CAF_CHECK_EQUAL("9c1185a5c5e9fc54612808977ee8f548b2258d31", str_hash(""));
35   CAF_CHECK_EQUAL("0bdc9d2d256b3ee9daae347be6f4dc835a467ffe", str_hash("a"));
36   CAF_CHECK_EQUAL("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc", str_hash("abc"));
37   CAF_CHECK_EQUAL("5d0689ef49d2fae572b881b123a85ffa21595f36",
38                   str_hash("message digest"));
39   CAF_CHECK_EQUAL("f71c27109c692c1b56bbdceb5b9d2865b3708dbc",
40                   str_hash("abcdefghijklmnopqrstuvwxyz"));
41   CAF_CHECK_EQUAL("12a053384a9c0c88e405a06c27dcf49ada62eb2b",
42                   str_hash("abcdbcdecdefdefgefghfghighij"
43                            "hijkijkljklmklmnlmnomnopnopq"));
44   CAF_CHECK_EQUAL("b0e20b6e3116640286ed3a87a5713079b21f5189",
45                   str_hash("ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
46                            "fghijklmnopqrstuvwxyz0123456789"));
47   CAF_CHECK_EQUAL("9b752e45573d4b39f4dbd3323cab82bf63326bfb",
48                   str_hash("1234567890123456789012345678901234567890"
49                            "1234567890123456789012345678901234567890"));
50 }
51