• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37521

pending completion
#37521

push

local

web-flow
Update current stable version in README.md (#49543)

72556 of 83503 relevant lines covered (86.89%)

32577274.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.15
/stdlib/UUIDs/src/UUIDs.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4
This module provides universally unique identifiers (UUIDs),
5
along with functions creating the different variants.
6
"""
7
module UUIDs
8

9
using Random
10

11
import SHA
12

13
export UUID, uuid1, uuid4, uuid5, uuid_version
14

15
import Base: UUID
16

17
"""
18
    uuid_version(u::UUID) -> Int
19

20
Inspects the given UUID and returns its version
21
(see [RFC 4122](https://www.ietf.org/rfc/rfc4122)).
22

23
# Examples
24
```jldoctest
25
julia> uuid_version(uuid4())
26
4
27
```
28
"""
29
uuid_version(u::UUID) = Int((u.value >> 76) & 0xf)
×
30

31
# Some UUID namespaces provided in the appendix of RFC 4122
32
# https://tools.ietf.org/html/rfc4122.html#appendix-C
33
const namespace_dns  = UUID(0x6ba7b8109dad11d180b400c04fd430c8) # 6ba7b810-9dad-11d1-80b4-00c04fd430c8
34
const namespace_url  = UUID(0x6ba7b8119dad11d180b400c04fd430c8) # 6ba7b811-9dad-11d1-80b4-00c04fd430c8
35
const namespace_oid  = UUID(0x6ba7b8129dad11d180b400c04fd430c8) # 6ba7b812-9dad-11d1-80b4-00c04fd430c8
36
const namespace_x500 = UUID(0x6ba7b8149dad11d180b400c04fd430c8) # 6ba7b814-9dad-11d1-80b4-00c04fd430c8
37

38
"""
39
    uuid1([rng::AbstractRNG]) -> UUID
40

41
Generates a version 1 (time-based) universally unique identifier (UUID), as specified
42
by RFC 4122. Note that the Node ID is randomly generated (does not identify the host)
43
according to section 4.5 of the RFC.
44

45
The default rng used by `uuid1` is not `GLOBAL_RNG` and every invocation of `uuid1()` without
46
an argument should be expected to return a unique identifier. Importantly, the outputs of
47
`uuid1` do not repeat even when `Random.seed!(seed)` is called. Currently (as of Julia 1.6),
48
`uuid1` uses `Random.RandomDevice` as the default rng. However, this is an implementation
49
detail that may change in the future.
50

51
!!! compat "Julia 1.6"
52
    The output of `uuid1` does not depend on `GLOBAL_RNG` as of Julia 1.6.
53

54
# Examples
55
```jldoctest; filter = r"[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}"
56
julia> rng = MersenneTwister(1234);
57

58
julia> uuid1(rng)
59
UUID("cfc395e8-590f-11e8-1f13-43a2532b2fa8")
60
```
61
"""
62
function uuid1(rng::AbstractRNG=Random.RandomDevice())
×
63
    u = rand(rng, UInt128)
×
64

65
    # mask off clock sequence and node
66
    u &= 0x00000000000000003fffffffffffffff
×
67

68
    # set the unicast/multicast bit and version
69
    u |= 0x00000000000010000000010000000000
×
70

71
    # 0x01b21dd213814000 is the number of 100 nanosecond intervals
72
    # between the UUID epoch and Unix epoch
73
    timestamp = round(UInt64, time() * 1e7) + 0x01b21dd213814000
×
74
    ts_low = timestamp & typemax(UInt32)
×
75
    ts_mid = (timestamp >> 32) & typemax(UInt16)
×
76
    ts_hi = (timestamp >> 48) & 0x0fff
×
77

78
    u |= UInt128(ts_low) << 96
×
79
    u |= UInt128(ts_mid) << 80
×
80
    u |= UInt128(ts_hi) << 64
×
81

82
    UUID(u)
×
83
end
84

85
"""
86
    uuid4([rng::AbstractRNG]) -> UUID
87

88
Generates a version 4 (random or pseudo-random) universally unique identifier (UUID),
89
as specified by RFC 4122.
90

91
The default rng used by `uuid4` is not `GLOBAL_RNG` and every invocation of `uuid4()` without
92
an argument should be expected to return a unique identifier. Importantly, the outputs of
93
`uuid4` do not repeat even when `Random.seed!(seed)` is called. Currently (as of Julia 1.6),
94
`uuid4` uses `Random.RandomDevice` as the default rng. However, this is an implementation
95
detail that may change in the future.
96

97
!!! compat "Julia 1.6"
98
    The output of `uuid4` does not depend on `GLOBAL_RNG` as of Julia 1.6.
99

100
# Examples
101
```jldoctest
102
julia> rng = MersenneTwister(1234);
103

104
julia> uuid4(rng)
105
UUID("7a052949-c101-4ca3-9a7e-43a2532b2fa8")
106
```
107
"""
108
function uuid4(rng::AbstractRNG=Random.RandomDevice())
43✔
109
    u = rand(rng, UInt128)
86✔
110
    u &= 0xffffffffffff0fff3fffffffffffffff
43✔
111
    u |= 0x00000000000040008000000000000000
43✔
112
    UUID(u)
43✔
113
end
114

115
"""
116
    uuid5(ns::UUID, name::String) -> UUID
117

118
Generates a version 5 (namespace and domain-based) universally unique identifier (UUID),
119
as specified by RFC 4122.
120

121
!!! compat "Julia 1.1"
122
    This function requires at least Julia 1.1.
123

124
# Examples
125
```jldoctest
126
julia> rng = MersenneTwister(1234);
127

128
julia> u4 = uuid4(rng)
129
UUID("7a052949-c101-4ca3-9a7e-43a2532b2fa8")
130

131
julia> u5 = uuid5(u4, "julia")
132
UUID("086cc5bb-2461-57d8-8068-0aed7f5b5cd1")
133
```
134
"""
135
function uuid5(ns::UUID, name::String)
×
136
    nsbytes = zeros(UInt8, 16)
×
137
    nsv = ns.value
×
138
    for idx in Base.OneTo(16)
×
139
        nsbytes[idx] = nsv >> 120
×
140
        nsv = nsv << 8
×
141
    end
×
142
    hash_result = SHA.sha1(append!(nsbytes, convert(Vector{UInt8}, codeunits(unescape_string(name)))))
×
143
    # set version number to 5
144
    hash_result[7] = (hash_result[7] & 0x0F) | (0x50)
×
145
    hash_result[9] = (hash_result[9] & 0x3F) | (0x80)
×
146
    v = zero(UInt128)
×
147
    #use only the first 16 bytes of the SHA1 hash
148
    for idx in Base.OneTo(16)
×
149
        v = (v << 0x08) | hash_result[idx]
×
150
    end
×
151
    return UUID(v)
×
152
end
153

154
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc