ntc-rcrypto 0.1.0 → 0.2.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: 014b37d0b14bd5a1bc2472553a24f4e9096dae19961305e7a86bca2494ac88e0
4
- data.tar.gz: f6b09ae301d179227725b6a98ac22b37fd0a81a188c49a64f4c7d11fdbe16497
3
+ metadata.gz: 674c20e578775c72001210a0178227aa67d6eee3832c643cbb0a6b6181812f8d
4
+ data.tar.gz: da9b39654af22be99b64610260430903a4c0becbd28305f3087756bd77628402
5
5
  SHA512:
6
- metadata.gz: b816de8c6bdbf36c91925ff3f33cb9e10456088743242c2c36fb41415d802dc35d33fe0dc479269ff109f6996f339910c4ea85beecd50a71c2ad0412fe2dc8a3
7
- data.tar.gz: 83fbb70f907106e4f25e56e360061d410cf75bd0d0b8af0067083888043f82e0825e0988534e8af2e1f1ddf97459fd9ffb07df33fab0a69977feb3e76b6780ea
6
+ metadata.gz: e2092ef095b2e1d5eec932d9fe53d1c81ab397097690bd13c500318428620ea196817e79451954effffded48871fc06037aedba0559eda1debae318c6977a93a
7
+ data.tar.gz: f999719fef2e36779d91471273acd6ce55fb1f1ee128ba6cffc9bbefe4664a886d1e3c2df234040e4503e378681bf7a72014230a98747444d26a28b817651bed
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # ntc-rcrypto
2
- ntc-rcrypto is module ruby cryptography.
2
+ ntc-rcrypto is a module ruby cryptography.
3
3
 
4
4
  ## Installation
5
5
 
data/lib/main.rb CHANGED
@@ -1,4 +1,6 @@
1
+ # Install lib: bundle install
1
2
  # Run Terminal: /bin/bash -c "env RBENV_VERSION=2.7.0 ~/.rbenv/libexec/rbenv exec bundle exec ruby ./lib/main.rb"
3
+ # /bin/bash -c "env RBENV_VERSION=3.3.5 ~/.rbenv/libexec/rbenv exec bundle exec ruby ./lib/main.rb"
2
4
 
3
5
  require 'rcrypto'
4
6
 
@@ -45,27 +47,28 @@ sss = Rcrypto::SSS.new
45
47
  # puts rs
46
48
  # puts rs.length # 109
47
49
 
48
- # # Test1
49
- # s = "nghiatcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
50
- # puts s
51
- # puts s.length
52
- # # creates a set of shares
53
- # arr = sss.create(3, 6, s, false)
54
- # # puts "================== arr"
55
- # # puts arr
56
- # # puts "=================="
57
- # # combines shares into secret
58
- # s1 = sss.combine(arr[0...3], false)
59
- # puts s1
60
- # puts s1.length
61
- #
62
- # s2 = sss.combine(arr[3...6], false)
63
- # puts s2
64
- # puts s2.length
65
- #
66
- # s3 = sss.combine(arr[1...5], false)
67
- # puts s3
68
- # puts s3.length
50
+ # Test1
51
+ s = "nghiatcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
52
+ puts s
53
+ puts s.length
54
+ # creates a set of shares
55
+ arr = sss.create(3, 6, s, false)
56
+ # puts "================== arr"
57
+ # puts arr
58
+ # puts "=================="
59
+ # combines shares into secret
60
+ s1 = sss.combine(arr[0...3], false)
61
+ puts s1
62
+ puts s1.length
63
+
64
+ s2 = sss.combine(arr[3...6], false)
65
+ puts s2
66
+ puts s2.length
67
+
68
+ s3 = sss.combine(arr[1...5], false)
69
+ puts s3
70
+ puts s3.length
71
+ puts s3 == s
69
72
 
70
73
 
71
74
  # # Test2
@@ -98,27 +101,28 @@ sss = Rcrypto::SSS.new
98
101
  # puts s3.length
99
102
 
100
103
 
101
- # # Test3
102
- # s = "nghiatcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
103
- # puts s
104
- # puts s.length
105
- # # creates a set of shares
106
- # arr = sss.create(3, 6, s, true)
107
- # # puts "================== arr"
108
- # # puts arr
109
- # # puts "=================="
110
- # # combines shares into secret
111
- # s1 = sss.combine(arr[0...3], true)
112
- # puts s1
113
- # puts s1.length
114
- #
115
- # s2 = sss.combine(arr[3...6], true)
116
- # puts s2
117
- # puts s2.length
118
- #
119
- # s3 = sss.combine(arr[1...5], true)
120
- # puts s3
121
- # puts s3.length
104
+ # Test3
105
+ s = "nghiatcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
106
+ puts s
107
+ puts s.length
108
+ # creates a set of shares
109
+ arr = sss.create(3, 6, s, true)
110
+ # puts "================== arr"
111
+ # puts arr
112
+ # puts "=================="
113
+ # combines shares into secret
114
+ s1 = sss.combine(arr[0...3], true)
115
+ puts s1
116
+ puts s1.length
117
+
118
+ s2 = sss.combine(arr[3...6], true)
119
+ puts s2
120
+ puts s2.length
121
+
122
+ s3 = sss.combine(arr[1...5], true)
123
+ puts s3
124
+ puts s3.length
125
+ puts s3 == s
122
126
 
123
127
 
124
128
  # # Test4
@@ -149,3 +153,49 @@ sss = Rcrypto::SSS.new
149
153
  # s3 = sss.combine(arr[1...5], true)
150
154
  # puts s3
151
155
  # puts s3.length
156
+
157
+ # Test5 Test full Hex with special cases not Latin symbols
158
+ s = "бар" # Cyrillic
159
+ puts s
160
+ puts s.length
161
+ # creates a set of shares
162
+ arr = sss.create(3, 6, s, false)
163
+ # puts "================== arr"
164
+ # puts arr
165
+ # puts "=================="
166
+ # combines shares into secret
167
+ s1 = sss.combine(arr[0...3], false)
168
+ puts s1
169
+ puts s1.length
170
+
171
+ s2 = sss.combine(arr[3...6], false)
172
+ puts s2
173
+ puts s2.length
174
+
175
+ s3 = sss.combine(arr[1...5], false)
176
+ puts s3
177
+ puts s3.length
178
+ puts s3 == s
179
+
180
+ # Test6 Test full Base64Url with special cases not Latin symbols
181
+ s = "бар" # Cyrillic
182
+ puts s
183
+ puts s.length
184
+ # creates a set of shares
185
+ arr = sss.create(3, 6, s, true)
186
+ # puts "================== arr"
187
+ # puts arr
188
+ # puts "=================="
189
+ # combines shares into secret
190
+ s1 = sss.combine(arr[0...3], true)
191
+ puts s1
192
+ puts s1.length
193
+
194
+ s2 = sss.combine(arr[3...6], true)
195
+ puts s2
196
+ puts s2.length
197
+
198
+ s3 = sss.combine(arr[1...5], true)
199
+ puts s3
200
+ puts s3.length
201
+ puts s3 == s
data/lib/rcrypto/sss.rb CHANGED
@@ -34,18 +34,13 @@ module Rcrypto
34
34
 
35
35
  # Convert string to hex.
36
36
  def hexlify(s)
37
- a = []
38
- if s.respond_to? :each_byte
39
- s.each_byte { |b| a << sprintf('%02X', b) }
40
- else
41
- s.each { |b| a << sprintf('%02X', b) }
42
- end
37
+ a = s.encode("UTF-8").bytes.map { |b| b.to_s(16) }
43
38
  a.join.downcase
44
39
  end
45
40
 
46
41
  # Convert hex to string.
47
42
  def unhexlify(s)
48
- s.split.pack('H*')
43
+ s.split.pack('H*').force_encoding("UTF-8")
49
44
  end
50
45
 
51
46
  # Return Uint8Array binary representation of hex string.
@@ -77,16 +72,16 @@ module Rcrypto
77
72
  # Returns the Int number base10 in base64 representation; note: this is
78
73
  # not a string representation; the base64 output is exactly 256 bits long.
79
74
  def to_base64(number)
80
- hexdata = number.to_s(16)
81
- n = 64 - hexdata.length
75
+ hex_data = number.to_s(16)
76
+ n = 64 - hex_data.length
82
77
  i = 0
83
78
  while i < n
84
- hexdata = '0' + hexdata
79
+ hex_data = '0' + hex_data
85
80
  i += 1
86
81
  end
87
- u8b = hex_to_u8b(hexdata)
88
- b64data = Base64.urlsafe_encode64(u8b)
89
- b64data
82
+ u8b = hex_to_u8b(hex_data)
83
+ b64_data = Base64.urlsafe_encode64(u8b)
84
+ b64_data
90
85
  end
91
86
 
92
87
  # Returns the number base64 in base 10 Int representation; note: this is
@@ -94,23 +89,23 @@ module Rcrypto
94
89
  # bits long, and the output is an arbitrary size base 10 integer.
95
90
  def from_base64(number)
96
91
  u8b = Base64.urlsafe_decode64(number)
97
- hexdata = u8b_to_hex(u8b)
98
- rs = hexdata.to_i(16)
92
+ hex_data = u8b_to_hex(u8b)
93
+ rs = hex_data.to_i(16)
99
94
  rs
100
95
  end
101
96
 
102
97
  # Returns the Int number base10 in Hex representation; note: this is
103
98
  # not a string representation; the Hex output is exactly 256 bits long.
104
99
  def to_hex(number)
105
- hexdata = number.to_s(16)
106
- # puts hexdata
107
- n = 64 - hexdata.length
100
+ hex_data = number.to_s(16)
101
+ # puts hex_data
102
+ n = 64 - hex_data.length
108
103
  i = 0
109
104
  while i < n
110
- hexdata = '0' + hexdata
105
+ hex_data = '0' + hex_data
111
106
  i += 1
112
107
  end
113
- hexdata
108
+ hex_data
114
109
  end
115
110
 
116
111
  # Returns the number Hex in base 10 Int representation; note: this is
@@ -163,14 +158,23 @@ module Rcrypto
163
158
  result
164
159
  end
165
160
 
166
- # Remove right character '0'
167
- def trim_right(s)
161
+ # Remove right doubled characters '0' (zero byte in hex)
162
+ def trim_right_doubled_zero(s)
163
+ last = s.length
168
164
  i = s.length - 1
169
- while i >= 0 && s[i] == '0'
170
- i -= 1
165
+ while i > 2
166
+ if s[i] == '0' && s[i - 1] == '0'
167
+ last = i - 1
168
+ else
169
+ break
170
+ end
171
+ i -= 2
172
+ end
173
+ if last == s.length
174
+ s
175
+ else
176
+ s[0..(last-1)]
171
177
  end
172
- rs = s[0..i]
173
- rs
174
178
  end
175
179
 
176
180
  # Converts an array of Ints to the original byte array, removing any
@@ -181,7 +185,7 @@ module Rcrypto
181
185
  tmp = to_hex(s)
182
186
  hex_data += tmp
183
187
  end
184
- hex_data = unhexlify(trim_right(hex_data))
188
+ hex_data = unhexlify(trim_right_doubled_zero(hex_data))
185
189
  hex_data
186
190
  end
187
191
 
@@ -211,19 +215,19 @@ module Rcrypto
211
215
  # find the number of parts it represents.
212
216
  share = shares[i]
213
217
  count = share.length / 88
214
- arrsh = []
218
+ arr_sh = []
215
219
  # and for each part, find the x,y pair...
216
220
  for j in 0...count
217
- cshare = share[j * 88...(j + 1) * 88]
218
- arrxy = []
221
+ pair = share[j * 88...(j + 1) * 88]
222
+ arr_xy = []
219
223
  # decoding from Base64.
220
- x = from_base64(cshare[0...44])
221
- y = from_base64(cshare[44...88])
222
- arrxy.push(x)
223
- arrxy.push(y)
224
- arrsh.push(arrxy)
224
+ x = from_base64(pair[0...44])
225
+ y = from_base64(pair[44...88])
226
+ arr_xy.push(x)
227
+ arr_xy.push(y)
228
+ arr_sh.push(arr_xy)
225
229
  end
226
- secrets.push(arrsh)
230
+ secrets.push(arr_sh)
227
231
  end
228
232
  secrets
229
233
  end
@@ -268,19 +272,19 @@ module Rcrypto
268
272
  # find the number of parts it represents.
269
273
  share = shares[i]
270
274
  count = share.length / 128
271
- arrsh = []
275
+ arr_sh = []
272
276
  # and for each part, find the x,y pair...
273
277
  for j in 0...count
274
- cshare = share[j * 128...(j + 1) * 128]
275
- arrxy = []
278
+ pair = share[j * 128...(j + 1) * 128]
279
+ arr_xy = []
276
280
  # decoding from Hex.
277
- x = from_hex(cshare[0...64])
278
- y = from_hex(cshare[64...128])
279
- arrxy.push(x)
280
- arrxy.push(y)
281
- arrsh.push(arrxy)
281
+ x = from_hex(pair[0...64])
282
+ y = from_hex(pair[64...128])
283
+ arr_xy.push(x)
284
+ arr_xy.push(y)
285
+ arr_sh.push(arr_xy)
282
286
  end
283
- secrets.push(arrsh)
287
+ secrets.push(arr_sh)
284
288
  end
285
289
  secrets
286
290
  end
@@ -340,8 +344,8 @@ module Rcrypto
340
344
  # polynomial[parts][minimum]
341
345
  polynomial = []
342
346
  for i in 0...secrets.length
343
- subpoly = []
344
- subpoly.push(secrets[i])
347
+ sub_poly = []
348
+ sub_poly.push(secrets[i])
345
349
  j = 1
346
350
  while j < minimum
347
351
  # Each coefficient should be unique
@@ -351,10 +355,10 @@ module Rcrypto
351
355
  end
352
356
 
353
357
  numbers.append(x)
354
- subpoly.push(x)
358
+ sub_poly.push(x)
355
359
  j += 1
356
360
  end
357
- polynomial.push(subpoly)
361
+ polynomial.push(sub_poly)
358
362
  end
359
363
 
360
364
  # Create the points object; this holds the (x, y) points of each share.
@@ -422,11 +426,8 @@ module Rcrypto
422
426
  # combine them via half products.
423
427
  # x=0 ==> [(0-bx)/(ax-bx)] * ...
424
428
  bx = points[k][j][0] # bx
425
- negbx = -bx # (0 - bx)
426
- axbx = ax - bx # (ax - bx)
427
-
428
- numerator = (numerator * negbx) % @@prime # (0 - bx) * ...
429
- denominator = (denominator * axbx) % @@prime # (ax - bx) * ...
429
+ numerator = (numerator * -bx) % @@prime # (0 - bx) * ...
430
+ denominator = (denominator * (ax - bx)) % @@prime # (ax - bx) * ...
430
431
  end
431
432
  end
432
433
  # LPI product: x=0, y = ay * [(x-bx)/(ax-bx)] * ...
@@ -1,4 +1,4 @@
1
1
  module Rcrypto
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
4
4
 
data/rcrypto.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["congnghia0609@gmail.com"]
11
11
 
12
12
  spec.summary = %q{ntc-rcrypto ruby cryptography.}
13
- spec.description = %q{ntc-rcrypto is module ruby cryptography.}
13
+ spec.description = %q{ntc-rcrypto is a module ruby cryptography.}
14
14
  spec.homepage = "https://github.com/congnghia0609/ntc-rcrypto"
15
15
  spec.license = 'Apache-2.0'
16
16
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ntc-rcrypto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nghiatc
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-26 00:00:00.000000000 Z
11
+ date: 2024-10-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: ntc-rcrypto is module ruby cryptography.
13
+ description: ntc-rcrypto is a module ruby cryptography.
14
14
  email:
15
15
  - congnghia0609@gmail.com
16
16
  executables: []
@@ -52,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  requirements: []
55
- rubygems_version: 3.1.2
55
+ rubygems_version: 3.5.16
56
56
  signing_key:
57
57
  specification_version: 4
58
58
  summary: ntc-rcrypto ruby cryptography.