Fingerprint.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_FINGERPRINT_HPP
  14. #define ZT_FINGERPRINT_HPP
  15. #include "Constants.hpp"
  16. #include "TriviallyCopyable.hpp"
  17. namespace ZeroTier {
  18. /**
  19. * Container for 384-bit identity hashes
  20. *
  21. * The size of the hash used with this container must be a multiple of 64 bits.
  22. * Currently it's used as H<384> and H<512>.
  23. *
  24. * Warning: the [] operator is not bounds checked.
  25. *
  26. * @tparam BITS Bits in hash, must be a multiple of 64
  27. */
  28. class Fingerprint : public TriviallyCopyable
  29. {
  30. public:
  31. ZT_ALWAYS_INLINE Fingerprint() noexcept {}
  32. explicit ZT_ALWAYS_INLINE Fingerprint(const void *h384) noexcept { memcpy(_h,h384,48); }
  33. ZT_ALWAYS_INLINE void set(const void *h384) noexcept { memcpy(_h,h384,48); }
  34. ZT_ALWAYS_INLINE void zero() noexcept
  35. {
  36. for(int i=0;i<(384 / (sizeof(unsigned long) * 8));++i)
  37. _h[i] = 0;
  38. }
  39. ZT_ALWAYS_INLINE uint8_t *data() noexcept { return reinterpret_cast<uint8_t *>(_h); }
  40. ZT_ALWAYS_INLINE const uint8_t *data() const noexcept { return reinterpret_cast<const uint8_t *>(_h); }
  41. ZT_ALWAYS_INLINE uint8_t operator[](const unsigned int i) const noexcept { return reinterpret_cast<const uint8_t *>(_h)[i]; }
  42. ZT_ALWAYS_INLINE uint8_t &operator[](const unsigned int i) noexcept { return reinterpret_cast<uint8_t *>(_h)[i]; }
  43. static constexpr unsigned int size() noexcept { return 48; }
  44. ZT_ALWAYS_INLINE unsigned long hashCode() const noexcept { return _h[0]; }
  45. ZT_ALWAYS_INLINE operator bool() const noexcept
  46. {
  47. for(int i=0;i<(384 / (sizeof(unsigned long) * 8));++i) {
  48. if (_h[i] != 0)
  49. return true;
  50. }
  51. return false;
  52. }
  53. ZT_ALWAYS_INLINE bool operator==(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) == 0; }
  54. ZT_ALWAYS_INLINE bool operator!=(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) != 0; }
  55. ZT_ALWAYS_INLINE bool operator<(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) < 0; }
  56. ZT_ALWAYS_INLINE bool operator>(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) > 0; }
  57. ZT_ALWAYS_INLINE bool operator<=(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) <= 0; }
  58. ZT_ALWAYS_INLINE bool operator>=(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) >= 0; }
  59. private:
  60. unsigned long _h[384 / (sizeof(unsigned long) * 8)];
  61. };
  62. } // namespace ZeroTier
  63. #endif