GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/crypto/hash.h
Date: 2024-04-21 02:33:16
Exec Total Coverage
Lines: 172 175 98.3%
Branches: 94 122 77.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Provides a bit syntactic sugar around the hash algorithms.
5 * In particular, hashes can easily be created by constructors.
6 * Also, we have a little to-string-from-string conversion.
7 *
8 * The complexity is due to the need to avoid dynamically allocated memory
9 * for the hashes. Almost everything happens on the stack.
10 */
11
12 #ifndef CVMFS_CRYPTO_HASH_H_
13 #define CVMFS_CRYPTO_HASH_H_
14
15 #include <arpa/inet.h>
16 #include <stdint.h>
17
18 #include <cassert>
19 #include <cctype>
20 #include <cstdlib>
21 #include <cstring>
22 #include <string>
23
24 #include "util/export.h"
25 #include "util/logging.h"
26 #include "util/prng.h"
27 #include "util/smalloc.h"
28
29 #ifdef CVMFS_NAMESPACE_GUARD
30 namespace CVMFS_NAMESPACE_GUARD {
31 #endif
32
33 namespace shash {
34
35 /**
36 * Don't change order! The integer value of the enum constants is used
37 * as file catalog flags and as flags in communication with the cache manager.
38 * If algorithms are added, the protocol definition for external cache managers
39 * needs to be updated, too.
40 */
41 enum Algorithms {
42 kMd5 = 0,
43 kSha1,
44 kRmd160,
45 kShake128, // with 160 output bits
46 kAny,
47 };
48
49 /**
50 * NOTE: when adding a suffix here, one must edit `cvmfs_swissknife scrub`
51 * accordingly, that checks for invalid hash suffixes
52 */
53 const char kSuffixNone = 0;
54 const char kSuffixCatalog = 'C';
55 const char kSuffixHistory = 'H';
56 const char kSuffixMicroCatalog = 'L'; // currently unused
57 const char kSuffixPartial = 'P';
58 const char kSuffixTemporary = 'T';
59 const char kSuffixCertificate = 'X';
60 const char kSuffixMetainfo = 'M';
61
62
63 /**
64 * Corresponds to Algorithms. "Any" is the maximum of all the other
65 * digest sizes.
66 * When the maximum digest size changes, the memory layout of DirectoryEntry and
67 * PosixQuotaManager::LruCommand changes, too!
68 */
69 const unsigned kDigestSizes[] =
70 {16, 20, 20, 20, 20};
71 // Md5 Sha1 Rmd160 Shake128 Any
72 const unsigned kMaxDigestSize = 20;
73
74 /**
75 * The maximum of GetContextSize()
76 */
77 const unsigned kMaxContextSize = 256;
78
79 /**
80 * Hex representations of hashes with the same length need a suffix
81 * to be distinguished from each other. They should all have one but
82 * for backwards compatibility MD5 and SHA-1 have none. Initialized in hash.cc
83 * like const char *kAlgorithmIds[] = {"", "", "-rmd160", ...
84 */
85 CVMFS_EXPORT extern const char *kAlgorithmIds[];
86 const unsigned kAlgorithmIdSizes[] =
87 {0, 0, 7, 9, 0};
88 // Md5 Sha1 -rmd160 -shake128 Any
89 const unsigned kMaxAlgorithmIdentifierSize = 9;
90
91 /**
92 * Corresponds to Algorithms. There is no block size for Any.
93 * Is an HMAC for SHAKE well-defined?
94 */
95 const unsigned kBlockSizes[] =
96 {64, 64, 64, 168};
97 // Md5 Sha1 Rmd160 Shake128
98
99 /**
100 * Distinguishes between interpreting a string as hex hash and hashing over
101 * the contents of a string.
102 */
103 struct CVMFS_EXPORT HexPtr {
104 const std::string *str;
105 15103 explicit HexPtr(const std::string &s) { str = &s; }
106 bool IsValid() const;
107 };
108
109 struct CVMFS_EXPORT AsciiPtr {
110 const std::string *str;
111 1709 explicit AsciiPtr(const std::string &s) { str = &s; }
112 };
113
114 typedef char Suffix;
115
116 /**
117 * Holds a hash digest and provides from string / to string conversion and
118 * comparison. The kAny algorithm may not be used in functions! The algorithm
119 * has to be changed beforehand.
120 * This class is not used directly, but used as base clase of Md5, Sha1, ...
121 */
122 template<unsigned digest_size_, Algorithms algorithm_>
123 struct CVMFS_EXPORT Digest {
124 unsigned char digest[digest_size_];
125 Algorithms algorithm;
126 Suffix suffix;
127
128 class Hex {
129 public:
130 661905 explicit Hex(const Digest<digest_size_, algorithm_> *digest) :
131 661905 digest_(*digest),
132 661905 hash_length_(2 * kDigestSizes[digest_.algorithm]),
133 661905 algo_id_length_(kAlgorithmIdSizes[digest_.algorithm]) {}
134
135 54064630 unsigned int length() const { return hash_length_ + algo_id_length_; }
136
137 26376914 char operator[](const unsigned int position) const {
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26373346 times.
26376914 assert(position < length());
139 26376898 return (position < hash_length_)
140
2/2
✓ Branch 0 taken 26372494 times.
✓ Branch 1 taken 852 times.
26376898 ? GetHashChar(position)
141 26376173 : GetAlgorithmIdentifierChar(position);
142 }
143
144 protected:
145 26375760 char GetHashChar(const unsigned int position) const {
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26372208 times.
26375760 assert(position < hash_length_);
147
2/2
✓ Branch 0 taken 13192570 times.
✓ Branch 1 taken 13179638 times.
26375760 const char digit = (position % 2 == 0)
148 13194346 ? digest_.digest[position / 2] / 16
149 13181414 : digest_.digest[position / 2] % 16;
150 26375760 return ToHex(digit);
151 }
152
153 852 char GetAlgorithmIdentifierChar(const unsigned int position) const {
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 852 times.
852 assert(position >= hash_length_);
155 852 return kAlgorithmIds[digest_.algorithm][position - hash_length_];
156 }
157
158 26375911 char ToHex(const char c) const {
159
2/2
✓ Branch 0 taken 16740537 times.
✓ Branch 1 taken 9631822 times.
26375911 return static_cast<char>(c + ((c <= 9) ? '0' : 'a' - 10));
160 }
161
162 private:
163 const Digest<digest_size_, algorithm_> &digest_;
164 const unsigned int hash_length_;
165 const unsigned int algo_id_length_;
166 };
167
168 4380542 unsigned GetDigestSize() const { return kDigestSizes[algorithm]; }
169 66 unsigned GetHexSize() const {
170 66 return 2*kDigestSizes[algorithm] + kAlgorithmIdSizes[algorithm];
171 }
172
173 71506246 Digest() :
174 71506246 algorithm(algorithm_), suffix(kSuffixNone)
175 {
176 71506246 SetNull();
177 71509776 }
178
179 51205 explicit Digest(const Algorithms a, const HexPtr hex, const char s = 0) :
180 51205 algorithm(a), suffix(s)
181 {
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 assert((algorithm_ == kAny) || (a == algorithm_));
183 51205 const unsigned char_size = 2*kDigestSizes[a];
184
185 51205 const std::string *str = hex.str;
186 51205 const unsigned length = str->length();
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51205 times.
51205 assert(length >= char_size); // A suffix won't hurt
188
189
2/2
✓ Branch 0 taken 860872 times.
✓ Branch 1 taken 51205 times.
912077 for (unsigned i = 0; i < char_size; i += 2) {
190 860872 this->digest[i/2] =
191
2/2
✓ Branch 1 taken 526221 times.
✓ Branch 2 taken 334651 times.
1721744 ((*str)[i] <= '9' ? (*str)[i] -'0' : (*str)[i] - 'a' + 10)*16 +
192
2/2
✓ Branch 1 taken 538719 times.
✓ Branch 2 taken 322153 times.
860872 ((*str)[i+1] <= '9' ? (*str)[i+1] - '0' : (*str)[i+1] - 'a' + 10);
193 }
194 51205 }
195
196 288 Digest(const Algorithms a,
197 const unsigned char *digest_buffer,
198 const Suffix s = kSuffixNone) :
199 288 algorithm(a), suffix(s)
200 {
201 288 memcpy(digest, digest_buffer, kDigestSizes[a]);
202 288 }
203
204 /**
205 * Generates a purely random hash
206 * Only used for testing purposes
207 */
208 138 void Randomize() {
209 138 Prng prng;
210 138 prng.InitLocaltime();
211
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
138 Randomize(&prng);
212 138 }
213
214 /**
215 * Generates a purely random hash
216 * Only used for testing purposes
217 *
218 * @param seed random number generator seed (for reproducibility)
219 */
220 2000442 void Randomize(const uint64_t seed) {
221 2000442 Prng prng;
222 2000442 prng.InitSeed(seed);
223
1/2
✓ Branch 1 taken 2000221 times.
✗ Branch 2 not taken.
2000442 Randomize(&prng);
224 2000442 }
225
226 /**
227 * Generates a purely random hash
228 * Only used for testing purposes
229 *
230 * @param prng random number generator object (for external reproducibility)
231 */
232 4378219 void Randomize(Prng *prng) {
233 4378219 const unsigned bytes = GetDigestSize();
234
2/2
✓ Branch 0 taken 76892656 times.
✓ Branch 1 taken 4378218 times.
81270895 for (unsigned i = 0; i < bytes; ++i) {
235 76892676 digest[i] = prng->Next(256);
236 }
237 4378219 }
238
239 7320 bool HasSuffix() const { return suffix != kSuffixNone; }
240 369512 void set_suffix(const Suffix s) { suffix = s; }
241
242 /**
243 * Generates a hexified representation of the digest including the identifier
244 * string for newly added hashes.
245 *
246 * @param with_suffix append the hash suffix (C,H,X, ...) to the result
247 * @return a string representation of the digest
248 */
249 660322 std::string ToString(const bool with_suffix = false) const {
250 660322 Hex hex(this);
251
4/4
✓ Branch 0 taken 7028 times.
✓ Branch 1 taken 653169 times.
✓ Branch 3 taken 97 times.
✓ Branch 4 taken 6922 times.
660308 const bool use_suffix = with_suffix && HasSuffix();
252 660299 const unsigned string_length = hex.length() + use_suffix;
253
1/2
✓ Branch 2 taken 660191 times.
✗ Branch 3 not taken.
660300 std::string result(string_length, 0);
254
255
2/2
✓ Branch 1 taken 26312688 times.
✓ Branch 2 taken 656374 times.
26974791 for (unsigned int i = 0; i < hex.length(); ++i) {
256
2/4
✓ Branch 1 taken 26311301 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 26310929 times.
✗ Branch 5 not taken.
26316240 result[i] = hex[i];
257 }
258
259
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 656277 times.
656485 if (use_suffix) {
260
1/2
✓ Branch 1 taken 97 times.
✗ Branch 2 not taken.
97 result[string_length - 1] = suffix;
261 }
262
263
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 660160 times.
656485 assert(result.length() == string_length);
264 1320542 return result;
265 }
266
267 /**
268 * Generates a hexified representation of the digest including the identifier
269 * string for newly added hashes. Output is in the form of
270 * 'openssl x509 fingerprint', e.g. 00:AA:BB:...-SHAKE128
271 *
272 * @param with_suffix append the hash suffix (C,H,X, ...) to the result
273 * @return a string representation of the digest
274 */
275 8 std::string ToFingerprint(const bool with_suffix = false) const {
276 8 Hex hex(this);
277
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
8 const bool use_suffix = with_suffix && HasSuffix();
278 8 const unsigned string_length =
279 8 hex.length() + kDigestSizes[algorithm] - 1 + use_suffix;
280
1/2
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 std::string result(string_length, 0);
281
282 8 unsigned l = hex.length();
283
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 8 times.
344 for (unsigned int hex_i = 0, result_i = 0; hex_i < l; ++hex_i, ++result_i) {
284
2/4
✓ Branch 1 taken 336 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 336 times.
✗ Branch 5 not taken.
336 result[result_i] = toupper(hex[hex_i]);
285
4/4
✓ Branch 0 taken 296 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 152 times.
336 if ((hex_i < 2 * kDigestSizes[algorithm] - 1) && (hex_i % 2 == 1)) {
286
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
144 result[++result_i] = ':';
287 }
288 }
289
290
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (use_suffix) {
291
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 result[string_length - 1] = suffix;
292 }
293
294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 assert(result.length() == string_length);
295 16 return result;
296 }
297
298 /**
299 * Convenience method to generate a string representation of the digest.
300 * See Digest<>::ToString() for details
301 *
302 * @return a string representation including the hash suffix of the digest
303 */
304 187 std::string ToStringWithSuffix() const {
305 187 return ToString(true);
306 }
307
308 /**
309 * Generate the standard relative path from the hexified digest to be used in
310 * CAS areas or cache directories. Throughout the entire system we use one
311 * directory level (first to hex digest characters) for namespace splitting.
312 * Note: This method appends the internal hash suffix to the path.
313 *
314 * @return a relative path representation of the digest including the suffix
315 */
316 1236 std::string MakePath() const {
317 1236 return MakePathExplicit(1, 2, suffix);
318 }
319
320 /**
321 * The alternative path is used to symlink the root catalog from the webserver
322 * root to the data directory. This way, the data directory can be protected
323 * while the root catalog remains accessible.
324 */
325 10 std::string MakeAlternativePath() const {
326
1/2
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 return ".cvmfsalt-" + ToStringWithSuffix();
327 }
328
329 /**
330 * Produces a relative path representation of the digest without appending the
331 * hash suffix. See Digest<>::MakePath() for more details.
332 *
333 * @return a relative path representation of the digest without the suffix
334 */
335 341 std::string MakePathWithoutSuffix() const {
336 341 return MakePathExplicit(1, 2, kSuffixNone);
337 }
338
339 /**
340 * Generates an arbitrary path representation of the digest. Both number of
341 * directory levels and the hash-digits per level can be customized. Further-
342 * more an arbitrary hash suffix can be provided.
343 * Note: This method is mainly meant for internal usage but stays public for
344 * historical reasons.
345 *
346 * @param dir_levels the number of namespace splitting directory levels
347 * @param digits_per_level each directory level's number of hex-digits
348 * @param hash_suffix the hash suffix character to be appended
349 * @return a relative path representation of the digest
350 */
351 1593 std::string MakePathExplicit(const unsigned dir_levels,
352 const unsigned digits_per_level,
353 const Suffix hash_suffix = kSuffixNone) const {
354 1593 Hex hex(this);
355
356 // figure out how big the output string needs to be
357 1593 const bool use_suffix = (hash_suffix != kSuffixNone);
358 1593 const unsigned string_length = hex.length() + dir_levels + use_suffix;
359 1593 std::string result;
360
1/2
✓ Branch 1 taken 1593 times.
✗ Branch 2 not taken.
1593 result.resize(string_length);
361
362 // build hexified hash and path delimiters
363 1593 unsigned i = 0;
364 1593 unsigned pos = 0;
365
2/2
✓ Branch 1 taken 60611 times.
✓ Branch 2 taken 1593 times.
62204 for (; i < hex.length(); ++i) {
366
4/4
✓ Branch 0 taken 59018 times.
✓ Branch 1 taken 1593 times.
✓ Branch 2 taken 28753 times.
✓ Branch 3 taken 30265 times.
60611 if (i > 0 && (i % digits_per_level == 0)
367
2/2
✓ Branch 0 taken 1601 times.
✓ Branch 1 taken 27152 times.
28753 && (i / digits_per_level <= dir_levels)) {
368
1/2
✓ Branch 1 taken 1601 times.
✗ Branch 2 not taken.
1601 result[pos++] = '/';
369 }
370
2/4
✓ Branch 1 taken 60611 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 60611 times.
✗ Branch 5 not taken.
60611 result[pos++] = hex[i];
371 }
372
373 // (optionally) add hash hint suffix
374
2/2
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 1019 times.
1593 if (use_suffix) {
375
1/2
✓ Branch 1 taken 574 times.
✗ Branch 2 not taken.
574 result[pos++] = hash_suffix;
376 }
377
378
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1593 times.
1593 assert(i == hex.length());
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1593 times.
1593 assert(pos == string_length);
380 3186 return result;
381 }
382
383 1731792 bool IsNull() const {
384
2/2
✓ Branch 0 taken 8790060 times.
✓ Branch 1 taken 371085 times.
9161145 for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) {
385
2/2
✓ Branch 0 taken 1360707 times.
✓ Branch 1 taken 7429353 times.
8790060 if (digest[i] != 0)
386 1360707 return false;
387 }
388 371085 return true;
389 }
390
391 /**
392 * Get a partial digest for use when only 32 bits are required
393 */
394 43 uint32_t Partial32() const {
395 43 const uint32_t *partial = (const uint32_t *)digest;
396 43 return ntohl(*partial);
397 }
398
399
400 71505594 void SetNull() {
401 71505594 memset(digest, 0, digest_size_);
402 71505594 }
403
404
405 62275160 bool operator ==(const Digest<digest_size_, algorithm_> &other) const {
406
2/2
✓ Branch 0 taken 6268461 times.
✓ Branch 1 taken 24870064 times.
62275160 if (this->algorithm != other.algorithm)
407 12535370 return false;
408
2/2
✓ Branch 0 taken 214927274 times.
✓ Branch 1 taken 11847549 times.
453542548 for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) {
409
2/2
✓ Branch 0 taken 13022515 times.
✓ Branch 1 taken 201904759 times.
429847788 if (this->digest[i] != other.digest[i])
410 26045030 return false;
411 }
412 23694760 return true;
413 }
414
415 6352965 bool operator !=(const Digest<digest_size_, algorithm_> &other) const {
416 6352965 return !(*this == other);
417 }
418
419 72576134 bool operator <(const Digest<digest_size_, algorithm_> &other) const {
420
2/2
✓ Branch 0 taken 2393005 times.
✓ Branch 1 taken 70183129 times.
72576134 if (this->algorithm != other.algorithm)
421 2393005 return (this->algorithm < other.algorithm);
422
1/2
✓ Branch 0 taken 199069605 times.
✗ Branch 1 not taken.
194714797 for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) {
423
2/2
✓ Branch 0 taken 34837908 times.
✓ Branch 1 taken 164231697 times.
199069605 if (this->digest[i] > other.digest[i])
424 34837908 return false;
425
2/2
✓ Branch 0 taken 39700029 times.
✓ Branch 1 taken 124531668 times.
164231697 if (this->digest[i] < other.digest[i])
426 39700029 return true;
427 }
428 6 return false;
429 }
430
431 23864 bool operator >(const Digest<digest_size_, algorithm_> &other) const {
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23864 times.
23864 if (this->algorithm != other.algorithm)
433 return (this->algorithm > other.algorithm);
434
2/2
✓ Branch 0 taken 28108 times.
✓ Branch 1 taken 36 times.
28144 for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) {
435
2/2
✓ Branch 0 taken 8999 times.
✓ Branch 1 taken 19109 times.
28108 if (this->digest[i] < other.digest[i])
436 8999 return false;
437
2/2
✓ Branch 0 taken 14829 times.
✓ Branch 1 taken 4280 times.
19109 if (this->digest[i] > other.digest[i])
438 14829 return true;
439 }
440 36 return false;
441 }
442 };
443
444
445 struct CVMFS_EXPORT Md5 : public Digest<16, kMd5> {
446 12788395 Md5() : Digest<16, kMd5>() { }
447 explicit Md5(const AsciiPtr ascii);
448 37 explicit Md5(const HexPtr hex) : Digest<16, kMd5>(kMd5, hex) { }
449 Md5(const char *chars, const unsigned length);
450
451 /**
452 * An MD5 hash can be seen as two 64bit integers.
453 */
454 Md5(const uint64_t lo, const uint64_t hi);
455 void ToIntPair(uint64_t *lo, uint64_t *hi) const;
456 };
457
458 struct CVMFS_EXPORT Sha1 : public Digest<20, kSha1> { };
459 struct CVMFS_EXPORT Rmd160 : public Digest<20, kRmd160> { };
460 struct CVMFS_EXPORT Shake128 : public Digest<20, kShake128> { };
461
462 /**
463 * Any as such must not be used except for digest storage.
464 * To do real work, the class has to be "blessed" to be a real hash by
465 * setting the algorithm field accordingly.
466 */
467 struct CVMFS_EXPORT Any : public Digest<kMaxDigestSize, kAny> {
468 17569374 Any() : Digest<kMaxDigestSize, kAny>() { }
469
470 3848663 explicit Any(const Algorithms a,
471 3848663 const char s = kSuffixNone) :
472 3848663 Digest<kMaxDigestSize, kAny>() { algorithm = a; suffix = s; }
473
474 288 Any(const Algorithms a,
475 const unsigned char *digest_buffer,
476 288 const Suffix suffix = kSuffixNone) :
477 288 Digest<kMaxDigestSize, kAny>(a, digest_buffer, suffix) { }
478
479 51168 explicit Any(const Algorithms a,
480 const HexPtr hex,
481 51168 const char suffix = kSuffixNone) :
482 51168 Digest<kMaxDigestSize, kAny>(a, hex, suffix) { }
483
484 Md5 CastToMd5();
485 };
486
487
488 /**
489 * Actual operations on digests, like "hash a file", "hash a buffer", or
490 * iterative operations.
491 */
492 CVMFS_EXPORT unsigned GetContextSize(const Algorithms algorithm);
493
494 /**
495 * Holds an OpenSSL context, only required for hash operations. Allows to
496 * deferr the storage allocation for the context to alloca.
497 */
498 class CVMFS_EXPORT ContextPtr {
499 public:
500 Algorithms algorithm;
501 void *buffer;
502 unsigned size;
503
504 255288 ContextPtr() : algorithm(kAny), buffer(NULL), size(0) {}
505
506 1237505 explicit ContextPtr(const Algorithms a) :
507 1237505 algorithm(a), buffer(NULL), size(GetContextSize(a)) {}
508 ContextPtr(const Algorithms a, void *b) :
509 algorithm(a), buffer(b), size(GetContextSize(a)) {}
510 };
511
512 CVMFS_EXPORT void Init(ContextPtr context);
513 CVMFS_EXPORT void Update(const unsigned char *buffer,
514 const unsigned buffer_size,
515 ContextPtr context);
516 CVMFS_EXPORT void Final(ContextPtr context, Any *any_digest);
517 CVMFS_EXPORT bool HashFile(const std::string &filename, Any *any_digest);
518 CVMFS_EXPORT bool HashFd(int fd, Any *any_digest);
519 CVMFS_EXPORT void HashMem(const unsigned char *buffer,
520 const unsigned buffer_size,
521 Any *any_digest);
522 CVMFS_EXPORT void HashString(const std::string &content, Any *any_digest);
523 CVMFS_EXPORT void Hmac(const std::string &key,
524 const unsigned char *buffer,
525 const unsigned buffer_size,
526 Any *any_digest);
527 1 inline void HmacString(const std::string &key, const std::string &content,
528 Any *any_digest)
529 {
530 1 Hmac(key,
531 1 reinterpret_cast<const unsigned char *>(content.data()),
532 1 content.size(),
533 any_digest);
534 1 }
535
536 /**
537 * Only used for AWS4 signature.
538 *
539 * Adding SHA-256 to the standard hash infrastructure would generally bloat the
540 * digets size to 32 bytes and require client data structure transformation
541 * during hotpatch.
542 */
543 CVMFS_EXPORT std::string Hmac256(const std::string &key,
544 const std::string &content,
545 bool raw_output = false);
546 CVMFS_EXPORT std::string Sha256File(const std::string &filename);
547 CVMFS_EXPORT std::string Sha256Mem(const unsigned char *buffer,
548 const unsigned buffer_size);
549 CVMFS_EXPORT std::string Sha256String(const std::string &content);
550
551 CVMFS_EXPORT
552 Algorithms ParseHashAlgorithm(const std::string &algorithm_option);
553 CVMFS_EXPORT
554 Any MkFromHexPtr(const HexPtr hex, const Suffix suffix = kSuffixNone);
555 CVMFS_EXPORT Any MkFromSuffixedHexPtr(const HexPtr hex);
556
557 } // namespace shash
558
559 #ifdef CVMFS_NAMESPACE_GUARD
560 } // namespace CVMFS_NAMESPACE_GUARD
561 #endif
562
563 #endif // CVMFS_CRYPTO_HASH_H_
564