Skip to content

Instantly share code, notes, and snippets.

@ArchieR7
Created September 12, 2018 13:06
Show Gist options
  • Save ArchieR7/3e358a4f0460f7260841c90f937817ba to your computer and use it in GitHub Desktop.
Save ArchieR7/3e358a4f0460f7260841c90f937817ba to your computer and use it in GitHub Desktop.
UUID initialize with version 3, 5 and namespace
extension UUID {
enum UUIDVersion: Int {
case v3 = 3
case v5 = 5
}
enum UUIDv5NameSpace: String {
case dns = "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
case url = "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
case oid = "6ba7b812-9dad-11d1-80b4-00c04fd430c8"
case x500 = "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
}
init(version: UUIDVersion, name: String, nameSpace: UUIDv5NameSpace) {
// Get UUID bytes from name space:
let spaceUID = UUID(uuidString: nameSpace.rawValue)!.uuid
var data = withUnsafePointer(to: spaceUID) {
Data(bytes: $0, count: MemoryLayout.size(ofValue: spaceUID))
}
// Append name string in UTF-8 encoding:
data.append(contentsOf: name.utf8)
// Compute digest (MD5 or SHA1, depending on the version):
var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> Void in
switch version {
case .v3:
_ = CC_MD5(ptr, CC_LONG(data.count), &digest)
case .v5:
_ = CC_SHA1(ptr, CC_LONG(data.count), &digest)
}
}
// Set version bits:
digest[6] &= 0x0F
digest[6] |= UInt8(version.rawValue) << 4
// Set variant bits:
digest[8] &= 0x3F
digest[8] |= 0x80
// Create UUID from digest:
self = NSUUID(uuidBytes: digest) as UUID
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment