|
| 1 | +import Foundation |
| 2 | + |
| 3 | +extension UUID { |
| 4 | + /// The nil UUID with all 128 bits set to zero. |
| 5 | + public static let null = UUID(uuid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) |
| 6 | + |
| 7 | + /// Creates a new time-based (version 1) UUID. |
| 8 | + /// |
| 9 | + /// The UUID is built from the current time and the Ethernet address. When the Ethernet address |
| 10 | + /// cannot be determined, a random value is used instead. |
| 11 | + /// |
| 12 | + /// When a random node is used, the least significand bit of byte 10 is set to 1. |
| 13 | + /// |
| 14 | + /// - warning: This method may expose the Ethernet address of the user's computer. If privacy is |
| 15 | + /// a concern, you should use a random UUID (`.v4`) instead. |
| 16 | + public static func v1() -> UUID { |
| 17 | + UUIDv1().rawValue |
| 18 | + } |
| 19 | + |
| 20 | + /// Creates a new name-based (version 3) UUID using MD5 hashing. |
| 21 | + /// |
| 22 | + /// - warning: Unless backward compatibility is an issue, SHA-1 (`.v5`) should be preferred. |
| 23 | + /// - parameter name: A value that is unique within the given namespace. |
| 24 | + /// - parameter namespace: An UUID namespace used as a prefix for `name`. |
| 25 | + public static func v3<T>(name: T, namespace: Namespace) -> UUID where T: Collection, T.Element == UInt8 { |
| 26 | + UUIDv3(name: name, namespace: namespace).rawValue |
| 27 | + } |
| 28 | + |
| 29 | + /// Creates a new name-based (version 3) UUID using MD5 hashing. |
| 30 | + /// |
| 31 | + /// - warning: Unless backward compatibility is an issue, SHA-1 (`.v5`) should be preferred. |
| 32 | + /// - parameter name: A value that is unique within the given namespace. |
| 33 | + /// - parameter namespace: An UUID namespace used as a prefix for `name`. |
| 34 | + public static func v3<T>(name: T, namespace: Namespace) -> UUID where T: DataProtocol { |
| 35 | + UUIDv3(name: name, namespace: namespace).rawValue |
| 36 | + } |
| 37 | + |
| 38 | + /// Creates a new name-based (version 3) UUID using MD5 hashing. |
| 39 | + /// |
| 40 | + /// - warning: Unless backward compatibility is an issue, SHA-1 (`.v5`) should be preferred. |
| 41 | + /// - parameter name: A value that is unique within the given namespace. |
| 42 | + /// - parameter namespace: An UUID namespace used as a prefix for `name`. |
| 43 | + public static func v3<T>(name: T, namespace: Namespace) -> UUID where T: StringProtocol { |
| 44 | + UUIDv3(name: name, namespace: namespace).rawValue |
| 45 | + } |
| 46 | + |
| 47 | + /// Creates a new random (version 4) UUID. |
| 48 | + /// |
| 49 | + /// The system's default source of random is used to create the new UUID. |
| 50 | + public static func v4() -> UUID { |
| 51 | + UUIDv4().rawValue |
| 52 | + } |
| 53 | + |
| 54 | + /// Creates a new random (version 4) UUID, using the given generator as a source for randomness. |
| 55 | + /// |
| 56 | + /// - parameter generator: The random number generator to use when creating the new random UUID. |
| 57 | + public static func v4<T>(using generator: inout T) -> UUID where T: RandomNumberGenerator { |
| 58 | + UUIDv4(using: &generator).rawValue |
| 59 | + } |
| 60 | + |
| 61 | + /// Creates a new name-based (version 5) UUID using SHA-1 hashing. |
| 62 | + /// |
| 63 | + /// - parameter name: A value that is unique within the given namespace. |
| 64 | + /// - parameter namespace: An UUID namespace used as a prefix for `name`. |
| 65 | + public static func v5<T>(name: T, namespace: Namespace) -> UUID where T: Collection, T.Element == UInt8 { |
| 66 | + UUIDv5(name: name, namespace: namespace).rawValue |
| 67 | + } |
| 68 | + |
| 69 | + /// Creates a new name-based (version 5) UUID using SHA-1 hashing. |
| 70 | + /// |
| 71 | + /// - parameter name: A value that is unique within the given namespace. |
| 72 | + /// - parameter namespace: An UUID namespace used as a prefix for `name`. |
| 73 | + public static func v5<T>(name: T, namespace: Namespace) -> UUID where T: DataProtocol { |
| 74 | + UUIDv5(name: name, namespace: namespace).rawValue |
| 75 | + } |
| 76 | + |
| 77 | + /// Creates a new name-based (version 5) UUID using SHA-1 hashing. |
| 78 | + /// |
| 79 | + /// - parameter name: A value that is unique within the given namespace. |
| 80 | + /// - parameter namespace: An UUID namespace used as a prefix for `name`. |
| 81 | + public static func v5<T>(name: T, namespace: Namespace) -> UUID where T: StringProtocol { |
| 82 | + UUIDv5(name: name, namespace: namespace).rawValue |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +extension UUID { |
| 87 | + internal init(bytes source: UnsafeRawBufferPointer, version: Version) { |
| 88 | + precondition(source.count >= 16) |
| 89 | + var bytes = UUID.null.uuid |
| 90 | + withUnsafeMutableBytes(of: &bytes) { target in |
| 91 | + _ = source.copyBytes(to: target, count: 16) |
| 92 | + } |
| 93 | + bytes.8 = bytes.8 & 0x3f | 0x80 |
| 94 | + bytes.6 = bytes.6 & 0x0f | version << 4 |
| 95 | + self.init(uuid: bytes) |
| 96 | + assert(self.version == version) |
| 97 | + } |
| 98 | +} |
0 commit comments