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