Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uuid5 #685

Merged
merged 7 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.2.0"
version = "3.3.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Please check the list below for the specific syntax you need.

## Supported features

* `uuid5` generates a version 5 universally unique identifier (UUID), as specified by RFC 4122 ([#28761]). (since Compat 3.3.0)

* `dot` now has a 3-argument method `dot(x, A, y)` without storing the intermediate result `A*y` ([#32739]). (since Compat 3.2.0)

* `pkgdir(m)` returns the root directory of the package that imported module `m` ([#33128]). (since Compat 3.2.0)
Expand Down Expand Up @@ -112,3 +114,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#33128]: https://github.com/JuliaLang/julia/pull/33128
[#33736]: http://github.com/JuliaLang/julia/pull/33736
[#32968]: https://github.com/JuliaLang/julia/pull/32968
[#28761]: https://github.com/JuliaLang/julia/pull/28761
27 changes: 27 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,33 @@ if VERSION < v"1.4.0-DEV.551"
Base.filter(f, t::Base.Any16) = Tuple(filter(f, collect(t)))
end

# https://github.com/JuliaLang/julia/pull/28761
export uuid5
if VERSION < v"1.1.0-DEV.326"
import SHA
import UUIDs: UUID
function uuid5(ns::UUID, name::String)
nsbytes = zeros(UInt8, 16)
nsv = ns.value
for idx in Base.OneTo(16)
nsbytes[idx] = nsv >> 120
nsv = nsv << 8
end
hash_result = SHA.sha1(append!(nsbytes, convert(Vector{UInt8}, codeunits(unescape_string(name)))))
# set version number to 5
hash_result[7] = (hash_result[7] & 0x0F) | (0x50)
hash_result[9] = (hash_result[9] & 0x3F) | (0x80)
v = zero(UInt128)
#use only the first 16 bytes of the SHA1 hash
for idx in Base.OneTo(16)
v = (v << 0x08) | hash_result[idx]
end
return UUID(v)
end
else
using UUIDs: uuid5
end

include("deprecated.jl")

end # module Compat
44 changes: 44 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Compat
using Test
using UUIDs: UUID, uuid1, uuid_version

@test isempty(detect_ambiguities(Base, Core, Compat))

Expand Down Expand Up @@ -214,4 +215,47 @@ end
@test filter(x -> x<2, (longtuple..., 1.5)) === (1, 1.5)
end

# https://github.com/JuliaLang/julia/pull/28761
@testset "uuid5" begin
u1 = uuid1()
u5 = uuid5(u1, "julia")
@test uuid_version(u5) == 5
@test u5 == UUID(string(u5)) == UUID(GenericString(string(u5)))
@test u5 == UUID(UInt128(u5))

following_uuids = [
UUID("22b4a8a1-e548-4eeb-9270-60426d66a48e"),
UUID("30ea6cfd-c270-569f-b4cb-795dead63686"),
UUID("31099374-e3a0-5fde-9482-791c639bf29b"),
UUID("6b34b357-a348-53aa-8c71-fb9b06c3a51e"),
UUID("fdbd7d4d-c462-59cc-ae6a-0c3b010240e2"),
UUID("d8cc6298-75d5-57e0-996c-279259ab365c"),
]

for (idx, init_uuid) in enumerate(following_uuids[1:end-1])
next_id = uuid5(init_uuid, "julia")
@test next_id == following_uuids[idx+1]
end

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

# Python-generated UUID following each of the standard namespaces
standard_namespace_uuids = [
(namespace_dns, UUID("00ca23ad-40ef-500c-a910-157de3950d07")),
(namespace_oid, UUID("b7bf72b0-fb4e-538b-952a-3be296f07f6d")),
(namespace_url, UUID("997cd5be-4705-5439-9fe6-d77b18d612e5")),
(namespace_x500, UUID("993c6684-82e7-5cdb-bd46-9bff0362e6a9")),
]

for (init_uuid, next_uuid) in standard_namespace_uuids
result = uuid5(init_uuid, "julia")
@test next_uuid == result
end
end

nothing