]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/test/core/sha1.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / Beast / test / core / sha1.cpp
1 //
2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7
8 #include <beast/core/detail/sha1.hpp>
9 #include <beast/unit_test/suite.hpp>
10 #include <array>
11
12 namespace beast {
13 namespace detail {
14
15 class sha1_test : public beast::unit_test::suite
16 {
17 public:
18 static
19 inline
20 std::uint8_t
21 unhex(char c)
22 {
23 if(c >= '0' && c <= '9')
24 return c - '0';
25 if(c >= 'a' && c <= 'f')
26 return c - 'a' + 10;
27 if(c >= 'A' && c <= 'F')
28 return c - 'A' + 10;
29 throw std::invalid_argument("not a hex digit");
30 }
31
32 static
33 std::string
34 unhex(std::string const& in)
35 {
36 std::string out;
37 out.reserve(in.size() / 2);
38 if(in.size() % 2)
39 throw std::domain_error("invalid hex string");
40 for(std::size_t i = 0; i < in.size(); i += 2)
41 out.push_back(
42 (unhex(in[i])<<4) + unhex(in[i+1]));
43 return out;
44 }
45
46 void
47 check(std::string const& message, std::string const& answer)
48 {
49 std::string digest;
50 digest = unhex(answer);
51 sha1_context ctx;
52 std::string result;
53 result.resize(sha1_context::digest_size);
54 init(ctx);
55 update(ctx, message.data(), message.size());
56 finish(ctx, &result[0]);
57 BEAST_EXPECT(result == digest);
58 }
59
60 void
61 run()
62 {
63 // http://www.di-mgt.com.au/sha_testvectors.html
64 //
65 check("abc",
66 "a9993e36" "4706816a" "ba3e2571" "7850c26c" "9cd0d89d");
67 check("",
68 "da39a3ee" "5e6b4b0d" "3255bfef" "95601890" "afd80709");
69 check("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
70 "84983e44" "1c3bd26e" "baae4aa1" "f95129e5" "e54670f1");
71 check("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
72 "a49b2446" "a02c645b" "f419f995" "b6709125" "3a04a259");
73 }
74 };
75
76 BEAST_DEFINE_TESTSUITE(sha1,core,beast);
77
78 } // test
79 } // beast
80