1// metrohash128.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_128_H
18#define METROHASH_METROHASH_128_H
19
20#include <stdint.h>
21
22class MetroHash128
23{
24public:
25 static const uint32_t bits = 128;
26
27 // Constructor initializes the same as Initialize()
28 MetroHash128(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[16];
53 static const uint8_t test_seed_1[16];
54
55private:
56 static const uint64_t k0 = 0xC83A91E1;
57 static const uint64_t k1 = 0x8648DBDB;
58 static const uint64_t k2 = 0x7BDEC03B;
59 static const uint64_t k3 = 0x2F5870A5;
60
61 struct { uint64_t v[4]; } state;
62 struct { uint8_t b[32]; } input;
63 uint64_t bytes;
64};
65
66
67// Legacy 128-bit hash functions -- do not use
68void metrohash128_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out);
69void metrohash128_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out);
70
71
72#endif // #ifndef METROHASH_METROHASH_128_H
73