|
| 1 | +import CryptoKit |
| 2 | +import Foundation |
| 3 | + |
| 4 | +let UUIDSize = 16 |
| 5 | +let SlideSize = 10 |
| 6 | + |
| 7 | +// UUIDv7 represented as 16 bytes |
| 8 | +struct UUIDv7 { |
| 9 | + var bytes: [UInt8] |
| 10 | + private var buffer: [UInt8] |
| 11 | + private var cursor: Int |
| 12 | + |
| 13 | + init() { |
| 14 | + bytes = [UInt8](repeating: 0, count: UUIDSize) |
| 15 | + buffer = [UInt8](repeating: 0, count: 96) // can do 10 UUIDs before re-randomizing buffer |
| 16 | + cursor = buffer.count |
| 17 | + } |
| 18 | + |
| 19 | + mutating func generate() -> String { |
| 20 | + let now = Int64(Date().timeIntervalSince1970 * 1000) |
| 21 | + return generateAt(ms: now) |
| 22 | + } |
| 23 | + |
| 24 | + mutating func generateAt(ms: Int64) -> String { |
| 25 | + let bytes = generateBytesAt(ms: ms) |
| 26 | + let uuidv7 = UUIDv7.format(bytes: bytes) |
| 27 | + return uuidv7 |
| 28 | + } |
| 29 | + |
| 30 | + mutating func generateBytesAt(ms: Int64) -> [UInt8] { |
| 31 | + var uuid = UUIDv7.nextSubarray(buffer: &buffer, cursor: &cursor) |
| 32 | + UUIDv7.fill(&uuid, ms: ms) |
| 33 | + let bytes = Array(uuid[0 ..< UUIDSize]) |
| 34 | + return bytes |
| 35 | + } |
| 36 | + |
| 37 | + static func fill(_ uuid: inout [UInt8], ms: Int64) { |
| 38 | + let timeHigh = UInt32(ms >> 16) // the lower 32 bits (of the upper 48 bits) |
| 39 | + let timeLow = UInt16(ms & 0xFFFF) // the lower 16 bits |
| 40 | + |
| 41 | + uuid[0] = UInt8((timeHigh >> 24) & 0xFF) |
| 42 | + uuid[1] = UInt8((timeHigh >> 16) & 0xFF) |
| 43 | + uuid[2] = UInt8((timeHigh >> 8) & 0xFF) |
| 44 | + uuid[3] = UInt8(timeHigh & 0xFF) |
| 45 | + |
| 46 | + uuid[4] = UInt8((timeLow >> 8) & 0xFF) |
| 47 | + uuid[5] = UInt8(timeLow & 0xFF) |
| 48 | + |
| 49 | + // set top 4 bits to 0b0111 (0x7 for UUID v7) |
| 50 | + uuid[6] = (uuid[6] & 0x0F) | 0x70 |
| 51 | + |
| 52 | + // set top 2 bits to 0b10 (RFC 4122 UUID variant) |
| 53 | + uuid[8] = (uuid[8] & 0x3F) | 0x80 |
| 54 | + } |
| 55 | + |
| 56 | + static func format(bytes: [UInt8]) -> String { |
| 57 | + let hexStr = bytes.map { String(format: "%02x", $0) }.joined() |
| 58 | + |
| 59 | + let part1 = hexStr.prefix(8) |
| 60 | + let part2 = hexStr.dropFirst(8).prefix(4) |
| 61 | + let part3 = hexStr.dropFirst(12).prefix(4) |
| 62 | + let part4 = hexStr.dropFirst(16).prefix(4) |
| 63 | + let part5 = hexStr.suffix(12) |
| 64 | + |
| 65 | + return "\(part1)-\(part2)-\(part3)-\(part4)-\(part5)" |
| 66 | + } |
| 67 | + |
| 68 | + static func setBuffer(bytes: [UInt8], buffer: inout [UInt8], cursor: inout Int) throws { |
| 69 | + guard bytes.count >= UUIDSize else { |
| 70 | + struct BufferTooSmallError: LocalizedError { |
| 71 | + var errorDescription: String? { "Minimum UUIDv7 buffer size is \(UUIDSize) bytes" } |
| 72 | + } |
| 73 | + throw BufferTooSmallError() |
| 74 | + } |
| 75 | + buffer = bytes |
| 76 | + cursor = buffer.count |
| 77 | + } |
| 78 | + |
| 79 | + // Advances the random buffer cursor, potentially refilling the buffer |
| 80 | + static func nextSubarray(buffer: inout [UInt8], cursor: inout Int) -> [UInt8] { |
| 81 | + cursor += 10 |
| 82 | + var end = cursor + UUIDSize |
| 83 | + if end > buffer.count { |
| 84 | + _ = SecRandomCopyBytes(kSecRandomDefault, buffer.count, &buffer) |
| 85 | + cursor = 0 |
| 86 | + end = UUIDSize |
| 87 | + } |
| 88 | + return Array(buffer[cursor ..< cursor + UUIDSize]) |
| 89 | + } |
| 90 | +} |
0 commit comments