Fingerprint.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "Address.hpp"
  18. #include "Utils.hpp"
  19. #include <algorithm>
  20. #define ZT_FINGERPRINT_STRING_BUFFER_LENGTH 96
  21. namespace ZeroTier {
  22. class Identity;
  23. /**
  24. * Container for 384-bit identity hashes
  25. *
  26. * The size of the hash used with this container must be a multiple of 64 bits.
  27. * Currently it's used as H<384> and H<512>.
  28. *
  29. * Warning: the [] operator is not bounds checked.
  30. *
  31. * @tparam BITS Bits in hash, must be a multiple of 64
  32. */
  33. class Fingerprint : public TriviallyCopyable
  34. {
  35. friend class Identity;
  36. public:
  37. /**
  38. * Create an empty/nil fingerprint
  39. */
  40. ZT_ALWAYS_INLINE Fingerprint() noexcept { memoryZero(this); }
  41. ZT_ALWAYS_INLINE Address address() const noexcept { return Address(_fp.address); }
  42. ZT_ALWAYS_INLINE const uint8_t *hash() const noexcept { return _fp.hash; }
  43. /**
  44. * Copy into ZT_Fingerprint struct as used in API and trace messages
  45. *
  46. * @param fp ZT_Fingerprint
  47. */
  48. ZT_ALWAYS_INLINE void getAPIFingerprint(ZT_Fingerprint *fp) const noexcept { memcpy(fp,&_fp,sizeof(ZT_Fingerprint)); }
  49. /**
  50. * @return Pointer to ZT_Fingerprint for API use
  51. */
  52. ZT_ALWAYS_INLINE const ZT_Fingerprint *apiFingerprint() const noexcept { return &_fp; }
  53. /**
  54. * Get a base32-encoded representation of this fingerprint
  55. *
  56. * @param s Base32 string
  57. */
  58. ZT_ALWAYS_INLINE void toString(char s[ZT_FINGERPRINT_STRING_BUFFER_LENGTH])
  59. {
  60. uint8_t tmp[48 + 5];
  61. address().copyTo(tmp);
  62. memcpy(tmp + 5,_fp.hash,48);
  63. Utils::b32e(tmp,sizeof(tmp),s,ZT_FINGERPRINT_STRING_BUFFER_LENGTH);
  64. s[ZT_FINGERPRINT_STRING_BUFFER_LENGTH-1] = 0; // sanity check, ensure always zero terminated
  65. }
  66. /**
  67. * Set this fingerprint to a base32-encoded string
  68. *
  69. * @param s String to decode
  70. * @return True if string appears to be valid and of the proper length (no other checking is done)
  71. */
  72. ZT_ALWAYS_INLINE bool fromString(const char *s)
  73. {
  74. uint8_t tmp[48 + 5];
  75. if (Utils::b32d(s,tmp,sizeof(tmp)) != sizeof(tmp))
  76. return false;
  77. _fp.address = Address(tmp).toInt();
  78. memcpy(_fp.hash,tmp + 5,48);
  79. return true;
  80. }
  81. ZT_ALWAYS_INLINE void zero() noexcept { memoryZero(this); }
  82. ZT_ALWAYS_INLINE unsigned long hashCode() const noexcept { return _fp.address; }
  83. ZT_ALWAYS_INLINE operator bool() const noexcept { return (_fp.address != 0); }
  84. ZT_ALWAYS_INLINE bool operator==(const Fingerprint &h) const noexcept { return ((_fp.address == h._fp.address)&&(memcmp(_fp.hash,h._fp.hash,ZT_IDENTITY_HASH_SIZE) == 0)); }
  85. ZT_ALWAYS_INLINE bool operator!=(const Fingerprint &h) const noexcept { return !(*this == h); }
  86. ZT_ALWAYS_INLINE bool operator<(const Fingerprint &h) const noexcept { return ((_fp.address < h._fp.address) || ((_fp.address == h._fp.address)&&(memcmp(_fp.hash,h._fp.hash,ZT_IDENTITY_HASH_SIZE) < 0))); }
  87. ZT_ALWAYS_INLINE bool operator>(const Fingerprint &h) const noexcept { return (h < *this); }
  88. ZT_ALWAYS_INLINE bool operator<=(const Fingerprint &h) const noexcept { return !(h < *this); }
  89. ZT_ALWAYS_INLINE bool operator>=(const Fingerprint &h) const noexcept { return !(*this < h); }
  90. private:
  91. ZT_Fingerprint _fp;
  92. };
  93. } // namespace ZeroTier
  94. #endif