marja@chromium.org | 9158ee0 | 2012-06-14 23:22:07 +0900 | [diff] [blame] | 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 | #include "base/guid.h" |
| 6 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 8 | #include <stdint.h> |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 9 | |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 10 | #include "base/rand_util.h" |
benchan | 71c45e9 | 2014-09-05 14:08:32 +0900 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
benchan | 71c45e9 | 2014-09-05 14:08:32 +0900 | [diff] [blame] | 13 | |
marja@chromium.org | 9158ee0 | 2012-06-14 23:22:07 +0900 | [diff] [blame] | 14 | namespace base { |
| 15 | |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | bool IsLowerHexDigit(char c) { |
| 19 | return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); |
| 20 | } |
| 21 | |
| 22 | bool IsValidGUIDInternal(const base::StringPiece& guid, bool strict) { |
marja@chromium.org | 9158ee0 | 2012-06-14 23:22:07 +0900 | [diff] [blame] | 23 | const size_t kGUIDLength = 36U; |
| 24 | if (guid.length() != kGUIDLength) |
| 25 | return false; |
| 26 | |
benchan | 71c45e9 | 2014-09-05 14:08:32 +0900 | [diff] [blame] | 27 | for (size_t i = 0; i < guid.length(); ++i) { |
marja@chromium.org | 9158ee0 | 2012-06-14 23:22:07 +0900 | [diff] [blame] | 28 | char current = guid[i]; |
| 29 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 30 | if (current != '-') |
| 31 | return false; |
| 32 | } else { |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 33 | if ((strict && !IsLowerHexDigit(current)) || !IsHexDigit(current)) |
marja@chromium.org | 9158ee0 | 2012-06-14 23:22:07 +0900 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return true; |
| 39 | } |
| 40 | |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 41 | } // namespace |
| 42 | |
| 43 | std::string GenerateGUID() { |
John Mellor | d5c2336 | 2017-09-27 01:28:19 +0900 | [diff] [blame] | 44 | uint64_t sixteen_bytes[2]; |
| 45 | // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the |
| 46 | // base version directly, and to prevent the dependency from base/ to crypto/. |
| 47 | base::RandBytes(&sixteen_bytes, sizeof(sixteen_bytes)); |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 48 | |
| 49 | // Set the GUID to version 4 as described in RFC 4122, section 4.4. |
| 50 | // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, |
| 51 | // where y is one of [8, 9, A, B]. |
| 52 | |
| 53 | // Clear the version bits and set the version to 4: |
Peter Kasting | 56e720f | 2018-01-17 14:29:09 +0900 | [diff] [blame] | 54 | sixteen_bytes[0] &= 0xffffffff'ffff0fffULL; |
| 55 | sixteen_bytes[0] |= 0x00000000'00004000ULL; |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 56 | |
| 57 | // Set the two most significant bits (bits 6 and 7) of the |
| 58 | // clock_seq_hi_and_reserved to zero and one, respectively: |
Peter Kasting | 56e720f | 2018-01-17 14:29:09 +0900 | [diff] [blame] | 59 | sixteen_bytes[1] &= 0x3fffffff'ffffffffULL; |
| 60 | sixteen_bytes[1] |= 0x80000000'00000000ULL; |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 61 | |
| 62 | return RandomDataToGUIDString(sixteen_bytes); |
| 63 | } |
| 64 | |
| 65 | bool IsValidGUID(const base::StringPiece& guid) { |
| 66 | return IsValidGUIDInternal(guid, false /* strict */); |
| 67 | } |
| 68 | |
| 69 | bool IsValidGUIDOutputString(const base::StringPiece& guid) { |
| 70 | return IsValidGUIDInternal(guid, true /* strict */); |
| 71 | } |
| 72 | |
| 73 | std::string RandomDataToGUIDString(const uint64_t bytes[2]) { |
| 74 | return StringPrintf("%08x-%04x-%04x-%04x-%012llx", |
| 75 | static_cast<unsigned int>(bytes[0] >> 32), |
| 76 | static_cast<unsigned int>((bytes[0] >> 16) & 0x0000ffff), |
| 77 | static_cast<unsigned int>(bytes[0] & 0x0000ffff), |
| 78 | static_cast<unsigned int>(bytes[1] >> 48), |
Peter Kasting | 56e720f | 2018-01-17 14:29:09 +0900 | [diff] [blame] | 79 | bytes[1] & 0x0000ffff'ffffffffULL); |
kinuko | f7e5098 | 2016-04-27 20:00:20 +0900 | [diff] [blame] | 80 | } |
| 81 | |
vabr@chromium.org | dd88808 | 2013-09-27 17:52:29 +0900 | [diff] [blame] | 82 | } // namespace base |