Fingerprint.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. ZT_INLINE Address address() const noexcept { return Address(_fp.address); }
  38. ZT_INLINE const uint8_t *hash() const noexcept { return _fp.hash; }
  39. /**
  40. * Copy into ZT_Fingerprint struct as used in API and trace messages
  41. *
  42. * @param fp ZT_Fingerprint
  43. */
  44. ZT_INLINE void getAPIFingerprint(ZT_Fingerprint *fp) const noexcept { memcpy(fp,&_fp,sizeof(ZT_Fingerprint)); }
  45. /**
  46. * @return Pointer to ZT_Fingerprint for API use
  47. */
  48. ZT_INLINE const ZT_Fingerprint *apiFingerprint() const noexcept { return &_fp; }
  49. /**
  50. * Get a base32-encoded representation of this fingerprint
  51. *
  52. * @param s Base32 string
  53. */
  54. ZT_INLINE void toString(char s[ZT_FINGERPRINT_STRING_BUFFER_LENGTH]) const noexcept
  55. {
  56. uint8_t tmp[48 + 5];
  57. address().copyTo(tmp);
  58. memcpy(tmp + 5,_fp.hash,48);
  59. Utils::b32e(tmp,sizeof(tmp),s,ZT_FINGERPRINT_STRING_BUFFER_LENGTH);
  60. s[ZT_FINGERPRINT_STRING_BUFFER_LENGTH-1] = 0; // sanity check, ensure always zero terminated
  61. }
  62. /**
  63. * Set this fingerprint to a base32-encoded string
  64. *
  65. * @param s String to decode
  66. * @return True if string appears to be valid and of the proper length (no other checking is done)
  67. */
  68. ZT_INLINE bool fromString(const char *s) noexcept
  69. {
  70. uint8_t tmp[48 + 5];
  71. if (Utils::b32d(s,tmp,sizeof(tmp)) != sizeof(tmp))
  72. return false;
  73. _fp.address = Address(tmp).toInt();
  74. memcpy(_fp.hash,tmp + 5,48);
  75. return true;
  76. }
  77. ZT_INLINE void zero() noexcept { memoryZero(this); }
  78. ZT_INLINE unsigned long hashCode() const noexcept { return _fp.address; }
  79. ZT_INLINE operator bool() const noexcept { return (_fp.address != 0); }
  80. ZT_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)); }
  81. ZT_INLINE bool operator!=(const Fingerprint &h) const noexcept { return !(*this == h); }
  82. ZT_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))); }
  83. ZT_INLINE bool operator>(const Fingerprint &h) const noexcept { return (h < *this); }
  84. ZT_INLINE bool operator<=(const Fingerprint &h) const noexcept { return !(h < *this); }
  85. ZT_INLINE bool operator>=(const Fingerprint &h) const noexcept { return !(*this < h); }
  86. private:
  87. ZT_Fingerprint _fp;
  88. };
  89. } // namespace ZeroTier
  90. #endif