extid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: faeb5eea6b6861a39118d9d05b72f047811ed29c4cc7200bd695fe2f8bccdf87
4
+ data.tar.gz: 5dfe631e43b5921395e462cf5e37c4037452d4db522f815c3b30e2d4d71cf33d
5
+ SHA512:
6
+ metadata.gz: 25269a9b265b689afee7642347f289af9440bb2eaac12543ce0f942d00cad2b6ea6186b391ec88ea64e3216943a1e0f89fb746238124478ba30a71e8629f1c99
7
+ data.tar.gz: 8cc5212c3b1bdcd0a3c9a2c2974caa60743e34e6132fdefcbb6ed444829623cb7056d430fb38971e438b87f8800123a95da09781567fb74b3aeb3ef12ce6ead2
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in extid.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ extid (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.16.3)
10
+ rake (13.0.6)
11
+
12
+ PLATFORMS
13
+ x86_64-darwin-21
14
+ x86_64-linux
15
+
16
+ DEPENDENCIES
17
+ extid!
18
+ minitest (~> 5.0)
19
+ rake (~> 13.0)
20
+
21
+ BUNDLED WITH
22
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Jack Christensen
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,38 @@
1
+ # ExtID
2
+
3
+ It can be valuable to internally use a serial integer as an ID without revealing that ID to the outside world. extid
4
+ uses AES-128 to convert to and from an external ID that cannot feasibly be decoded without the secret key.
5
+
6
+ This prevents outsiders from quantifying the usage of your application by observing the rate of increase of IDs as well
7
+ as provides protection against brute force crawling of all resources.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```
14
+ $ bundle add extid
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```
20
+ $ gem install extid
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'extid'
27
+
28
+ prefix = "user"
29
+ key = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].pack('C*')
30
+ type = ExtID::Type.new prefix, key
31
+ type.encode(1) # => "user_13189a6ae4ab07ae70a3aabd30be99de"
32
+ type.decode("user_13189a6ae4ab07ae70a3aabd30be99de") # => 1
33
+ ```
34
+
35
+ ## Other Implementations
36
+
37
+ * [Go](https://github.com/jackc/go-extid)
38
+ * [PostgreSQL](https://github.com/jackc/pg-extid)
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
data/extid.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/extid/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "extid"
7
+ spec.version = ExtID::VERSION
8
+ spec.authors = ["Jack Christensen"]
9
+ spec.email = ["jack@jackchristensen.com"]
10
+
11
+ spec.summary = "Convert integers to and from opaque external IDs"
12
+ spec.description = "It can be valuable to internally use a serial integer as an ID without revealing that ID to the outside world. extid uses AES-128 to convert to and from an external ID that cannot feasibly be decoded without the secret key."
13
+ spec.homepage = "https://github.com/jackc/ruby-extid"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(__dir__) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "example-gem", "~> 1.0"
32
+
33
+ # For more information and examples about making a new gem, check out our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ExtID
4
+ VERSION = "0.1.0"
5
+ end
data/lib/extid.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+ require_relative "extid/version"
5
+
6
+ module ExtID
7
+ MinInt64 = -9223372036854775808
8
+ MaxInt64 = 9223372036854775807
9
+
10
+ class Type
11
+ def initialize(prefix, key)
12
+ raise ArgumentError, "key must be exactly 16 bytes" unless key.bytesize == 16
13
+
14
+ @prefix = prefix + "_"
15
+ @key = key
16
+ end
17
+
18
+ def encode(n)
19
+ raise ArgumentError, "n is too small" unless n >= MinInt64
20
+ raise ArgumentError, "n is too big" unless n <= MaxInt64
21
+
22
+ binary_number = [n].pack("q>")
23
+ raise ArgumentError, "n could not be encoded into 64-bit binary" unless binary_number.bytesize == 8
24
+ plaintext = binary_number + "\x0\x0\x0\x0\x0\x0\x0\x0"
25
+
26
+ cipher = OpenSSL::Cipher.new('AES-128-ECB')
27
+ cipher.encrypt
28
+ cipher.key = @key
29
+ cipher.padding = 0
30
+
31
+ ciphertext = cipher.update(plaintext) + cipher.final
32
+ @prefix + ciphertext.unpack("H*").first
33
+ end
34
+
35
+ def decode(s)
36
+ raise ArgumentError, "invalid prefix" unless s.start_with?(@prefix)
37
+ hex_ciphertext = s[@prefix.size..]
38
+ ciphertext = [hex_ciphertext].pack("H*")
39
+
40
+ cipher = OpenSSL::Cipher.new('AES-128-ECB')
41
+ cipher.decrypt
42
+ cipher.key = @key
43
+ cipher.padding = 0
44
+
45
+ plaintext = cipher.update(ciphertext) + cipher.final
46
+ plaintext[0, 8].unpack("q>").first
47
+ end
48
+ end
49
+ end
data/sig/extid.rbs ADDED
@@ -0,0 +1,10 @@
1
+ module ExtID
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+
5
+ def initialize: (String prefix, String key) -> void
6
+
7
+ def encode: (Integer n) -> String
8
+
9
+ def decode: (String s) -> Integer
10
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jack Christensen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: It can be valuable to internally use a serial integer as an ID without
14
+ revealing that ID to the outside world. extid uses AES-128 to convert to and from
15
+ an external ID that cannot feasibly be decoded without the secret key.
16
+ email:
17
+ - jack@jackchristensen.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - extid.gemspec
28
+ - lib/extid.rb
29
+ - lib/extid/version.rb
30
+ - sig/extid.rbs
31
+ homepage: https://github.com/jackc/ruby-extid
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/jackc/ruby-extid
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.3.7
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Convert integers to and from opaque external IDs
55
+ test_files: []