ntc-rcrypto 0.1.0 → 0.2.1
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 +4 -4
- data/README.md +1 -1
- data/lib/main.rb +92 -42
- data/lib/rcrypto/sss.rb +56 -54
- data/lib/rcrypto/version.rb +1 -1
- data/rcrypto.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2136cbe7a8af4858b246be9c84ec972c089e65b7d10ca8b263a7614bdbc2d1c4
|
4
|
+
data.tar.gz: 2a2051b1e54702db769866fbe307d4eeaada6b3740ca037934c2d180f29feaf1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3389aea3a3c9e09cac0223242725e9a52394d04b1751c2044af53a69e845809ae8db1ce7aaf15d732eac084820b4e73dcf7ea7d8034bee836c1c59863d5b2686
|
7
|
+
data.tar.gz: 5d794c6749e74e620da550229ca7ae00a8760cc40f4fb23833d33875a7b1315288cc1d95216b730755ed06eebd4cdaca8e176e7373886d108ff62be3faf7d163
|
data/README.md
CHANGED
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
|
-
#
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
#
|
53
|
-
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
#
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
#
|
106
|
-
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
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
|
-
|
81
|
-
n = 64 -
|
75
|
+
hex_data = number.to_s(16)
|
76
|
+
n = 64 - hex_data.length
|
82
77
|
i = 0
|
83
78
|
while i < n
|
84
|
-
|
79
|
+
hex_data = '0' + hex_data
|
85
80
|
i += 1
|
86
81
|
end
|
87
|
-
u8b = hex_to_u8b(
|
88
|
-
|
89
|
-
|
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
|
-
|
98
|
-
rs =
|
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
|
-
|
106
|
-
# puts
|
107
|
-
n = 64 -
|
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
|
-
|
105
|
+
hex_data = '0' + hex_data
|
111
106
|
i += 1
|
112
107
|
end
|
113
|
-
|
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
|
167
|
-
def
|
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
|
170
|
-
i
|
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(
|
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
|
-
|
218
|
+
arr_sh = []
|
215
219
|
# and for each part, find the x,y pair...
|
216
220
|
for j in 0...count
|
217
|
-
|
218
|
-
|
221
|
+
pair = share[j * 88...(j + 1) * 88]
|
222
|
+
arr_xy = []
|
219
223
|
# decoding from Base64.
|
220
|
-
x = from_base64(
|
221
|
-
y = from_base64(
|
222
|
-
|
223
|
-
|
224
|
-
|
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(
|
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
|
-
|
275
|
+
arr_sh = []
|
272
276
|
# and for each part, find the x,y pair...
|
273
277
|
for j in 0...count
|
274
|
-
|
275
|
-
|
278
|
+
pair = share[j * 128...(j + 1) * 128]
|
279
|
+
arr_xy = []
|
276
280
|
# decoding from Hex.
|
277
|
-
x = from_hex(
|
278
|
-
y = from_hex(
|
279
|
-
|
280
|
-
|
281
|
-
|
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(
|
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
|
-
|
344
|
-
|
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
|
-
|
358
|
+
sub_poly.push(x)
|
355
359
|
j += 1
|
356
360
|
end
|
357
|
-
polynomial.push(
|
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.
|
@@ -372,6 +376,7 @@ module Rcrypto
|
|
372
376
|
while in_numbers(numbers, x)
|
373
377
|
x = random_number()
|
374
378
|
end
|
379
|
+
numbers.append(x)
|
375
380
|
y = evaluate_polynomial(polynomial, j, x)
|
376
381
|
if is_base64
|
377
382
|
s += to_base64(x)
|
@@ -422,11 +427,8 @@ module Rcrypto
|
|
422
427
|
# combine them via half products.
|
423
428
|
# x=0 ==> [(0-bx)/(ax-bx)] * ...
|
424
429
|
bx = points[k][j][0] # bx
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
numerator = (numerator * negbx) % @@prime # (0 - bx) * ...
|
429
|
-
denominator = (denominator * axbx) % @@prime # (ax - bx) * ...
|
430
|
+
numerator = (numerator * -bx) % @@prime # (0 - bx) * ...
|
431
|
+
denominator = (denominator * (ax - bx)) % @@prime # (ax - bx) * ...
|
430
432
|
end
|
431
433
|
end
|
432
434
|
# LPI product: x=0, y = ay * [(x-bx)/(ax-bx)] * ...
|
data/lib/rcrypto/version.rb
CHANGED
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
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nghiatc
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-02-17 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.
|
55
|
+
rubygems_version: 3.5.16
|
56
56
|
signing_key:
|
57
57
|
specification_version: 4
|
58
58
|
summary: ntc-rcrypto ruby cryptography.
|