uuid47 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 867e6ce9a5231bc9b4d39e1b82962817047fdc251bd4a6566d2dfc148b06d8d0
4
+ data.tar.gz: dfba986cb3dcaa85ec4923a68f9dc286ead06f45b2f9d682338651e0cc20aa2d
5
+ SHA512:
6
+ metadata.gz: 572a0f4fe2aa4786bee27eb05e8ed9c365b670e1f1dd797e3d20515b2f9c484655a1fa64243e1c95399601a9492aa716170e2da3cf6c0c7adfec70a76dbc4749
7
+ data.tar.gz: 1dece9924a2dcc5ec482317e0e31510c35a5fd69c98b89778de72da08ae11200c9dd57a1ee58ff630cc40ab4ea863918cb1f9592067f45eab38135dd87b1623e
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Ryo Kajiwara
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # UUID47
2
+
3
+ Implementation of [UUIDv47](https://github.com/stateless-me/uuidv47) in Ruby
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ ```bash
12
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
13
+ ```
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ ```bash
18
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ require 'securerandom'
25
+
26
+ key = SecureRandom.random_bytes(16)
27
+ v7 = "018f2d9f-9a2a-7def-8c3f-7b1a2c4d5e6f" # any v7 will work
28
+ v4facade = UUID47.encode_v4facade(v7, key)
29
+ v7_recoved = UUID47.decode_v4facade(v4facade, key)
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sylph01/uuid47.
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UUID47
4
+ VERSION = "0.1.0"
5
+ end
data/lib/uuid47.rb ADDED
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "uuid47/version"
4
+ require 'siphash'
5
+ require 'uuidtools'
6
+
7
+ module UUID47
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+
11
+ def self.encode_v4facade(id_v7, key)
12
+ uuid_str = UUIDTools::UUID.parse(id_v7).raw
13
+ mask = self.mask48(uuid_str, key)
14
+ # apply mask
15
+ new_uuid_str = self.xor_strings(mask, uuid_str[0..5]) + uuid_str[6..]
16
+ # then set version
17
+ self.set_version(new_uuid_str, 4)
18
+ # then convert to UUID string
19
+ UUIDTools::UUID.parse_raw(new_uuid_str).to_str
20
+ end
21
+
22
+ def self.decode_v4facade(v4facade, key)
23
+ uuid_str = UUIDTools::UUID.parse(v4facade).raw
24
+ mask = self.mask48(uuid_str, key)
25
+ # (de)apply mask
26
+ new_uuid_str = self.xor_strings(mask, uuid_str[0..5]) + uuid_str[6..]
27
+ # then set version
28
+ self.set_version(new_uuid_str, 7)
29
+ # then convert to UUID string
30
+ UUIDTools::UUID.parse_raw(new_uuid_str).to_str
31
+ end
32
+
33
+ private
34
+ def self.mask48(raw, key)
35
+ msg = ''
36
+ msg += (raw[6].ord & 0x0f).chr
37
+ msg += raw[7]
38
+ msg += (raw[8].ord & 0x3F).chr
39
+ msg += raw[9..15]
40
+ mask_int = SipHash.digest(key, msg)
41
+ mask = ''
42
+ mask += (mask_int >> 40 & 0xff).chr
43
+ mask += (mask_int >> 32 & 0xff).chr
44
+ mask += (mask_int >> 24 & 0xff).chr
45
+ mask += (mask_int >> 16 & 0xff).chr
46
+ mask += (mask_int >> 8 & 0xff).chr
47
+ mask += (mask_int >> 0 & 0xff).chr
48
+ mask
49
+ end
50
+
51
+ def self.xor_strings(a, b)
52
+ a.bytes.zip(b.bytes).map { |x, y| (x ^ y).chr }.join
53
+ end
54
+
55
+ def self.set_version(uuid_str, version)
56
+ uuid_str[6] = ((uuid_str[6].ord & 0x0f) | ((version & 0x0f) << 4)).chr
57
+ end
58
+ end
data/sig/uuid47.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Uuid47
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uuid47
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Kajiwara
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: ''
13
+ email:
14
+ - sylph01@s01.ninja
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE.txt
20
+ - README.md
21
+ - Rakefile
22
+ - lib/uuid47.rb
23
+ - lib/uuid47/version.rb
24
+ - sig/uuid47.rbs
25
+ homepage: https://github.com/sylph01/uuid47
26
+ licenses:
27
+ - MIT
28
+ metadata:
29
+ homepage_uri: https://github.com/sylph01/uuid47
30
+ source_code_uri: https://github.com/sylph01/uuid47
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 3.2.0
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.7.1
46
+ specification_version: 4
47
+ summary: UUIDv47 implementation in Ruby
48
+ test_files: []