1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_UUID_H_ 6 #define BASE_UUID_H_ 7 8 #include <stdint.h> 9 10 #include <compare> 11 #include <iosfwd> 12 #include <string> 13 #include <string_view> 14 15 #include "base/base_export.h" 16 #include "base/containers/span.h" 17 #include "base/types/pass_key.h" 18 #include "build/build_config.h" 19 20 namespace content { 21 class FileSystemAccessManagerImpl; 22 } 23 24 namespace base { 25 26 class BASE_EXPORT Uuid { 27 public: 28 // Length in bytes of the input required to format the input as a Uuid in the 29 // form of version 4. 30 static constexpr size_t kGuidV4InputLength = 16; 31 32 // Generate a 128-bit random Uuid in the form of version 4. see RFC 4122, 33 // section 4.4. The format of Uuid version 4 must be 34 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where y is one of [8, 9, a, b]. The 35 // hexadecimal values "a" through "f" are output as lower case characters. 36 // A cryptographically secure random source will be used, but consider using 37 // UnguessableToken for greater type-safety if Uuid format is unnecessary. 38 static Uuid GenerateRandomV4(); 39 40 // Formats a sequence of 16 random bytes as a Uuid in the form of version 4. 41 // `input` must: 42 // - have been randomly generated (e.g. created from an UnguessableToken), and 43 // - be of length 16 (this is checked at compile-time). 44 // Despite taking 128 bits of randomness, certain bits will always be 45 // masked over to adhere to the V4 Uuid format. 46 // Useful in cases where an opaque identifier that is generated from stable 47 // inputs needs to be formatted as a V4 Uuid. Currently only exposed to the 48 // File System Access API to return a V4 Uuid for the getUniqueId() method. 49 static Uuid FormatRandomDataAsV4( 50 base::span<const uint8_t, kGuidV4InputLength> input, 51 base::PassKey<content::FileSystemAccessManagerImpl> pass_key); 52 static Uuid FormatRandomDataAsV4ForTesting( 53 base::span<const uint8_t, kGuidV4InputLength> input); 54 55 // Returns a valid Uuid if the input string conforms to the Uuid format, and 56 // an invalid Uuid otherwise. Note that this does NOT check if the hexadecimal 57 // values "a" through "f" are in lower case characters. 58 static Uuid ParseCaseInsensitive(std::string_view input); 59 static Uuid ParseCaseInsensitive(std::u16string_view input); 60 61 // Similar to ParseCaseInsensitive(), but all hexadecimal values "a" through 62 // "f" must be lower case characters. 63 static Uuid ParseLowercase(std::string_view input); 64 static Uuid ParseLowercase(std::u16string_view input); 65 66 // Constructs an invalid Uuid. 67 Uuid(); 68 69 Uuid(const Uuid& other); 70 Uuid& operator=(const Uuid& other); 71 Uuid(Uuid&& other); 72 Uuid& operator=(Uuid&& other); 73 is_valid()74 bool is_valid() const { return !lowercase_.empty(); } 75 76 // Returns the Uuid in a lowercase string format if it is valid, and an empty 77 // string otherwise. The returned value is guaranteed to be parsed by 78 // ParseLowercase(). 79 // 80 // NOTE: While AsLowercaseString() is currently a trivial getter, callers 81 // should not treat it as such. When the internal type of base::Uuid changes, 82 // this will be a non-trivial converter. See the TODO above `lowercase_` for 83 // more context. 84 const std::string& AsLowercaseString() const; 85 86 // Invalid Uuids are equal. 87 friend bool operator==(const Uuid&, const Uuid&) = default; 88 // Uuids are 128bit chunks of data so must be indistinguishable if equivalent. 89 friend std::strong_ordering operator<=>(const Uuid&, const Uuid&) = default; 90 91 private: 92 static Uuid FormatRandomDataAsV4Impl( 93 base::span<const uint8_t, kGuidV4InputLength> input); 94 95 // TODO(crbug.com/1026195): Consider using a different internal type. 96 // Most existing representations of Uuids in the codebase use std::string, 97 // so matching the internal type will avoid inefficient string conversions 98 // during the migration to base::Uuid. 99 // 100 // The lowercase form of the Uuid. Empty for invalid Uuids. 101 std::string lowercase_; 102 }; 103 104 // For runtime usage only. Do not store the result of this hash, as it may 105 // change in future Chromium revisions. 106 struct BASE_EXPORT UuidHash { 107 size_t operator()(const Uuid& uuid) const; 108 }; 109 110 // Stream operator so Uuid objects can be used in logging statements. 111 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Uuid& uuid); 112 113 } // namespace base 114 115 #endif // BASE_UUID_H_ 116