lite-uxid 1.3.0 → 1.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e12d0fcd0ea1e1653d22eea382fb4d770aa50dda8d9cfebc3a0633e50e01c64d
4
- data.tar.gz: 4eb22adb047d392dc1ee1645b9465c98940137f2b562635f8678d08398dc06af
3
+ metadata.gz: 1f8fdc0fac3f66d26f1fa5ed7944775912fedb7ef4a8c1d8d582ca3b8ddad11c
4
+ data.tar.gz: 9e0b2729d115fab68c449034c95ce38706680eaf0327a1f46bea2e4afb654a66
5
5
  SHA512:
6
- metadata.gz: 831968f72b1e450f5caaa4b330f236b2c0b1c6780dfe4153d68265f7a53752e12090b589c7235e92c16fd2e1ce2e58da8df7ef6321b47d13c02d514c4bb9f7f1
7
- data.tar.gz: d8491cec5de3a930c968992b2b159691b041549c890ed77a9369b5edaffda1bb6592cba992ad4f7284cd297cc7646c9b5dcefe6f64156fba08e959be57280c68
6
+ metadata.gz: 8cdb90bf7523761fe7fe9673195d888419ffc5fd0199c52bd353e39b779746b5e4bbe7e18232baf97a8bfd106afb0eb593e68818eac886aa5cc17cfa20a6bc56
7
+ data.tar.gz: 07c960b84a617349cd65c74639dbf3fee6e82ea519d0bf64ff3f52dcb75b8e6b8ff3d06c935cd58170e5987ed06ff5b47913bce7866964e24abe935c6749b383
data/CHANGELOG.md CHANGED
@@ -6,13 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.4.0] - 2022-11-20
10
+ ### Changed
11
+ - Improved global config flexibility
12
+ - Update config generator to generate dynamic salt
13
+
9
14
  ## [1.3.0] - 2022-11-20
10
15
  ### Added
11
16
  - Added uuid option
12
17
 
13
18
  ## [1.2.0] - 2022-11-19
14
19
  ### Added
15
- - Added individual character and length options
20
+ - Added individual character and size options
16
21
  ### Changed
17
22
  - Improved docs
18
23
  - Improved internal setup
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-uxid (1.3.0)
4
+ lite-uxid (1.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  Lite::Uxid is a library for generating or obfuscating ID's based on different patterns.
7
7
  It's very useful to hide the number of resources in your database and protect against enumeration attacks.
8
+ By default, it implements websafe variants of each type.
8
9
 
9
10
  ## Installation
10
11
 
@@ -25,6 +26,7 @@ Or install it yourself as:
25
26
  ## Table of Contents
26
27
 
27
28
  * [Configuration](#configuration)
29
+ * [Usage](#usage)
28
30
  * [Hashid](#hashid)
29
31
  * [NanoID](#nanoid)
30
32
  * [ULID](#ulid)
@@ -40,21 +42,36 @@ Or install it yourself as:
40
42
 
41
43
  ```ruby
42
44
  Lite::Uxid.configure do |config|
43
- config.encoding_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
44
- config.encoding_salt = 1_369_136
45
- config.hashid_length = 12
46
- config.nanoid_length = 21
47
- config.ulid_length = 26
45
+ config.hashid_charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
46
+ config.hashid_salt = 1_369_136
47
+ config.hashid_size = 16
48
+ config.nanoid_charset = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
49
+ config.nanoid_size = 21
50
+ config.ulid_charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
51
+ config.ulid_size = 26
48
52
  end
49
53
  ```
50
54
 
55
+ ## Usage
56
+
57
+ #### Instance
58
+ ```ruby
59
+ coder = Lite::Uxid::Hashid.new(10, size: 12)
60
+ coder.encode #=> '67wGI0'
61
+ ```
62
+
63
+ #### Class
64
+ ```ruby
65
+ Lite::Uxid::Hashid.decode('67wGI0', size: 12) #=> 10
66
+ ```
67
+
51
68
  ## Hashid
52
69
 
53
70
  [More information](https://hashids.org)
54
71
 
55
72
  ```ruby
56
- Lite::Uxid::Hashid.encode(10) #=> '67wGI0'
57
- Lite::Uxid::Hashid.decode('67wGI0') #=> 10
73
+ Lite::Uxid::Hashid.encode(10) #=> '1zWr1m0'
74
+ Lite::Uxid::Hashid.decode('1zWr1m0') #=> 10
58
75
  ```
59
76
 
60
77
  ## NanoID
@@ -70,12 +87,12 @@ Lite::Uxid::Nanoid.encode #=> 'sMuNUa3Cegn6r5GRQ4Ij2'
70
87
  [More information](https://github.com/ulid/spec)
71
88
 
72
89
  ```ruby
73
- Lite::Uxid::Ulid.encode #=> '01gial8st6qrroptaks2tj4smq'
90
+ Lite::Uxid::Ulid.encode #=> '01GJAY9KGR539EZF4QWYEJGSN7'
74
91
  ```
75
92
 
76
93
  ## UUID
77
94
 
78
- [More information](https://en.wikipedia.org/wiki/Universally_unique_identifier)
95
+ Implements v4 of the specification. [More information](https://en.wikipedia.org/wiki/Universally_unique_identifier)
79
96
 
80
97
  ```ruby
81
98
  Lite::Uxid::Uuid.encode #=> '4376a67e-1189-44b3-a599-7f7566bf105b'
@@ -86,7 +103,7 @@ Lite::Uxid::Uuid.encode #=> '4376a67e-1189-44b3-a599-7f7566bf105b'
86
103
  Local options can be passed to override global options.
87
104
 
88
105
  ```ruby
89
- Lite::Uxid::Ulid.encode(chars: 'abc123', length: 12) #=> 'a3b12c12c3ca'
106
+ Lite::Uxid::Ulid.encode(chars: 'abc123', size: 12) #=> 'a3b12c12c3ca'
90
107
  ```
91
108
 
92
109
  ## ActiveRecord
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Lite::Uxid.configure do |config|
4
- config.encoding_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
5
- config.encoding_salt = 1_369_136
6
- config.hashid_length = 12
7
- config.nanoid_length = 21
8
- config.ulid_length = 26
4
+ config.hashid_charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
5
+ config.hashid_salt = 1_369_136
6
+ config.hashid_size = 16
7
+ config.nanoid_charset = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
8
+ config.nanoid_size = 21
9
+ config.ulid_charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10
+ config.ulid_size = 26
9
11
  end
@@ -5,14 +5,16 @@ module Lite
5
5
 
6
6
  class Configuration
7
7
 
8
- attr_accessor :encoding_chars, :encoding_salt, :hashid_length, :nanoid_length, :ulid_length
8
+ attr_accessor :hashid_charset, :hashid_salt, :hashid_size, :nanoid_charset, :nanoid_size, :ulid_charset, :ulid_size
9
9
 
10
10
  def initialize
11
- @encoding_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
12
- @encoding_salt = 1_369_136
13
- @hashid_length = 12
14
- @nanoid_length = 21
15
- @ulid_length = 26
11
+ @hashid_charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
12
+ @hashid_salt = 1_369_136
13
+ @hashid_size = 16
14
+ @nanoid_charset = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
15
+ @nanoid_size = 21
16
+ @ulid_charset = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
17
+ @ulid_size = 26
16
18
  end
17
19
 
18
20
  end
@@ -5,11 +5,11 @@ module Lite
5
5
  class Hashid < Reversible
6
6
 
7
7
  def encode
8
- encode_chars((id + coder_salt) << coder_length)
8
+ encode_chars((id + coder_salt) << coder_size)
9
9
  end
10
10
 
11
11
  def decode
12
- (decode_chars(id) >> coder_length) - coder_salt
12
+ (decode_chars(id) >> coder_size) - coder_salt
13
13
  end
14
14
 
15
15
  private
@@ -21,8 +21,8 @@ module Lite
21
21
  str = ""
22
22
 
23
23
  while decoded_id.positive?
24
- str = "#{coder_chars[decoded_id % coder_base]}#{str}"
25
- decoded_id /= coder_base
24
+ str = "#{coder_charset[decoded_id % coder_length]}#{str}"
25
+ decoded_id /= coder_length
26
26
  end
27
27
 
28
28
  str
@@ -31,12 +31,12 @@ module Lite
31
31
  def decode_chars(encoded_id)
32
32
  pos = 0
33
33
  num = 0
34
- len = encoded_id.length
34
+ len = encoded_id.size
35
35
  max = len - 1
36
36
 
37
37
  while pos < len
38
- pow = coder_base**(max - pos)
39
- num += coder_chars.index(encoded_id[pos]) * pow
38
+ pow = coder_length**(max - pos)
39
+ num += coder_charset.index(encoded_id[pos]) * pow
40
40
  pos += 1
41
41
  end
42
42
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "securerandom" unless defined?(SecureRandom)
4
+
3
5
  module Lite
4
6
  module Uxid
5
7
  class Irreversible
@@ -34,14 +36,14 @@ module Lite
34
36
 
35
37
  private
36
38
 
37
- def coder_base
38
- @coder_base ||= coder_chars.length
39
+ def coder_bytes
40
+ @coder_bytes ||= SecureRandom.random_bytes(coder_size).bytes
39
41
  end
40
42
 
41
- def coder_chars
42
- @coder_chars ||=
43
- opts.delete(:chars) ||
44
- Lite::Uxid.configuration.encoding_chars
43
+ def coder_charset
44
+ @coder_charset ||=
45
+ opts.delete(:charset) ||
46
+ Lite::Uxid.configuration.send("#{coder_class.downcase}_charset")
45
47
  end
46
48
 
47
49
  def coder_class
@@ -49,15 +51,19 @@ module Lite
49
51
  end
50
52
 
51
53
  def coder_length
52
- @coder_length ||=
53
- opts.delete(:length) ||
54
- Lite::Uxid.configuration.send("#{coder_class.downcase}_length")
54
+ @coder_length ||= coder_charset.size
55
55
  end
56
56
 
57
57
  def coder_salt
58
58
  @coder_salt ||=
59
59
  opts.delete(:salt) ||
60
- Lite::Uxid.configuration.encoding_salt
60
+ Lite::Uxid.configuration.send("#{coder_class.downcase}_salt")
61
+ end
62
+
63
+ def coder_size
64
+ @coder_size ||=
65
+ opts.delete(:size) ||
66
+ Lite::Uxid.configuration.send("#{coder_class.downcase}_size")
61
67
  end
62
68
 
63
69
  end
@@ -5,7 +5,9 @@ module Lite
5
5
  class Nanoid < Irreversible
6
6
 
7
7
  def encode
8
- coder_chars.chars.sample(coder_length).join
8
+ (0...coder_size).each_with_object(+"") do |i, str|
9
+ str << coder_charset[coder_bytes[i] & 63]
10
+ end
9
11
  end
10
12
 
11
13
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "securerandom" unless defined?(SecureRandom)
4
-
5
3
  module Lite
6
4
  module Uxid
7
5
  class Ulid < Irreversible
@@ -10,11 +8,11 @@ module Lite
10
8
 
11
9
  def encode
12
10
  oct = octect
13
- ele = "0" * coder_length
14
- pos = coder_length - 1
11
+ ele = "0" * coder_size
12
+ pos = coder_size - 1
15
13
 
16
14
  while oct.positive?
17
- ele[pos] = coder_chars[oct & MASK]
15
+ ele[pos] = coder_charset[oct & MASK]
18
16
  oct >>= 5
19
17
  pos -= 1
20
18
  end
@@ -35,7 +33,7 @@ module Lite
35
33
 
36
34
  def unixtime_ms
37
35
  time = Time.respond_to?(:current) ? Time.current : Time.now
38
- (time.to_f * 1_000).to_i
36
+ time.to_i * 1_000
39
37
  end
40
38
 
41
39
  def unixtime_48bit
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "securerandom" unless defined?(SecureRandom)
4
-
5
3
  module Lite
6
4
  module Uxid
7
5
  class Uuid < Irreversible
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Uxid
5
5
 
6
- VERSION = "1.3.0"
6
+ VERSION = "1.4.0"
7
7
 
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-uxid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez