ntc-rcrypto 0.0.4 → 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: d418af6ae2b5279365489c245007dec3863765e7268bab4481074926a0dc5e3f
4
- data.tar.gz: c32826acf84e80db1a30611c4196dac565116588ec257da008ee5658501ed6d6
3
+ metadata.gz: 674c20e578775c72001210a0178227aa67d6eee3832c643cbb0a6b6181812f8d
4
+ data.tar.gz: da9b39654af22be99b64610260430903a4c0becbd28305f3087756bd77628402
5
5
  SHA512:
6
- metadata.gz: 1e4ec45b5a06df1eb2eef30e5f151a6474a26ac35248cee058c15f2dfd501d7690509c783dc4c6ef369ff66de47f49f2ecac607699e0ca9b97f6bd254c93db39
7
- data.tar.gz: 89688e0525dc327f04e2f1006c058f469ccf313c2e94bf1c21065beb90f7b1b2ad49b71fe3b9d37777ca4e0ce4254b5ed9e387d07528115b60271bb20c376c61
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,13 +158,23 @@ module Rcrypto
163
158
  result
164
159
  end
165
160
 
166
- 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
167
164
  i = s.length - 1
168
- while i >= 0 && s[i] == '0'
169
- 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)]
170
177
  end
171
- rs = s[0..i]
172
- rs
173
178
  end
174
179
 
175
180
  # Converts an array of Ints to the original byte array, removing any
@@ -180,7 +185,7 @@ module Rcrypto
180
185
  tmp = to_hex(s)
181
186
  hex_data += tmp
182
187
  end
183
- hex_data = unhexlify(trim_right(hex_data))
188
+ hex_data = unhexlify(trim_right_doubled_zero(hex_data))
184
189
  hex_data
185
190
  end
186
191
 
@@ -210,19 +215,19 @@ module Rcrypto
210
215
  # find the number of parts it represents.
211
216
  share = shares[i]
212
217
  count = share.length / 88
213
- arrsh = []
218
+ arr_sh = []
214
219
  # and for each part, find the x,y pair...
215
220
  for j in 0...count
216
- cshare = share[j * 88...(j + 1) * 88]
217
- arrxy = []
221
+ pair = share[j * 88...(j + 1) * 88]
222
+ arr_xy = []
218
223
  # decoding from Base64.
219
- x = from_base64(cshare[0...44])
220
- y = from_base64(cshare[44...88])
221
- arrxy.push(x)
222
- arrxy.push(y)
223
- 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)
224
229
  end
225
- secrets.push(arrsh)
230
+ secrets.push(arr_sh)
226
231
  end
227
232
  secrets
228
233
  end
@@ -267,19 +272,19 @@ module Rcrypto
267
272
  # find the number of parts it represents.
268
273
  share = shares[i]
269
274
  count = share.length / 128
270
- arrsh = []
275
+ arr_sh = []
271
276
  # and for each part, find the x,y pair...
272
277
  for j in 0...count
273
- cshare = share[j * 128...(j + 1) * 128]
274
- arrxy = []
278
+ pair = share[j * 128...(j + 1) * 128]
279
+ arr_xy = []
275
280
  # decoding from Hex.
276
- x = from_hex(cshare[0...64])
277
- y = from_hex(cshare[64...128])
278
- arrxy.push(x)
279
- arrxy.push(y)
280
- 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)
281
286
  end
282
- secrets.push(arrsh)
287
+ secrets.push(arr_sh)
283
288
  end
284
289
  secrets
285
290
  end
@@ -315,9 +320,13 @@ module Rcrypto
315
320
  # Verify minimum isn't greater than shares; there is no way to recreate
316
321
  # the original polynomial in our current setup, therefore it doesn't make
317
322
  # sense to generate fewer shares than are needed to reconstruct the secrets.
323
+ if minimum <= 0 || shares <= 0
324
+ raise Exception('minimum or shares is invalid')
325
+ end
318
326
  if minimum > shares
319
327
  raise Exception('cannot require more shares then existing')
320
328
  end
329
+ raise Exception('secret is NULL or empty') if secret.empty?
321
330
 
322
331
  # Convert the secrets to its respective 256-bit Int representation.
323
332
  secrets = split_secret_to_int(secret)
@@ -335,21 +344,21 @@ module Rcrypto
335
344
  # polynomial[parts][minimum]
336
345
  polynomial = []
337
346
  for i in 0...secrets.length
338
- subpoly = []
339
- subpoly.push(secrets[i])
347
+ sub_poly = []
348
+ sub_poly.push(secrets[i])
340
349
  j = 1
341
350
  while j < minimum
342
351
  # Each coefficient should be unique
343
- number = random_number()
344
- while in_numbers(numbers, number)
345
- number = random_number()
352
+ x = random_number()
353
+ while in_numbers(numbers, x)
354
+ x = random_number()
346
355
  end
347
356
 
348
- numbers.append(number)
349
- subpoly.push(number)
357
+ numbers.append(x)
358
+ sub_poly.push(x)
350
359
  j += 1
351
360
  end
352
- polynomial.push(subpoly)
361
+ polynomial.push(sub_poly)
353
362
  end
354
363
 
355
364
  # Create the points object; this holds the (x, y) points of each share.
@@ -359,23 +368,15 @@ module Rcrypto
359
368
  #
360
369
  # Note: this array is technically unnecessary due to creating result
361
370
  # in the inner loop. Can disappear later if desired.
362
- #
363
- # points[shares][parts][2]
364
- points = []
365
371
  for i in 0...shares
366
372
  s = ''
367
- arrsh = []
368
373
  for j in 0...secrets.length
369
- arrxy = []
370
374
  # generate a new x-coordinate.
371
- number = random_number()
372
- while in_numbers(numbers, number)
373
- number = random_number()
375
+ x = random_number()
376
+ while in_numbers(numbers, x)
377
+ x = random_number()
374
378
  end
375
- x = number
376
- y = evaluate_polynomial(polynomial, j, number)
377
- arrxy.push(x)
378
- arrxy.push(y)
379
+ y = evaluate_polynomial(polynomial, j, x)
379
380
  if is_base64
380
381
  s += to_base64(x)
381
382
  s += to_base64(y)
@@ -383,9 +384,7 @@ module Rcrypto
383
384
  s += to_hex(x)
384
385
  s += to_hex(y)
385
386
  end
386
- arrsh.push(arrxy)
387
387
  end
388
- points.push(arrsh)
389
388
  result.push(s)
390
389
  end
391
390
  result
@@ -427,11 +426,8 @@ module Rcrypto
427
426
  # combine them via half products.
428
427
  # x=0 ==> [(0-bx)/(ax-bx)] * ...
429
428
  bx = points[k][j][0] # bx
430
- negbx = -bx # (0 - bx)
431
- axbx = ax - bx # (ax - bx)
432
-
433
- numerator = (numerator * negbx) % @@prime # (0 - bx) * ...
434
- denominator = (denominator * axbx) % @@prime # (ax - bx) * ...
429
+ numerator = (numerator * -bx) % @@prime # (0 - bx) * ...
430
+ denominator = (denominator * (ax - bx)) % @@prime # (ax - bx) * ...
435
431
  end
436
432
  end
437
433
  # LPI product: x=0, y = ay * [(x-bx)/(ax-bx)] * ...
@@ -1,4 +1,4 @@
1
1
  module Rcrypto
2
- VERSION = "0.0.4"
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.0.4
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-25 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.