Skip to content

Commit

Permalink
Add uuid5 (#685)
Browse files Browse the repository at this point in the history
* Add uuid5

* Bump version number
  • Loading branch information
tkf authored Feb 6, 2020
1 parent 601e535 commit 2121e6b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
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

2 comments on commit 2121e6b

@martinholters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/8964

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v3.3.0 -m "<description of version>" 2121e6bb227adc5a87e6b32b98c701b9e2da5d7c
git push origin v3.3.0

Please sign in to comment.