guid.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_GUID_H_
  5. #define BASE_GUID_H_
  6. #include <stdint.h>
  7. #include <iosfwd>
  8. #include <string>
  9. #include "base/base_export.h"
  10. #include "base/hash/hash.h"
  11. #include "base/strings/string_piece.h"
  12. #include "build/build_config.h"
  13. namespace base {
  14. // DEPRECATED, use GUID::GenerateRandomV4() instead.
  15. BASE_EXPORT std::string GenerateGUID();
  16. // DEPRECATED, use GUID::ParseCaseInsensitive() and GUID::is_valid() instead.
  17. BASE_EXPORT bool IsValidGUID(StringPiece input);
  18. BASE_EXPORT bool IsValidGUID(StringPiece16 input);
  19. // DEPRECATED, use GUID::ParseLowercase() and GUID::is_valid() instead.
  20. BASE_EXPORT bool IsValidGUIDOutputString(StringPiece input);
  21. // For unit testing purposes only. Do not use outside of tests.
  22. BASE_EXPORT std::string RandomDataToGUIDString(const uint64_t bytes[2]);
  23. class BASE_EXPORT GUID {
  24. public:
  25. // Generate a 128-bit random GUID in the form of version 4. see RFC 4122,
  26. // section 4.4. The format of GUID version 4 must be
  27. // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where y is one of [8, 9, a, b]. The
  28. // hexadecimal values "a" through "f" are output as lower case characters.
  29. // A cryptographically secure random source will be used, but consider using
  30. // UnguessableToken for greater type-safety if GUID format is unnecessary.
  31. static GUID GenerateRandomV4();
  32. // Returns a valid GUID if the input string conforms to the GUID format, and
  33. // an invalid GUID otherwise. Note that this does NOT check if the hexadecimal
  34. // values "a" through "f" are in lower case characters.
  35. static GUID ParseCaseInsensitive(StringPiece input);
  36. static GUID ParseCaseInsensitive(StringPiece16 input);
  37. // Similar to ParseCaseInsensitive(), but all hexadecimal values "a" through
  38. // "f" must be lower case characters.
  39. static GUID ParseLowercase(StringPiece input);
  40. static GUID ParseLowercase(StringPiece16 input);
  41. // Constructs an invalid GUID.
  42. GUID();
  43. GUID(const GUID& other);
  44. GUID& operator=(const GUID& other);
  45. GUID(GUID&& other);
  46. GUID& operator=(GUID&& other);
  47. bool is_valid() const { return !lowercase_.empty(); }
  48. // Returns the GUID in a lowercase string format if it is valid, and an empty
  49. // string otherwise. The returned value is guaranteed to be parsed by
  50. // ParseLowercase().
  51. //
  52. // NOTE: While AsLowercaseString() is currently a trivial getter, callers
  53. // should not treat it as such. When the internal type of base::GUID changes,
  54. // this will be a non-trivial converter. See the TODO above `lowercase_` for
  55. // more context.
  56. const std::string& AsLowercaseString() const;
  57. // Invalid GUIDs are equal.
  58. bool operator==(const GUID& other) const;
  59. bool operator!=(const GUID& other) const;
  60. bool operator<(const GUID& other) const;
  61. bool operator<=(const GUID& other) const;
  62. bool operator>(const GUID& other) const;
  63. bool operator>=(const GUID& other) const;
  64. private:
  65. // TODO(crbug.com/1026195): Consider using a different internal type.
  66. // Most existing representations of GUIDs in the codebase use std::string,
  67. // so matching the internal type will avoid inefficient string conversions
  68. // during the migration to base::GUID.
  69. //
  70. // The lowercase form of the GUID. Empty for invalid GUIDs.
  71. std::string lowercase_;
  72. };
  73. // For runtime usage only. Do not store the result of this hash, as it may
  74. // change in future Chromium revisions.
  75. struct BASE_EXPORT GUIDHash {
  76. size_t operator()(const GUID& guid) const {
  77. // TODO(crbug.com/1026195): Avoid converting to string to take the hash when
  78. // the internal type is migrated to a non-string type.
  79. return FastHash(guid.AsLowercaseString());
  80. }
  81. };
  82. // Stream operator so GUID objects can be used in logging statements.
  83. BASE_EXPORT std::ostream& operator<<(std::ostream& out, const GUID& guid);
  84. } // namespace base
  85. #endif // BASE_GUID_H_