GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/crypto/hash.h
Date: 2024-04-28 02:33:07
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 13819 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 660572 explicit Hex(const Digest<digest_size_, algorithm_> *digest) :
131 660572 digest_(*digest),
132 660572 hash_length_(2 * kDigestSizes[digest_.algorithm]),
133 660572 algo_id_length_(kAlgorithmIdSizes[digest_.algorithm]) {}
134
135 53900533 unsigned int length() const { return hash_length_ + algo_id_length_; }
136
137 26298039 char operator[](const unsigned int position) const {
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26294476 times.
26298039 assert(position < length());
139 26298028 return (position < hash_length_)
140
2/2
✓ Branch 0 taken 26293651 times.
✓ Branch 1 taken 825 times.
26298028 ? GetHashChar(position)
141 26296844 : GetAlgorithmIdentifierChar(position);
142 }
143
144 protected:
145 26296979 char GetHashChar(const unsigned int position) const {
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26293427 times.
26296979 assert(position < hash_length_);
147
2/2
✓ Branch 0 taken 13157886 times.
✓ Branch 1 taken 13135541 times.
26296979 const char digit = (position % 2 == 0)
148 13159662 ? digest_.digest[position / 2] / 16
149 13137317 : digest_.digest[position / 2] % 16;
150 26296979 return ToHex(digit);
151 }
152
153 825 char GetAlgorithmIdentifierChar(const unsigned int position) const {
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 825 times.
825 assert(position >= hash_length_);
155 825 return kAlgorithmIds[digest_.algorithm][position - hash_length_];
156 }
157
158 26297112 char ToHex(const char c) const {
159
2/2
✓ Branch 0 taken 16700151 times.
✓ Branch 1 taken 9593409 times.
26297112 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 69607262 Digest() :
174 69607262 algorithm(algorithm_), suffix(kSuffixNone)
175 {
176 69607262 SetNull();
177 69609922 }
178
179 14448 explicit Digest(const Algorithms a, const HexPtr hex, const char s = 0) :
180 14448 algorithm(a), suffix(s)
181 {
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 assert((algorithm_ == kAny) || (a == algorithm_));
183 14448 const unsigned char_size = 2*kDigestSizes[a];
184
185 14448 const std::string *str = hex.str;
186 14448 const unsigned length = str->length();
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14448 times.
14448 assert(length >= char_size); // A suffix won't hurt
188
189
2/2
✓ Branch 0 taken 266092 times.
✓ Branch 1 taken 14448 times.
280540 for (unsigned i = 0; i < char_size; i += 2) {
190 266092 this->digest[i/2] =
191
2/2
✓ Branch 1 taken 172657 times.
✓ Branch 2 taken 93435 times.
532184 ((*str)[i] <= '9' ? (*str)[i] -'0' : (*str)[i] - 'a' + 10)*16 +
192
2/2
✓ Branch 1 taken 168253 times.
✓ Branch 2 taken 97839 times.
266092 ((*str)[i+1] <= '9' ? (*str)[i+1] - '0' : (*str)[i+1] - 'a' + 10);
193 }
194 14448 }
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 6070 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 659005 std::string ToString(const bool with_suffix = false) const {
250 659005 Hex hex(this);
251
4/4
✓ Branch 0 taken 5785 times.
✓ Branch 1 taken 653082 times.
✓ Branch 3 taken 97 times.
✓ Branch 4 taken 5672 times.
658978 const bool use_suffix = with_suffix && HasSuffix();
252 658962 const unsigned string_length = hex.length() + use_suffix;
253
1/2
✓ Branch 2 taken 658864 times.
✗ Branch 3 not taken.
658956 std::string result(string_length, 0);
254
255
2/2
✓ Branch 1 taken 26234091 times.
✓ Branch 2 taken 653356 times.
26894230 for (unsigned int i = 0; i < hex.length(); ++i) {
256
2/4
✓ Branch 1 taken 26232260 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 26231698 times.
✗ Branch 5 not taken.
26237643 result[i] = hex[i];
257 }
258
259
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 653259 times.
653467 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 12 times.
✓ Branch 2 taken 658752 times.
653467 assert(result.length() == string_length);
264 1317726 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 60592 times.
✓ Branch 2 taken 1592 times.
62185 for (; i < hex.length(); ++i) {
366
4/4
✓ Branch 0 taken 59000 times.
✓ Branch 1 taken 1592 times.
✓ Branch 2 taken 28747 times.
✓ Branch 3 taken 30253 times.
60592 if (i > 0 && (i % digits_per_level == 0)
367
2/2
✓ Branch 0 taken 1601 times.
✓ Branch 1 taken 27146 times.
28747 && (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 60592 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 60592 times.
✗ Branch 5 not taken.
60592 result[pos++] = hex[i];
371 }
372
373 // (optionally) add hash hint suffix
374
2/2
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 1018 times.
1592 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.
1592 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 1731744 bool IsNull() const {
384
2/2
✓ Branch 0 taken 8789857 times.
✓ Branch 1 taken 371074 times.
9160931 for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) {
385
2/2
✓ Branch 0 taken 1360670 times.
✓ Branch 1 taken 7429187 times.
8789857 if (digest[i] != 0)
386 1360670 return false;
387 }
388 371074 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 69606886 void SetNull() {
401 69606886 memset(digest, 0, digest_size_);
402 69606886 }
403
404
405 62249470 bool operator ==(const Digest<digest_size_, algorithm_> &other) const {
406
2/2
✓ Branch 0 taken 6262050 times.
✓ Branch 1 taken 24863620 times.
62249470 if (this->algorithm != other.algorithm)
407 12522546 return false;
408
2/2
✓ Branch 0 taken 214851042 times.
✓ Branch 1 taken 11843883 times.
453383214 for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) {
409
2/2
✓ Branch 0 taken 13019737 times.
✓ Branch 1 taken 201831305 times.
429695764 if (this->digest[i] != other.digest[i])
410 26039474 return false;
411 }
412 23687450 return true;
413 }
414
415 6352946 bool operator !=(const Digest<digest_size_, algorithm_> &other) const {
416 6352946 return !(*this == other);
417 }
418
419 72592126 bool operator <(const Digest<digest_size_, algorithm_> &other) const {
420
2/2
✓ Branch 0 taken 2393080 times.
✓ Branch 1 taken 70199046 times.
72592126 if (this->algorithm != other.algorithm)
421 2393080 return (this->algorithm < other.algorithm);
422
1/2
✓ Branch 0 taken 199264562 times.
✗ Branch 1 not taken.
194915914 for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) {
423
2/2
✓ Branch 0 taken 34709292 times.
✓ Branch 1 taken 164555270 times.
199264562 if (this->digest[i] > other.digest[i])
424 34709292 return false;
425
2/2
✓ Branch 0 taken 39838402 times.
✓ Branch 1 taken 124716868 times.
164555270 if (this->digest[i] < other.digest[i])
426 39838402 return true;
427 }
428 6 return false;
429 }
430
431 23874 bool operator >(const Digest<digest_size_, algorithm_> &other) const {
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23874 times.
23874 if (this->algorithm != other.algorithm)
433 return (this->algorithm > other.algorithm);
434
2/2
✓ Branch 0 taken 28114 times.
✓ Branch 1 taken 36 times.
28150 for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) {
435
2/2
✓ Branch 0 taken 9019 times.
✓ Branch 1 taken 19095 times.
28114 if (this->digest[i] < other.digest[i])
436 9019 return false;
437
2/2
✓ Branch 0 taken 14819 times.
✓ Branch 1 taken 4276 times.
19095 if (this->digest[i] > other.digest[i])
438 14819 return true;
439 }
440 36 return false;
441 }
442 };
443
444
445 struct CVMFS_EXPORT Md5 : public Digest<16, kMd5> {
446 12488345 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 17531545 Any() : Digest<kMaxDigestSize, kAny>() { }
469
470 3253372 explicit Any(const Algorithms a,
471 3253372 const char s = kSuffixNone) :
472 3253372 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 14411 explicit Any(const Algorithms a,
480 const HexPtr hex,
481 14411 const char suffix = kSuffixNone) :
482 14411 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 255282 ContextPtr() : algorithm(kAny), buffer(NULL), size(0) {}
505
506 638366 explicit ContextPtr(const Algorithms a) :
507 638366 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