Crypto.uuid
func Crypto.uuid(): stringCrypto.uuid returns a random version 4 UUID, in lower case with hyphens.
Details
- Each call returns a new id from the operating system's secure source of randomness. No
Random.newseed affects it, so a test cannot fix the value it returns.
Basic examples
Make an id. Its value differs on every run, so this example checks its shape:
Crypto.uuid().length() // 36Two calls give two different ids:
Crypto.uuid() == Crypto.uuid() // falsePrint one:
println(Crypto.uuid())The program prints a line such as 43f692cc-2d9e-4718-94cc-98b1b5aec6d9.
Scope
The five groups hold 8, 4, 4, 4, and 12 hex digits:
Crypto.uuid().split("-").map({ x.length() }) // [8, 4, 4, 4, 12]The first digit of the third group is always 4, the version:
Crypto.uuid().slice(14, 15) // "4"The first digit of the fourth group is one of 8, 9, a, or b, the variant:
"89ab".contains(Crypto.uuid().slice(19, 20)) // trueEvery letter is lower case:
let id = Crypto.uuid()
id == id.toLower() // trueApplications
Give each record its own id with a field default. A default is evaluated afresh for each literal, so two tasks with the same title still get different ids:
type Task = { id: string = Crypto.uuid(), title: string }
let a = Task{ title: "write docs" }
let b = Task{ title: "write docs" }
a.title == b.title // true
a.id == b.id // falseKeep records in a map keyed by their ids:
type User = { id: string, name: string }
let users: Map[string, User] = Map{}
for name in ["Ann", "Bob"] {
let user = User{ id: Crypto.uuid(), name }
users[user.id] = user
}
let names = users.values()
| Array.map({ x.name })
| Array.sort()
| String.join(", ")
println(names)Ann, BobGive a scratch file a name that no other run is likely to pick. replace: false makes a clash an Err instead of an overwrite, and defer removes the file when done:
func scratch(text: string): Result[string, Error] {
let path = "upload-${Crypto.uuid()}.tmp"
try File.write(path, text, replace: false)
defer { File.remove(path) }
File.read(path)
}
println(scratch("hello").unwrap())helloCheck that a string from outside has the shape Crypto.uuid produces before using it as an id:
let shape = `^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`
let pattern = Regex.compile(shape).unwrap()
func parseId(text: string): Result[string, Error] {
if !pattern.test(text) {
return Result.fail("not a version 4 UUID: ${text}")
}
Result.ok(text)
}
println(parseId(Crypto.uuid()).isOk())
println(parseId("42"))true
Err{ error: Error{ message: "not a version 4 UUID: 42" } }Properties and relations
Without its hyphens an id is 16 bytes in hex. The high four bits of byte 6 hold the version, 4, and the top two bits of byte 8 hold the variant, 10 in binary:
let id = Crypto.uuid()
let bytes = Encoding.fromHex(id.replace("-", "")).unwrap()
assert(bytes.length() == 16)
assert(Bits.shiftRight(bytes[6], 4) == byte(4))
assert(Bits.and(bytes[8], byte(0xc0)) == byte(0x80))A thousand calls give a thousand different ids:
let ids = Array.range(0, 1000).map({ Crypto.uuid() })
assert(Set.of(ids).size() == 1000)Possible issues
An id is random, so sorting ids does not sort records by the time they were made. Store the creation time in a field of its own for that.
Crypto.uuid always gives lower case, but an id from another system may come in upper case, and == compares strings exactly. Compare with String.equalFold:
let stored = "f47ac10b-58cc-4372-a567-0e02b2c3d479"
let received = "F47AC10B-58CC-4372-A567-0E02B2C3D479"
stored == received // false
stored.equalFold(received) // trueA let evaluates Crypto.uuid() once. Call it again, or use a field default, for each value that needs its own id:
let id = Crypto.uuid()
let ids = [id, id]
ids[0] == ids[1] // trueSee also
Crypto.randomBytes, Encoding.toHex, File.tempFile, Random.new, String.equalFold
