19 CHECK(
sum.toString() ==
"0000000000000000000000000000000000000000");
22 SECTION(
"from string, ok") {
23 Sha1Sum sum(
"1234567890123456789012345678901234567890");
25 CHECK(
sum.toString() ==
"1234567890123456789012345678901234567890");
27 SECTION(
"from string, too short") {
28 CHECK_THROWS(
Sha1Sum(
"123456789012345678901234567890123456789"));
30 SECTION(
"from string, too long") {
31 CHECK_THROWS(
Sha1Sum(
"12345678901234567890123456789012345678901"));
33 SECTION(
"from string, invalid char") {
34 CHECK_THROWS(
Sha1Sum(
"g234567890123456789012345678901234567890"));
44 sum.
parse40(subspan<40>(
"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"));
45 CHECK(
sum.toString() ==
"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd");
47 SECTION(
"invalid char") {
48 CHECK_THROWS(
sum.parse40(subspan<40>(
"abcdabcdabcdabcdabcdabcdabcdabcd-bcdabcd")));
54 Sha1Sum sum(
"1111111111111111111111111111111111111111");
55 REQUIRE(!
sum.empty());
56 REQUIRE(
sum.toString() !=
"0000000000000000000000000000000000000000");
60 CHECK(
sum.toString() ==
"0000000000000000000000000000000000000000");
63static void testCompare(
const Sha1Sum& x,
const Sha1Sum& y,
bool expectEqual,
bool expectLess)
93 Sha1Sum sumA (
"0000000000000000000000000000000000000000");
94 Sha1Sum sumB (
"0000000000000000000000000000000000000001");
95 Sha1Sum sumB2(
"0000000000000000000000000000000000000001");
96 Sha1Sum sumC (
"0000000000100000000000000000000000000001");
98 testCompare(sumB, sumB2,
true,
false);
99 testCompare(sumA, sumB,
false,
true);
100 testCompare(sumC, sumB,
false,
false);
105 Sha1Sum sum(
"abcdef0123ABCDEF0123abcdef0123ABCDEF0123");
106 std::stringstream ss;
108 CHECK(ss.str() ==
"abcdef0123abcdef0123abcdef0123abcdef0123");
114 const char* in =
"abc";
116 CHECK(output.
toString() ==
"a9993e364706816aba3e25717850c26c9cd0d89d");
122 SECTION(
"single block") {
123 const char* in =
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
124 sha1.
update({std::bit_cast<const uint8_t*>(in), strlen(in)});
128 CHECK(sum1.
toString() ==
"84983e441c3bd26ebaae4aa1f95129e5e54670f1");
130 SECTION(
"multiple blocks") {
131 const char* in =
"aaaaaaaaaaaaaaaaaaaaaaaaa";
132 REQUIRE(strlen(in) == 25);
134 sha1.
update({std::bit_cast<const uint8_t*>(in), strlen(in)});
138 CHECK(
sum.toString() ==
"34aa973cd4c4daa4f61eeb2bdbad27316534016f");
146 const char* in =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
147 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
149 sha1.
update({std::bit_cast<const uint8_t*>(in), size_t(0)});
151 CHECK(
sum.toString() ==
"da39a3ee5e6b4b0d3255bfef95601890afd80709");
154 sha1.
update({std::bit_cast<const uint8_t*>(in), 25});
156 CHECK(
sum.toString() ==
"44f4647e1542a79d7d68ceb7f75d1dbf77fdebfc");
159 sha1.
update({std::bit_cast<const uint8_t*>(in), 55});
161 CHECK(
sum.toString() ==
"c1c8bbdc22796e28c0e15163d20899b65621d65a");
164 sha1.
update({std::bit_cast<const uint8_t*>(in), 56});
166 CHECK(
sum.toString() ==
"c2db330f6083854c99d4b5bfb6e8f29f201be699");
169 sha1.
update({std::bit_cast<const uint8_t*>(in), 60});
171 CHECK(
sum.toString() ==
"13d956033d9af449bfe2c4ef78c17c20469c4bf1");
174 sha1.
update({std::bit_cast<const uint8_t*>(in), 63});
176 CHECK(
sum.toString() ==
"03f09f5b158a7a8cdad920bddc29b81c18a551f5");
179 sha1.
update({std::bit_cast<const uint8_t*>(in), 64});
181 CHECK(
sum.toString() ==
"0098ba824b5c16427bd7a1122a5a442a25ec644d");
Helper class to perform a sha1 calculation.
Sha1Sum digest()
Get the final hash.
void update(std::span< const uint8_t > data)
Incrementally calculate the hash value.
static Sha1Sum calc(std::span< const uint8_t > data)
Easier to use interface, if you can pass all data in one go.
This class represents the result of a sha1 calculation (a 160-bit value).
void parse40(std::span< const char, 40 > str)
Parse from a 40-character long buffer.
std::string toString() const
This file implemented 3 utility functions:
constexpr auto sum(InputRange &&range, Proj proj={})
TEST_CASE("Sha1Sum: constructors")
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.