| 1 | // metrohash64.h |
| 2 | // |
| 3 | // Copyright 2015-2018 J. Andrew Rogers |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | #ifndef METROHASH_METROHASH_64_H |
| 18 | #define METROHASH_METROHASH_64_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | class MetroHash64 |
| 23 | { |
| 24 | public: |
| 25 | static const uint32_t bits = 64; |
| 26 | |
| 27 | // Constructor initializes the same as Initialize() |
| 28 | MetroHash64(const uint64_t seed=0); |
| 29 | |
| 30 | // Initializes internal state for new hash with optional seed |
| 31 | void Initialize(const uint64_t seed=0); |
| 32 | |
| 33 | // Update the hash state with a string of bytes. If the length |
| 34 | // is sufficiently long, the implementation switches to a bulk |
| 35 | // hashing algorithm directly on the argument buffer for speed. |
| 36 | void Update(const uint8_t * buffer, const uint64_t length); |
| 37 | |
| 38 | // Constructs the final hash and writes it to the argument buffer. |
| 39 | // After a hash is finalized, this instance must be Initialized()-ed |
| 40 | // again or the behavior of Update() and Finalize() is undefined. |
| 41 | void Finalize(uint8_t * const hash); |
| 42 | |
| 43 | // A non-incremental function implementation. This can be significantly |
| 44 | // faster than the incremental implementation for some usage patterns. |
| 45 | static void Hash(const uint8_t * buffer, const uint64_t length, uint8_t * const hash, const uint64_t seed=0); |
| 46 | |
| 47 | // Does implementation correctly execute test vectors? |
| 48 | static bool ImplementationVerified(); |
| 49 | |
| 50 | // test vectors -- Hash(test_string, seed=0) => test_seed_0 |
| 51 | static const char * test_string; |
| 52 | static const uint8_t test_seed_0[8]; |
| 53 | static const uint8_t test_seed_1[8]; |
| 54 | |
| 55 | private: |
| 56 | static const uint64_t k0 = 0xD6D018F5; |
| 57 | static const uint64_t k1 = 0xA2AA033B; |
| 58 | static const uint64_t k2 = 0x62992FC1; |
| 59 | static const uint64_t k3 = 0x30BC5B29; |
| 60 | |
| 61 | struct { uint64_t v[4]; } state; |
| 62 | struct { uint8_t b[32]; } input; |
| 63 | uint64_t bytes; |
| 64 | uint64_t vseed; |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | // Legacy 64-bit hash functions -- do not use |
| 69 | void metrohash64_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out); |
| 70 | void metrohash64_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out); |
| 71 | |
| 72 | |
| 73 | #endif // #ifndef METROHASH_METROHASH_64_H |
| 74 | |