Fingerprint.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * Address and full hash of an identity's public keys.
  25. *
  26. * This is the same size as ZT_Fingerprint and should be cast-able back and forth.
  27. * This is checked in Tests.cpp.
  28. */
  29. class Fingerprint : public TriviallyCopyable
  30. {
  31. friend class Identity;
  32. public:
  33. /**
  34. * Create an empty/nil fingerprint
  35. */
  36. ZT_INLINE Fingerprint() noexcept { memoryZero(this); }
  37. /**
  38. * Create a Fingerprint that is a copy of the external API companion structure
  39. *
  40. * @param apifp API fingerprint
  41. */
  42. ZT_INLINE Fingerprint(const ZT_Fingerprint &apifp) noexcept { Utils::copy<sizeof(ZT_Fingerprint)>(&m_cfp,&apifp); }
  43. ZT_INLINE Address address() const noexcept { return Address(m_cfp.address); }
  44. ZT_INLINE const uint8_t *hash() const noexcept { return m_cfp.hash; }
  45. ZT_INLINE ZT_Fingerprint *apiFingerprint() noexcept { return &m_cfp; }
  46. ZT_INLINE const ZT_Fingerprint *apiFingerprint() const noexcept { return &m_cfp; }
  47. /**
  48. * @return True if hash is not all zero (missing/unspecified)
  49. */
  50. ZT_INLINE bool haveHash() const noexcept { return (!Utils::allZero(m_cfp.hash, sizeof(m_cfp.hash))); }
  51. /**
  52. * Get a base32-encoded representation of this fingerprint
  53. *
  54. * @param s Base32 string
  55. */
  56. ZT_INLINE void toString(char s[ZT_FINGERPRINT_STRING_BUFFER_LENGTH]) const noexcept
  57. {
  58. uint8_t tmp[48 + 5];
  59. address().copyTo(tmp);
  60. Utils::copy<48>(tmp + 5, m_cfp.hash);
  61. Utils::b32e(tmp,sizeof(tmp),s,ZT_FINGERPRINT_STRING_BUFFER_LENGTH);
  62. s[ZT_FINGERPRINT_STRING_BUFFER_LENGTH-1] = 0; // sanity check, ensure always zero terminated
  63. }
  64. /**
  65. * Set this fingerprint to a base32-encoded string
  66. *
  67. * @param s String to decode
  68. * @return True if string appears to be valid and of the proper length (no other checking is done)
  69. */
  70. ZT_INLINE bool fromString(const char *s) noexcept
  71. {
  72. uint8_t tmp[48 + 5];
  73. if (Utils::b32d(s,tmp,sizeof(tmp)) != sizeof(tmp))
  74. return false;
  75. m_cfp.address = Address(tmp).toInt();
  76. Utils::copy<48>(m_cfp.hash, tmp + 5);
  77. return true;
  78. }
  79. ZT_INLINE void zero() noexcept { memoryZero(this); }
  80. ZT_INLINE unsigned long hashCode() const noexcept { return (unsigned long)m_cfp.address; }
  81. ZT_INLINE operator bool() const noexcept { return (m_cfp.address != 0); }
  82. ZT_INLINE bool operator==(const Fingerprint &h) const noexcept { return ((m_cfp.address == h.m_cfp.address) && (memcmp(m_cfp.hash, h.m_cfp.hash, ZT_FINGERPRINT_HASH_SIZE) == 0)); }
  83. ZT_INLINE bool operator!=(const Fingerprint &h) const noexcept { return !(*this == h); }
  84. ZT_INLINE bool operator<(const Fingerprint &h) const noexcept { return ((m_cfp.address < h.m_cfp.address) || ((m_cfp.address == h.m_cfp.address) && (memcmp(m_cfp.hash, h.m_cfp.hash, ZT_FINGERPRINT_HASH_SIZE) < 0))); }
  85. ZT_INLINE bool operator>(const Fingerprint &h) const noexcept { return (h < *this); }
  86. ZT_INLINE bool operator<=(const Fingerprint &h) const noexcept { return !(h < *this); }
  87. ZT_INLINE bool operator>=(const Fingerprint &h) const noexcept { return !(*this < h); }
  88. private:
  89. ZT_Fingerprint m_cfp;
  90. };
  91. } // namespace ZeroTier
  92. #endif