Fingerprint.hpp 3.3 KB

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