blob: 021efc19b5e3d69a1dc6e88ae7dda516e8e4f492 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include "util/cache.h"
#include "util/assert.h"
#include "util/math.h"
namespace mixxx {
cache_key_t cacheKeyFromMessageDigest(const QByteArray& messageDigest) {
cache_key_t key = invalidCacheKey();
DEBUG_ASSERT(!isValidCacheKey(key));
if (messageDigest.isEmpty()) {
return key;
}
// Source: FIPS 180-4 Secure Hash Standard (SHS)
// SP 800-107
// 5 Hash function Usage
// 5.1 Truncated Message Digest
const auto significantByteCount = math_min(
messageDigest.size(),
static_cast<int>(sizeof(cache_key_t)));
for (auto i = 0; i < significantByteCount; ++i) {
// Only 8 bits are relevant and we don't want the sign
// extension of a (signed) char during the conversion.
const cache_key_t nextByte =
static_cast<unsigned char>(messageDigest.at(i));
DEBUG_ASSERT(nextByte == (nextByte & 0xFF));
key <<= 8;
key |= nextByte;
}
if (!isValidCacheKey(key)) {
// Unlikely but possible
key = ~key;
}
DEBUG_ASSERT(isValidCacheKey(key));
return key;
}
} // namespace mixxx
|