blob: 0e0b36734866a932452048999498c0e4b9acfa56 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]3469e7e2010-10-14 20:34:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7e49ad32012-06-14 14:22:075#ifndef BASE_GUID_H_
6#define BASE_GUID_H_
[email protected]3469e7e2010-10-14 20:34:597
avi9b6f42932015-12-26 22:15:148#include <stdint.h>
9
Daniel Hosseinian16ab8cf2020-11-12 23:18:3810#include <iosfwd>
[email protected]3469e7e2010-10-14 20:34:5911#include <string>
12
[email protected]7e49ad32012-06-14 14:22:0713#include "base/base_export.h"
Austin Sullivana48afe32022-09-09 21:01:1614#include "base/containers/span.h"
Daniel Hosseinianaa27a002020-11-13 00:39:3415#include "base/hash/hash.h"
kinuko4ad1f552016-04-27 11:00:2016#include "base/strings/string_piece.h"
Austin Sullivana48afe32022-09-09 21:01:1617#include "base/types/pass_key.h"
[email protected]3469e7e2010-10-14 20:34:5918#include "build/build_config.h"
19
Austin Sullivana48afe32022-09-09 21:01:1620namespace content {
21class FileSystemAccessManagerImpl;
22}
23
[email protected]7e49ad32012-06-14 14:22:0724namespace base {
[email protected]3469e7e2010-10-14 20:34:5925
Daniel Hosseinian16ab8cf2020-11-12 23:18:3826// DEPRECATED, use GUID::GenerateRandomV4() instead.
[email protected]7e49ad32012-06-14 14:22:0727BASE_EXPORT std::string GenerateGUID();
[email protected]3469e7e2010-10-14 20:34:5928
Daniel Hosseinian16ab8cf2020-11-12 23:18:3829// DEPRECATED, use GUID::ParseCaseInsensitive() and GUID::is_valid() instead.
30BASE_EXPORT bool IsValidGUID(StringPiece input);
31BASE_EXPORT bool IsValidGUID(StringPiece16 input);
[email protected]d3d728e92010-10-20 03:24:5532
Daniel Hosseinian16ab8cf2020-11-12 23:18:3833// DEPRECATED, use GUID::ParseLowercase() and GUID::is_valid() instead.
34BASE_EXPORT bool IsValidGUIDOutputString(StringPiece input);
kinuko4ad1f552016-04-27 11:00:2035
[email protected]3469e7e2010-10-14 20:34:5936// For unit testing purposes only. Do not use outside of tests.
avi9b6f42932015-12-26 22:15:1437BASE_EXPORT std::string RandomDataToGUIDString(const uint64_t bytes[2]);
[email protected]3469e7e2010-10-14 20:34:5938
Daniel Hosseinian16ab8cf2020-11-12 23:18:3839class BASE_EXPORT GUID {
40 public:
Austin Sullivana48afe32022-09-09 21:01:1641 // Length in bytes of the input required to format the input as a GUID in the
42 // form of version 4.
43 static constexpr size_t kGuidV4InputLength = 16;
44
Daniel Hosseinian16ab8cf2020-11-12 23:18:3845 // 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();
52
Austin Sullivana48afe32022-09-09 21:01:1653 // Formats a sequence of 16 random bytes as a GUID in the form of version 4.
54 // `input` must:
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);
67
Daniel Hosseinian16ab8cf2020-11-12 23:18:3868 // 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);
73
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);
78
79 // Constructs an invalid GUID.
80 GUID();
81
82 GUID(const GUID& other);
83 GUID& operator=(const GUID& other);
Vaclav Brozek1a19c7142022-05-13 20:53:5884 GUID(GUID&& other);
85 GUID& operator=(GUID&& other);
Daniel Hosseinian16ab8cf2020-11-12 23:18:3886
87 bool is_valid() const { return !lowercase_.empty(); }
88
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
91 // ParseLowercase().
92 //
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
96 // more context.
97 const std::string& AsLowercaseString() const;
98
99 // Invalid GUIDs are equal.
100 bool operator==(const GUID& other) const;
101 bool operator!=(const GUID& other) const;
Daniel Hosseinian2ea7563b2020-11-13 21:26:54102 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;
Daniel Hosseinian16ab8cf2020-11-12 23:18:38106
107 private:
Austin Sullivana48afe32022-09-09 21:01:16108 static GUID FormatRandomDataAsV4Impl(
109 base::span<const uint8_t, kGuidV4InputLength> input);
110
Daniel Hosseinian16ab8cf2020-11-12 23:18:38111 // 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.
115 //
116 // The lowercase form of the GUID. Empty for invalid GUIDs.
117 std::string lowercase_;
118};
119
Daniel Hosseinianaa27a002020-11-13 00:39:34120// For runtime usage only. Do not store the result of this hash, as it may
121// change in future Chromium revisions.
122struct 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());
127 }
128};
129
Daniel Hosseinian16ab8cf2020-11-12 23:18:38130// Stream operator so GUID objects can be used in logging statements.
131BASE_EXPORT std::ostream& operator<<(std::ostream& out, const GUID& guid);
132
[email protected]4461e9ad2013-09-27 08:52:29133} // namespace base
[email protected]3469e7e2010-10-14 20:34:59134
[email protected]7e49ad32012-06-14 14:22:07135#endif // BASE_GUID_H_