tins 1.47.0 → 1.48.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: 37dea3f4959ecf698b1908647ef0a7e1c19abe61aa669bbc63aa65fde10714e4
4
- data.tar.gz: 78d9256391b2a6dd105be5bc8e0b37aef0508516ddc2cbc578e4766a4f02a50c
3
+ metadata.gz: e6498c3e89e39b8351e408abc526110d4a67b7666990a100657ebcb30d626312
4
+ data.tar.gz: a131c7c65f440c56fbfd295478acb9400ccaf27e2b07e43ea8e4e6b17f89bdd9
5
5
  SHA512:
6
- metadata.gz: 04d75c6e6264659e08574a21bf0a2899702a7765ca90e32baec72056b6a95a639b35aeef7c0ad0d81a35287b3b076b9185f029be9115ae1d0b009236a3a4ca2a
7
- data.tar.gz: df69723660df31a08011a5209d460d55a6422cc1999067e2739365866ce30f4f2135a81484d722ae9ac9d83a0ed1c9cdbfe53d1f76ee6060457ad292717cd126
6
+ metadata.gz: 2905a2fa59926a1406cbbfc2ffcf5d68f536558287e852a701b61f9df0f7dfd4e9cd98e62113806cd44015ac26bb0187a55314f6a0246c97baa4ec9bb2aad848
7
+ data.tar.gz: c5bbe5c05a230bcf2127355e021ca2c90d72ccab8260543b11f8dfd9664746ffc1ea667f7cf7433e2a740e60a51aa4b199cd614e0f811f89086bca4adf0112b8
data/CHANGES.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changes
2
2
 
3
+ ## 2025-12-05 v1.48.0
4
+
5
+ - Added `Tins::Token.analyze` class method to calculate bit strength
6
+ - Moved bit calculation logic from `initialize` to `analyze` method for reuse
7
+ - Implemented bit strength calculation using formula: `length ×
8
+ log₂(alphabet_size)`
9
+ - Added comprehensive tests for the `analyze` method with various alphabets
10
+ - Support both token string and length-based analysis
11
+ - Implemented proper error handling for required parameters
12
+ - Updated `initialize` method to use the new `analyze` method
13
+ - Test cases verify bit strength for **BASE16**, **BASE64**, and **BASE32**
14
+ alphabets
15
+ - Method returns integer bit strength for cryptographic token analysis
16
+
3
17
  ## 2025-11-14 v1.47.0
4
18
 
5
19
  - Tins::GO
data/lib/tins/token.rb CHANGED
@@ -81,7 +81,7 @@ module Tins
81
81
  bits > 0 or raise ArgumentError, 'bits has to be positive'
82
82
  length = (Math.log(1 << bits) / Math.log(alphabet.size)).ceil
83
83
  end
84
- self.bits = (Math.log(alphabet.size ** length) / Math.log(2)).floor
84
+ self.bits = self.class.analyze(alphabet:, length:)
85
85
  token = +''
86
86
  length.times { token << alphabet[random.random_number(alphabet.size)] }
87
87
  super token
@@ -91,5 +91,22 @@ module Tins
91
91
  #
92
92
  # @return [Integer] the number of bits of entropy in the token
93
93
  attr_accessor :bits
94
+
95
+ # The analyze method calculates the bit length of a token based on its
96
+ # alphabet and length.
97
+ #
98
+ # @param alphabet [String] the alphabet used for token generation, defaults
99
+ # to Tins::Token::DEFAULT_ALPHABET
100
+ # @param token [String, nil] the token string to analyze, optional
101
+ # @param length [Integer, nil] the length of the token, optional
102
+ #
103
+ # @return [Integer] the calculated bit length of the token
104
+ #
105
+ # @raise [ArgumentError] if neither token nor length is provided, or if both are provided
106
+ def self.analyze(alphabet: Tins::Token::DEFAULT_ALPHABET, token: nil, length: nil)
107
+ token.nil? ^ length.nil? or raise ArgumentError, 'either token or length is required'
108
+ length ||= token.length
109
+ (Math.log(alphabet.size ** length) / Math.log(2)).floor
110
+ end
94
111
  end
95
112
  end
data/lib/tins/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Tins
2
2
  # Tins version
3
- VERSION = '1.47.0'
3
+ VERSION = '1.48.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
data/tests/token_test.rb CHANGED
@@ -32,6 +32,37 @@ module Tins
32
32
  assert_equal 128, token.length
33
33
  assert_equal 256, token.bits
34
34
  end
35
+
36
+ def test_analyze_method
37
+ # Test with default alphabet and length
38
+ bits = Tins::Token.analyze(alphabet: Tins::Token::DEFAULT_ALPHABET, length: 22)
39
+ assert_equal 130, bits
40
+
41
+ # Test with hex alphabet and length
42
+ bits = Tins::Token.analyze(alphabet: Tins::Token::BASE16_LOWERCASE_ALPHABET, length: 32)
43
+ assert_equal 128, bits # 32 × 4 = 128 bits
44
+
45
+ # Test with base64 alphabet and length
46
+ bits = Tins::Token.analyze(alphabet: Tins::Token::BASE64_ALPHABET, length: 44)
47
+ assert_equal 264, bits # 44 × 6 = 264 bits
48
+
49
+ # Test with base32 alphabet and length
50
+ bits = Tins::Token.analyze(alphabet: Tins::Token::BASE32_ALPHABET, length: 52)
51
+ assert_equal 260, bits # 52 × 5 = 260 bits
52
+
53
+ # Test with token string instead of length
54
+ token = "5f4dcc3b5aa765d61d8327deb882cf99"
55
+ bits = Tins::Token.analyze(alphabet: Tins::Token::BASE16_LOWERCASE_ALPHABET, token: token)
56
+ assert_equal 128, bits
57
+
58
+ # Test error handling
59
+ assert_raise(ArgumentError) do
60
+ Tins::Token.analyze(alphabet: Tins::Token::DEFAULT_ALPHABET)
61
+ end
62
+
63
+ assert_raise(ArgumentError) do
64
+ Tins::Token.analyze(alphabet: Tins::Token::DEFAULT_ALPHABET, token: "test", length: 5)
65
+ end
66
+ end
35
67
  end
36
68
  end
37
-
data/tins.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: tins 1.47.0 ruby lib
2
+ # stub: tins 1.48.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "tins".freeze
6
- s.version = "1.47.0".freeze
6
+ s.version = "1.48.0".freeze
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tins
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.47.0
4
+ version: 1.48.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank