Skip to content

Swift library for generating and working with UUIDs

License

Notifications You must be signed in to change notification settings

baarde/uuid-kit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UUIDKit

MIT License Swift 5.1

UUIDKit is a Swift library for generating and working with Universally Unique Identifiers (UUIDs) as specified in RFC 4122.

It extends the existing Foundation's UUID type.

Generating UUIDs

The default initializer for UUID returns a random (version 4) UUID. This library adds methods for creating version 1, version 3 and version 5 UUIDs.

Time-based UUIDs (UUID.v1)

UUID.v1 returns a time-based (version 1) UUID.

let uuidv1 = UUID.v1()

Instead of relying on the computer's Ethernet address to ensure global uniqueness of time-based UUIDs, this method generates a random node identifier that is used for the duration of the program's execution, as specified in RFC 4122 section 4.5. The reason is discussed in this issue.

Random UUIDs (UUID.v4)

UUID.v4 returns a random (version 4) UUID. You may pass a custom random number generator.

var generator = SystemRandomNumberGenerator()
let uuidv4 = UUID.v4(using: generator)

Name-based UUIDs (UUID.v3 and UUID.v5)

UUID.v3 and UUID.v5 return name-based UUIDs using either MD5 (version 3) or SHA-1 (version 5) hashing.

let uuidv3 = UUID.v3(name: "thats.an.example", namespace: .dns)
let uuidv5 = UUID.v5(name: "http://example.com/index.html", namespace: .url)

Name-based UUIDs are built by combining a unique name with a namespace identifier. UUIDKit contains a list of pre-defined namespaces (.dns, .url, .oid and .x500); but any UUID may be used as a namespace.

let customNamespace = UUID.Namespace(UUID(uuidString: "34cd6bf4-3f41-4717-95ea-131762f60af8")!)

License

This project is licensed for use under the MIT License (MIT). Please see the LICENSE file for more information.