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 5 #ifndef BASE_GUID_H_ 6 #define BASE_GUID_H_ 7 8 #include <stdint.h> 9 10 #include <iosfwd> 11 #include <string> 12 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" 17 18 namespace base { 19 20 // DEPRECATED, use GUID::GenerateRandomV4() instead. 21 BASE_EXPORT std::string GenerateGUID(); 22 23 // DEPRECATED, use GUID::ParseCaseInsensitive() and GUID::is_valid() instead. 24 BASE_EXPORT bool IsValidGUID(StringPiece input); 25 BASE_EXPORT bool IsValidGUID(StringPiece16 input); 26 27 // DEPRECATED, use GUID::ParseLowercase() and GUID::is_valid() instead. 28 BASE_EXPORT bool IsValidGUIDOutputString(StringPiece input); 29 30 // For unit testing purposes only. Do not use outside of tests. 31 BASE_EXPORT std::string RandomDataToGUIDString(const uint64_t bytes[2]); 32 33 class BASE_EXPORT GUID { 34 public: 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(); 42 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); 48 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); 53 54 // Constructs an invalid GUID. 55 GUID(); 56 57 GUID(const GUID& other); 58 GUID& operator=(const GUID& other); 59 is_valid()60 bool is_valid() const { return !lowercase_.empty(); } 61 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 64 // ParseLowercase(). 65 // 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 69 // more context. 70 const std::string& AsLowercaseString() const; 71 72 // Invalid GUIDs are equal. 73 bool operator==(const GUID& other) const; 74 bool operator!=(const GUID& other) const; 75 76 private: 77 // TODO(crbug.com/1026195): Consider using a different internal type. 78 // Most existing representations of GUIDs in the codebase use std::string, 79 // so matching the internal type will avoid inefficient string conversions 80 // during the migration to base::GUID. 81 // 82 // The lowercase form of the GUID. Empty for invalid GUIDs. 83 std::string lowercase_; 84 }; 85 86 // For runtime usage only. Do not store the result of this hash, as it may 87 // change in future Chromium revisions. 88 struct BASE_EXPORT GUIDHash { operatorGUIDHash89 size_t operator()(const GUID& guid) const { 90 // TODO(crbug.com/1026195): Avoid converting to string to take the hash when 91 // the internal type is migrated to a non-string type. 92 return FastHash(guid.AsLowercaseString()); 93 } 94 }; 95 96 // Stream operator so GUID objects can be used in logging statements. 97 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const GUID& guid); 98 99 } // namespace base 100 101 #endif // BASE_GUID_H_ 102