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.
13 #include "base/base_export.h"
14 #include "base/hash/hash.h"
15 #include "base/strings/string_piece.h"
16 #include "build/build_config.h"
20 // DEPRECATED, use GUID::GenerateRandomV4() instead.
21 BASE_EXPORT std::string GenerateGUID();
23 // DEPRECATED, use GUID::ParseCaseInsensitive() and GUID::is_valid() instead.
24 BASE_EXPORT bool IsValidGUID(StringPiece input);
25 BASE_EXPORT bool IsValidGUID(StringPiece16 input);
27 // DEPRECATED, use GUID::ParseLowercase() and GUID::is_valid() instead.
28 BASE_EXPORT bool IsValidGUIDOutputString(StringPiece input);
30 // For unit testing purposes only. Do not use outside of tests.
31 BASE_EXPORT std::string RandomDataToGUIDString(const uint64_t bytes[2]);
33 class BASE_EXPORT GUID {
35 // Generate a 128-bit random GUID in the form of version 4. see RFC 4122,
36 // section 4.4. The format of GUID version 4 must be
37 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where y is one of [8, 9, a, b]. The
38 // hexadecimal values "a" through "f" are output as lower case characters.
39 // A cryptographically secure random source will be used, but consider using
40 // UnguessableToken for greater type-safety if GUID format is unnecessary.
41 static GUID GenerateRandomV4();
43 // Returns a valid GUID if the input string conforms to the GUID format, and
44 // an invalid GUID otherwise. Note that this does NOT check if the hexadecimal
45 // values "a" through "f" are in lower case characters.
46 static GUID ParseCaseInsensitive(StringPiece input);
47 static GUID ParseCaseInsensitive(StringPiece16 input);
49 // Similar to ParseCaseInsensitive(), but all hexadecimal values "a" through
50 // "f" must be lower case characters.
51 static GUID ParseLowercase(StringPiece input);
52 static GUID ParseLowercase(StringPiece16 input);
54 // Constructs an invalid GUID.
57 GUID(const GUID& other);
58 GUID& operator=(const GUID& other);
60 bool is_valid() const { return !lowercase_.empty(); }
62 // Returns the GUID in a lowercase string format if it is valid, and an empty
63 // string otherwise. The returned value is guaranteed to be parsed by
66 // NOTE: While AsLowercaseString() is currently a trivial getter, callers
67 // should not treat it as such. When the internal type of base::GUID changes,
68 // this will be a non-trivial converter. See the TODO above `lowercase_` for
70 const std::string& AsLowercaseString() const;
72 // Invalid GUIDs are equal.
73 bool operator==(const GUID& other) const;
74 bool operator!=(const GUID& other) const;
75 bool operator<(const GUID& other) const;
76 bool operator<=(const GUID& other) const;
77 bool operator>(const GUID& other) const;
78 bool operator>=(const GUID& other) const;
81 // TODO(crbug.com/1026195): Consider using a different internal type.
82 // Most existing representations of GUIDs in the codebase use std::string,
83 // so matching the internal type will avoid inefficient string conversions
84 // during the migration to base::GUID.
86 // The lowercase form of the GUID. Empty for invalid GUIDs.
87 std::string lowercase_;
90 // For runtime usage only. Do not store the result of this hash, as it may
91 // change in future Chromium revisions.
92 struct BASE_EXPORT GUIDHash {
93 size_t operator()(const GUID& guid) const {
94 // TODO(crbug.com/1026195): Avoid converting to string to take the hash when
95 // the internal type is migrated to a non-string type.
96 return FastHash(guid.AsLowercaseString());
100 // Stream operator so GUID objects can be used in logging statements.
101 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const GUID& guid);
105 #endif // BASE_GUID_H_