uuid-v9 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +47 -0
- data/lib/uuid-v9.rb +108 -0
- data/spec/uuid-v9_spec.rb +133 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 30c3ab5576199b652feaba4dd0b27d093b0f0ca2f360235928db75cbed640f0c
|
4
|
+
data.tar.gz: c3dbea2d7e23028bc48bb5a8615f92daa8d191cf5711cdba5b0e46d244174690
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a8d7628ee54094f1627f42723e2953c1805ca1629e8701f485882a96e9e3c4b98106a0abc34d80b4c3c4df71a1431405ff5aec6eb5e01f96981e85388b02952c
|
7
|
+
data.tar.gz: 26eaa520c36ffe03a75f25edbd301989353f14e3e504b480e392a56d5e0af380e66be8ff315a93ac01480442cb6dcec2021014d724840a062a7728d399177a22
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 JHunt
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# UUID v9
|
2
|
+
|
3
|
+
## Fast, lightweight, zero-dependency Ruby implementation of UUID version 9
|
4
|
+
|
5
|
+
The v9 UUID supports both sequential (time-based) and non-sequential (random) UUIDs with an optional prefix of up to four bytes, an optional checksum, and sufficient randomness to avoid collisions. It uses the UNIX timestamp for sequential UUIDs and CRC-8 for checksums. A version digit can be added if desired, but is omitted by default.
|
6
|
+
|
7
|
+
To learn more about UUID v9, please visit the website: https://uuidv9.jhunt.dev
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install UUID v9 from Rubygems
|
12
|
+
|
13
|
+
```bash
|
14
|
+
gem install uuid_v9
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'uuid_v9'
|
21
|
+
|
22
|
+
ordered_id = UUIDv9::UUIDv9.generate
|
23
|
+
prefixed_ordered_id = UUIDv9::UUIDv9.generate(prefix: 'a1b2c3d4')
|
24
|
+
unordered_id = UUIDv9::UUIDv9.generate(timestamp: false)
|
25
|
+
prefixed_unordered_id = UUIDv9::UUIDv9.generate(prefix: 'a1b2c3d4', timestamp: false)
|
26
|
+
ordered_id_with_checksum = UUIDv9::UUIDv9.generate(checksum: true)
|
27
|
+
ordered_id_with_version = UUIDv9::UUIDv9.generate(version: true)
|
28
|
+
ordered_id_with_legacy_mode = UUIDv9::UUIDv9.generate(legacy: true)
|
29
|
+
|
30
|
+
is_valid = UUIDv9::UUIDv9.valid?(ordered_id)
|
31
|
+
is_valid_with_checksum = UUIDv9::UUIDv9.valid?(ordered_id_with_checksum, checksum: true)
|
32
|
+
is_valid_with_version = UUIDv9::UUIDv9.valid?(ordered_id_with_version, version: true)
|
33
|
+
```
|
34
|
+
|
35
|
+
## Backward Compatibility
|
36
|
+
|
37
|
+
Some UUID validators check for specific features of v1 or v4 UUIDs. This causes some valid v9 UUIDs to appear invalid. Three possible workarounds are:
|
38
|
+
|
39
|
+
1) Use the built-in validator (recommended)
|
40
|
+
2) Use legacy mode*
|
41
|
+
3) Bypass the validator (not recommended)
|
42
|
+
|
43
|
+
_*Legacy mode adds version and variant digits to immitate v1 or v4 UUIDs depending on the presence of a timestamp._
|
44
|
+
|
45
|
+
## License
|
46
|
+
|
47
|
+
This project is licensed under the [MIT License](LICENSE).
|
data/lib/uuid-v9.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
class UUIDv9
|
2
|
+
UUID_REGEX = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
|
3
|
+
BASE16_REGEX = /^[0-9a-fA-F]+$/
|
4
|
+
|
5
|
+
def self.calc_checksum(hex_string)
|
6
|
+
data = hex_string.scan(/.{1,2}/).map { |byte| byte.to_i(16) }
|
7
|
+
polynomial = 0x07
|
8
|
+
crc = 0x00
|
9
|
+
|
10
|
+
data.each do |byte|
|
11
|
+
crc ^= byte
|
12
|
+
8.times do
|
13
|
+
if crc & 0x80 != 0
|
14
|
+
crc = (crc << 1) ^ polynomial
|
15
|
+
else
|
16
|
+
crc <<= 1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
crc &= 0xFF
|
22
|
+
crc.to_s(16).rjust(2, '0')
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.verify_checksum(uuid)
|
26
|
+
base16_string = uuid.delete('-')[0, 30]
|
27
|
+
crc = calc_checksum(base16_string)
|
28
|
+
crc == uuid[34, 2]
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.check_version(uuid, version = nil)
|
32
|
+
version_digit = uuid[14]
|
33
|
+
variant_digit = uuid[19]
|
34
|
+
(!version || version_digit == version) &&
|
35
|
+
(version_digit == '9' || (['1', '4'].include?(version_digit) && "89abAB".include?(variant_digit)))
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.is_uuid?(uuid)
|
39
|
+
!uuid.empty? && UUID_REGEX.match?(uuid)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.is_valid_uuidv9?(uuid, options)
|
43
|
+
is_uuid?(uuid) &&
|
44
|
+
(!options.key?(:checksum) || !options[:checksum] || verify_checksum(uuid)) &&
|
45
|
+
(!options.key?(:version) || !options[:version] || check_version(uuid))
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.random_bytes(count)
|
49
|
+
Array.new(count) { rand(0..15).to_s(16) }.join
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.random_char(chars)
|
53
|
+
chars[rand(chars.length)]
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.is_base16?(str)
|
57
|
+
BASE16_REGEX.match?(str)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.validate_prefix(prefix)
|
61
|
+
raise ArgumentError, 'Prefix must be a string' if prefix.nil?
|
62
|
+
raise ArgumentError, 'Prefix must be no more than 8 characters' if prefix.length > 8
|
63
|
+
raise ArgumentError, 'Prefix must be only hexadecimal characters' unless is_base16?(prefix)
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.add_dashes(str)
|
67
|
+
"#{str[0, 8]}-#{str[8, 4]}-#{str[12, 4]}-#{str[16, 4]}-#{str[20..]}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.generate(options = {})
|
71
|
+
prefix = options.fetch(:prefix, nil).to_s
|
72
|
+
timestamp = options.fetch(:timestamp, true)
|
73
|
+
checksum = options.fetch(:checksum, false)
|
74
|
+
version = options.fetch(:version, false)
|
75
|
+
legacy = options.fetch(:legacy, false)
|
76
|
+
|
77
|
+
if prefix && !prefix.empty?
|
78
|
+
validate_prefix(prefix)
|
79
|
+
prefix = prefix.downcase
|
80
|
+
end
|
81
|
+
|
82
|
+
center = case timestamp
|
83
|
+
when true
|
84
|
+
Time.now.to_i.to_s(16)
|
85
|
+
when Time
|
86
|
+
timestamp.to_i.to_s(16)
|
87
|
+
when Numeric, String
|
88
|
+
Time.at(timestamp.to_i).to_i.to_s(16)
|
89
|
+
else
|
90
|
+
''
|
91
|
+
end
|
92
|
+
|
93
|
+
suffix_length = 32 - prefix.length - center.length - (checksum ? 2 : 0) - (legacy ? 2 : (version ? 1 : 0))
|
94
|
+
suffix = random_bytes(suffix_length)
|
95
|
+
|
96
|
+
joined = "#{prefix}#{center}#{suffix}"
|
97
|
+
|
98
|
+
if legacy
|
99
|
+
joined = "#{joined[0, 12]}#{center.length > 0 ? '1' : '4'}#{joined[12, 3]}#{random_char('89ab')}#{joined[15..]}"
|
100
|
+
elsif version
|
101
|
+
joined = "#{joined[0, 12]}9#{joined[12..]}"
|
102
|
+
end
|
103
|
+
|
104
|
+
joined += calc_checksum(joined) if checksum
|
105
|
+
|
106
|
+
add_dashes(joined)
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'rspec/autorun'
|
2
|
+
require_relative '../lib/uuid-v9'
|
3
|
+
|
4
|
+
UUID_REGEX = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
|
5
|
+
UUID_V1_REGEX = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-1[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/
|
6
|
+
UUID_V4_REGEX = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/
|
7
|
+
UUID_V9_REGEX = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-9[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/
|
8
|
+
|
9
|
+
describe 'UUIDv9' do
|
10
|
+
it 'should validate as a UUID' do
|
11
|
+
id1 = UUIDv9.generate()
|
12
|
+
id2 = UUIDv9.generate({ prefix: 'a1b2c3d4' })
|
13
|
+
id3 = UUIDv9.generate({ prefix: 'a1b2c3d4', timestamp: false })
|
14
|
+
id4 = UUIDv9.generate({ prefix: 'a1b2c3d4', checksum: true })
|
15
|
+
id5 = UUIDv9.generate({ prefix: 'a1b2c3d4', checksum: true, version: true })
|
16
|
+
id6 = UUIDv9.generate({ prefix: 'a1b2c3d4', checksum: true, legacy: true })
|
17
|
+
|
18
|
+
expect(UUID_REGEX.match?(id1)).to be true
|
19
|
+
expect(UUID_REGEX.match?(id2)).to be true
|
20
|
+
expect(UUID_REGEX.match?(id3)).to be true
|
21
|
+
expect(UUID_REGEX.match?(id4)).to be true
|
22
|
+
expect(UUID_REGEX.match?(id5)).to be true
|
23
|
+
expect(UUID_REGEX.match?(id6)).to be true
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should generate sequential UUIDs' do
|
27
|
+
id1 = UUIDv9.generate()
|
28
|
+
sleep(1)
|
29
|
+
id2 = UUIDv9.generate()
|
30
|
+
sleep(1)
|
31
|
+
id3 = UUIDv9.generate()
|
32
|
+
|
33
|
+
expect(id1 < id2).to be true
|
34
|
+
expect(id2 < id3).to be true
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should generate sequential UUIDs with a prefix' do
|
38
|
+
id1 = UUIDv9.generate({ prefix: 'a1b2c3d4' })
|
39
|
+
sleep(1)
|
40
|
+
id2 = UUIDv9.generate({ prefix: 'a1b2c3d4' })
|
41
|
+
sleep(1)
|
42
|
+
id3 = UUIDv9.generate({ prefix: 'a1b2c3d4' })
|
43
|
+
|
44
|
+
expect(id1 < id2).to be true
|
45
|
+
expect(id2 < id3).to be true
|
46
|
+
expect(id1[0, 8]).to eq 'a1b2c3d4'
|
47
|
+
expect(id2[0, 8]).to eq 'a1b2c3d4'
|
48
|
+
expect(id3[0, 8]).to eq 'a1b2c3d4'
|
49
|
+
expect(id1[9, 8]).to eq id2[9, 8]
|
50
|
+
expect(id2[9, 8]).to eq id3[9, 8]
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should generate non-sequential UUIDs' do
|
54
|
+
id_s = UUIDv9.generate({ timestamp: false })
|
55
|
+
sleep(1)
|
56
|
+
id_ns = UUIDv9.generate({ timestamp: false })
|
57
|
+
|
58
|
+
expect(id_s[0, 4]).not_to eq id_ns[0, 4]
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should generate non-sequential UUIDs with a prefix' do
|
62
|
+
id_s = UUIDv9.generate({ prefix: 'a1b2c3d4', timestamp: false })
|
63
|
+
sleep(1)
|
64
|
+
id_ns = UUIDv9.generate({ prefix: 'a1b2c3d4', timestamp: false })
|
65
|
+
|
66
|
+
expect(id_s[0, 8]).to eq 'a1b2c3d4'
|
67
|
+
expect(id_ns[0, 8]).to eq 'a1b2c3d4'
|
68
|
+
expect(id_s[9, 8]).not_to eq id_ns[9, 8]
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should generate UUIDs with a checksum' do
|
72
|
+
id1 = UUIDv9.generate({ checksum: true })
|
73
|
+
id2 = UUIDv9.generate({ timestamp: false, checksum: true })
|
74
|
+
|
75
|
+
expect(UUID_REGEX.match?(id1)).to be true
|
76
|
+
expect(UUID_REGEX.match?(id2)).to be true
|
77
|
+
expect(UUIDv9.verify_checksum(id1)).to be true
|
78
|
+
expect(UUIDv9.verify_checksum(id2)).to be true
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should generate UUIDs with a version' do
|
82
|
+
id1 = UUIDv9.generate({ version: true })
|
83
|
+
id2 = UUIDv9.generate({ timestamp: false, version: true })
|
84
|
+
|
85
|
+
expect(UUID_REGEX.match?(id1)).to be true
|
86
|
+
expect(UUID_REGEX.match?(id2)).to be true
|
87
|
+
expect(UUID_V9_REGEX.match?(id1)).to be true
|
88
|
+
expect(UUID_V9_REGEX.match?(id2)).to be true
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should generate backward compatible UUIDs' do
|
92
|
+
id1 = UUIDv9.generate({ checksum: true, legacy: true })
|
93
|
+
id2 = UUIDv9.generate({ prefix: 'a1b2c3d4', legacy: true })
|
94
|
+
id3 = UUIDv9.generate({ timestamp: false, legacy: true })
|
95
|
+
id4 = UUIDv9.generate({ prefix: 'a1b2c3d4', timestamp: false, legacy: true })
|
96
|
+
|
97
|
+
expect(UUID_REGEX.match?(id1)).to be true
|
98
|
+
expect(UUID_REGEX.match?(id2)).to be true
|
99
|
+
expect(UUID_REGEX.match?(id3)).to be true
|
100
|
+
expect(UUID_REGEX.match?(id4)).to be true
|
101
|
+
expect(UUID_V1_REGEX.match?(id1)).to be true
|
102
|
+
expect(UUID_V1_REGEX.match?(id2)).to be true
|
103
|
+
expect(UUID_V4_REGEX.match?(id3)).to be true
|
104
|
+
expect(UUID_V4_REGEX.match?(id4)).to be true
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should correctly validate and verify checksum' do
|
108
|
+
id1 = UUIDv9.generate({ checksum: true })
|
109
|
+
id2 = UUIDv9.generate({ timestamp: false, checksum: true })
|
110
|
+
id3 = UUIDv9.generate({ prefix: 'a1b2c3d4', checksum: true })
|
111
|
+
id4 = UUIDv9.generate({ prefix: 'a1b2c3d4', timestamp: false, checksum: true })
|
112
|
+
id5 = UUIDv9.generate({ checksum: true, version: true })
|
113
|
+
id6 = UUIDv9.generate({ checksum: true, legacy: true })
|
114
|
+
id7 = UUIDv9.generate({ timestamp: false, checksum: true, legacy: true })
|
115
|
+
|
116
|
+
expect(UUIDv9.is_uuid?(id1)).to be true
|
117
|
+
expect(UUIDv9.is_uuid?('not-a-real-uuid')).to be false
|
118
|
+
expect(UUIDv9.is_valid_uuidv9?(id1, { checksum: true })).to be true
|
119
|
+
expect(UUIDv9.is_valid_uuidv9?(id2, { checksum: true })).to be true
|
120
|
+
expect(UUIDv9.is_valid_uuidv9?(id3, { checksum: true })).to be true
|
121
|
+
expect(UUIDv9.is_valid_uuidv9?(id4, { checksum: true })).to be true
|
122
|
+
expect(UUIDv9.is_valid_uuidv9?(id5, { checksum: true, version: true })).to be true
|
123
|
+
expect(UUIDv9.is_valid_uuidv9?(id6, { checksum: true, version: true })).to be true
|
124
|
+
expect(UUIDv9.is_valid_uuidv9?(id7, { checksum: true, version: true })).to be true
|
125
|
+
expect(UUIDv9.verify_checksum(id1)).to be true
|
126
|
+
expect(UUIDv9.verify_checksum(id2)).to be true
|
127
|
+
expect(UUIDv9.verify_checksum(id3)).to be true
|
128
|
+
expect(UUIDv9.verify_checksum(id4)).to be true
|
129
|
+
expect(UUIDv9.verify_checksum(id5)).to be true
|
130
|
+
expect(UUIDv9.verify_checksum(id6)).to be true
|
131
|
+
expect(UUIDv9.verify_checksum(id7)).to be true
|
132
|
+
end
|
133
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uuid-v9
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- JHunt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: '"The v9 UUID supports both sequential (time-based) and non-sequential
|
14
|
+
(random) UUIDs with an optional prefix of up to four bytes, an optional checksum,
|
15
|
+
and sufficient randomness to avoid collisions. It uses the UNIX timestamp for sequential
|
16
|
+
UUIDs and CRC-8 for checksums. A version digit can be added if desired, but is omitted
|
17
|
+
by default."'
|
18
|
+
email:
|
19
|
+
- hello@jhunt.dev
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- lib/uuid-v9.rb
|
27
|
+
- spec/uuid-v9_spec.rb
|
28
|
+
homepage: https://uuidv9.jhunt.dev
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubygems_version: 3.5.16
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: '"Fast, lightweight, zero-dependency Ruby implementation of UUID version
|
51
|
+
9"'
|
52
|
+
test_files: []
|