| // 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 "third_party/github.com/google/farmhash/src/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; |
| } |
| |
| } // namespace cobalt::util |