cache-digest: Truncate hash from MSB

This commit is contained in:
Tatsuhiro Tsujikawa 2016-07-21 23:32:20 +09:00
parent 24edc961fb
commit f770831811
1 changed files with 22 additions and 24 deletions

View File

@ -32,6 +32,26 @@
namespace nghttp2 { namespace nghttp2 {
namespace {
// Truncates |md| to |nbits| bits counting from MSB. |nbits| is
// guaranteed to be less than or equal to 62.
uint64_t truncate_hash(const uint8_t *md, uint32_t nbits) {
uint64_t v;
v = (static_cast<uint64_t>(md[0]) << 56) +
(static_cast<uint64_t>(md[1]) << 48) +
(static_cast<uint64_t>(md[2]) << 40) +
(static_cast<uint64_t>(md[3]) << 32) +
(static_cast<uint64_t>(md[4]) << 24) +
(static_cast<uint64_t>(md[5]) << 16) +
(static_cast<uint64_t>(md[6]) << 8) + static_cast<uint64_t>(md[31]);
v >>= 64 - nbits;
return v;
}
} // namespace
namespace { namespace {
int compute_hash_values(std::vector<uint64_t> &hash_values, int compute_hash_values(std::vector<uint64_t> &hash_values,
const std::vector<std::string> &uris, uint32_t nbits) { const std::vector<std::string> &uris, uint32_t nbits) {
@ -41,8 +61,6 @@ int compute_hash_values(std::vector<uint64_t> &hash_values,
return -1; return -1;
} }
uint64_t mask = (static_cast<uint64_t>(1) << nbits) - 1;
auto ctx = EVP_MD_CTX_create(); auto ctx = EVP_MD_CTX_create();
hash_values.resize(uris.size()); hash_values.resize(uris.size());
@ -70,18 +88,7 @@ int compute_hash_values(std::vector<uint64_t> &hash_values,
assert(len == 32); assert(len == 32);
uint64_t v; *p++ = truncate_hash(md.data(), nbits);
v = (static_cast<uint64_t>(md[24]) << 56) +
(static_cast<uint64_t>(md[25]) << 48) +
(static_cast<uint64_t>(md[26]) << 40) +
(static_cast<uint64_t>(md[27]) << 32) +
(static_cast<uint64_t>(md[28]) << 24) +
(static_cast<uint64_t>(md[29]) << 16) +
(static_cast<uint64_t>(md[30]) << 8) + static_cast<uint64_t>(md[31]);
v &= mask;
*p++ = v;
} }
EVP_MD_CTX_destroy(ctx); EVP_MD_CTX_destroy(ctx);
@ -219,7 +226,6 @@ ssize_t cache_digest_encode(uint8_t *data, size_t datalen,
int cache_digest_hash(uint64_t &key, size_t nbits, const StringRef &s) { int cache_digest_hash(uint64_t &key, size_t nbits, const StringRef &s) {
int rv; int rv;
uint64_t mask = (static_cast<uint64_t>(1) << nbits) - 1;
std::array<uint8_t, 32> md; std::array<uint8_t, 32> md;
@ -246,15 +252,7 @@ int cache_digest_hash(uint64_t &key, size_t nbits, const StringRef &s) {
EVP_MD_CTX_destroy(ctx); EVP_MD_CTX_destroy(ctx);
key = (static_cast<uint64_t>(md[24]) << 56) + key = truncate_hash(md.data(), nbits);
(static_cast<uint64_t>(md[25]) << 48) +
(static_cast<uint64_t>(md[26]) << 40) +
(static_cast<uint64_t>(md[27]) << 32) +
(static_cast<uint64_t>(md[28]) << 24) +
(static_cast<uint64_t>(md[29]) << 16) +
(static_cast<uint64_t>(md[30]) << 8) + static_cast<uint64_t>(md[31]);
key &= mask;
return 0; return 0;
} }