2
0

metrohash64.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // metrohash64.h
  2. //
  3. // The MIT License (MIT)
  4. //
  5. // Copyright (c) 2015 J. Andrew Rogers
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. #ifndef METROHASH_METROHASH_64_H
  26. #define METROHASH_METROHASH_64_H
  27. struct MetroHash64
  28. {
  29. // Initializes internal state for new hash with optional seed
  30. void Initialize(const uint64_t seed=0);
  31. // Update the hash state with a string of bytes. If the length
  32. // is sufficiently long, the implementation switches to a bulk
  33. // hashing algorithm directly on the argument buffer for speed.
  34. void Update(const uint8_t * buffer, const uint64_t length);
  35. // Constructs the final hash and writes it to the argument buffer.
  36. // After a hash is finalized, this instance must be Initialized()-ed
  37. // again or the behavior of Update() and Finalize() is undefined.
  38. void Finalize(uint8_t * const hash);
  39. // A non-incremental function implementation. This can be significantly
  40. // faster than the incremental implementation for some usage patterns.
  41. static void Hash(const uint8_t * buffer, const uint64_t length, uint8_t * const hash, const uint64_t seed=0);
  42. // Does implementation correctly execute test vectors?
  43. static bool ImplementationVerified();
  44. // test vectors -- Hash(test_string, seed=0) => test_seed_0
  45. static const char * test_string;
  46. static const uint8_t test_seed_0[8];
  47. static const uint8_t test_seed_1[8];
  48. private:
  49. static const uint64_t k0 = 0xD6D018F5;
  50. static const uint64_t k1 = 0xA2AA033B;
  51. static const uint64_t k2 = 0x62992FC1;
  52. static const uint64_t k3 = 0x30BC5B29;
  53. struct State { uint64_t v[4]; } state;
  54. struct { uint8_t b[32]; } input;
  55. uint64_t bytes;
  56. uint64_t vseed;
  57. };
  58. #endif // #ifndef METROHASH_METROHASH_64_H