/src/libavif/build/_deps/fuzztest-src/common/hash.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2022 The Centipede Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "./common/hash.h" |
16 | | |
17 | | #include <cstddef> |
18 | | #include <cstdint> |
19 | | #include <string> |
20 | | #include <string_view> |
21 | | |
22 | | #include "./common/defs.h" |
23 | | #include "./common/sha1.h" |
24 | | |
25 | | namespace centipede { |
26 | | |
27 | 0 | std::string Hash(ByteSpan span) { |
28 | | // Compute SHA1. |
29 | 0 | uint8_t sha1[kShaDigestLength]; |
30 | 0 | SHA1(span.data(), span.size(), sha1); |
31 | | // Convert SHA1 to text. |
32 | 0 | static_assert(kHashLen == 2 * kShaDigestLength); |
33 | 0 | char sha1_hex_text[kHashLen]; |
34 | 0 | static const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', |
35 | 0 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
36 | 0 | for (size_t i = 0; i < kShaDigestLength; i++) { |
37 | 0 | sha1_hex_text[i * 2 + 0] = hex[sha1[i] / 16]; |
38 | 0 | sha1_hex_text[i * 2 + 1] = hex[sha1[i] % 16]; |
39 | 0 | } |
40 | 0 | return {sha1_hex_text, sha1_hex_text + kHashLen}; |
41 | 0 | } |
42 | | |
43 | 0 | std::string Hash(std::string_view str) { |
44 | 0 | static_assert(sizeof(decltype(str)::value_type) == sizeof(uint8_t)); |
45 | 0 | return Hash(AsByteSpan(str)); |
46 | 0 | } |
47 | | |
48 | | } // namespace centipede |