blob: 80ca8b06b259fc84562623bd645e8b662daa5929 [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/lib/util/hash.h"
#include <farmhash.h>
namespace cobalt::util {
namespace {
const uint32_t kBytesPerInt64 = 8;
const uint32_t kSingleByteMask = 0xFF;
void AppendBytesLittleEndian(uint64_t value, std::string *bytes) {
for (uint32_t i = 0; i < kBytesPerInt64; i++) {
bytes->push_back(static_cast<char>((value >> (kBytesPerInt64 * i)) & kSingleByteMask));
}
}
} // namespace
std::string FarmhashFingerprint(const std::string &data) {
farmhash::uint128_t fingerprint = farmhash::Fingerprint128(data);
std::string bytes;
AppendBytesLittleEndian(farmhash::Uint128Low64(fingerprint), &bytes);
AppendBytesLittleEndian(farmhash::Uint128High64(fingerprint), &bytes);
return bytes;
}
uint64_t Farmhash64(const std::string &data) { return farmhash::Fingerprint64(data); }
} // namespace cobalt::util