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.
13 #include "base/base_export.h"
14 #include "base/containers/span.h"
15 #include "base/hash/hash.h"
16 #include "base/strings/string_piece.h"
17 #include "base/types/pass_key.h"
18 #include "build/build_config.h"
21 class FileSystemAccessManagerImpl;
26 // DEPRECATED, use GUID::GenerateRandomV4() instead.
27 BASE_EXPORT std::string GenerateGUID();
29 // DEPRECATED, use GUID::ParseCaseInsensitive() and GUID::is_valid() instead.
30 BASE_EXPORT bool IsValidGUID(StringPiece input);
31 BASE_EXPORT bool IsValidGUID(StringPiece16 input);
33 // DEPRECATED, use GUID::ParseLowercase() and GUID::is_valid() instead.
34 BASE_EXPORT bool IsValidGUIDOutputString(StringPiece input);
36 // For unit testing purposes only. Do not use outside of tests.
37 BASE_EXPORT std::string RandomDataToGUIDString(const uint64_t bytes[2]);
39 class BASE_EXPORT GUID {
41 // Length in bytes of the input required to format the input as a GUID in the
43 static constexpr size_t kGuidV4InputLength = 16;
45 // Generate a 128-bit random GUID in the form of version 4. see RFC 4122,
46 // section 4.4. The format of GUID version 4 must be
47 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where y is one of [8, 9, a, b]. The
48 // hexadecimal values "a" through "f" are output as lower case characters.
49 // A cryptographically secure random source will be used, but consider using
50 // UnguessableToken for greater type-safety if GUID format is unnecessary.
51 static GUID GenerateRandomV4();
53 // Formats a sequence of 16 random bytes as a GUID in the form of version 4.
55 // - have been randomly generated (e.g. created from an UnguessableToken), and
56 // - be of length 16 (this is checked at compile-time).
57 // Despite taking 128 bits of randomness, certain bits will always be
58 // masked over to adhere to the V4 GUID format.
59 // Useful in cases where an opaque identifier that is generated from stable
60 // inputs needs to be formatted as a V4 GUID. Currently only exposed to the
61 // File System Access API to return a V4 GUID for the getUniqueId() method.
62 static GUID FormatRandomDataAsV4(
63 base::span<const uint8_t, kGuidV4InputLength> input,
64 base::PassKey<content::FileSystemAccessManagerImpl> pass_key);
65 static GUID FormatRandomDataAsV4ForTesting(
66 base::span<const uint8_t, kGuidV4InputLength> input);
68 // Returns a valid GUID if the input string conforms to the GUID format, and
69 // an invalid GUID otherwise. Note that this does NOT check if the hexadecimal
70 // values "a" through "f" are in lower case characters.
71 static GUID ParseCaseInsensitive(StringPiece input);
72 static GUID ParseCaseInsensitive(StringPiece16 input);
74 // Similar to ParseCaseInsensitive(), but all hexadecimal values "a" through
75 // "f" must be lower case characters.
76 static GUID ParseLowercase(StringPiece input);
77 static GUID ParseLowercase(StringPiece16 input);
79 // Constructs an invalid GUID.
82 GUID(const GUID& other);
83 GUID& operator=(const GUID& other);
85 GUID& operator=(GUID&& other);
87 bool is_valid() const { return !lowercase_.empty(); }
89 // Returns the GUID in a lowercase string format if it is valid, and an empty
90 // string otherwise. The returned value is guaranteed to be parsed by
93 // NOTE: While AsLowercaseString() is currently a trivial getter, callers
94 // should not treat it as such. When the internal type of base::GUID changes,
95 // this will be a non-trivial converter. See the TODO above `lowercase_` for
97 const std::string& AsLowercaseString() const;
99 // Invalid GUIDs are equal.
100 bool operator==(const GUID& other) const;
101 bool operator!=(const GUID& other) const;
102 bool operator<(const GUID& other) const;
103 bool operator<=(const GUID& other) const;
104 bool operator>(const GUID& other) const;
105 bool operator>=(const GUID& other) const;
108 static GUID FormatRandomDataAsV4Impl(
109 base::span<const uint8_t, kGuidV4InputLength> input);
111 // TODO(crbug.com/1026195): Consider using a different internal type.
112 // Most existing representations of GUIDs in the codebase use std::string,
113 // so matching the internal type will avoid inefficient string conversions
114 // during the migration to base::GUID.
116 // The lowercase form of the GUID. Empty for invalid GUIDs.
117 std::string lowercase_;
120 // For runtime usage only. Do not store the result of this hash, as it may
121 // change in future Chromium revisions.
122 struct BASE_EXPORT GUIDHash {
123 size_t operator()(const GUID& guid) const {
124 // TODO(crbug.com/1026195): Avoid converting to string to take the hash when
125 // the internal type is migrated to a non-string type.
126 return FastHash(guid.AsLowercaseString());
130 // Stream operator so GUID objects can be used in logging statements.
131 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const GUID& guid);
135 #endif // BASE_GUID_H_