secretsharing 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -27,6 +27,9 @@
27
27
  # show secret
28
28
  puts s.secret
29
29
 
30
+ # show password representation of secret (Base64)
31
+ puts s.secret_password
32
+
30
33
  # show shares
31
34
  s.shares.each { |share| puts share }
32
35
 
@@ -1,5 +1,6 @@
1
1
  require 'openssl'
2
2
  require 'digest/sha1'
3
+ require 'base64'
3
4
 
4
5
  module SecretSharing
5
6
  # The SecretSharing::Shamir class can be used to share random
@@ -67,6 +68,14 @@ module SecretSharing
67
68
  @secret
68
69
  end
69
70
 
71
+ # The secret in a password representation (Base64-encoded)
72
+ def secret_password
73
+ if ! secret_set? then
74
+ raise "Secret not (yet) set."
75
+ end
76
+ Base64.encode64([@secret.to_s(16)].pack('h*')).split("\n").join
77
+ end
78
+
70
79
  # Add a secret share to the object. Accepts either a SecretSharing::Shamir::Share
71
80
  # instance or a string representing one. Returns true if enough shares have
72
81
  # been added to recover the secret, false otherweise.
@@ -170,11 +179,13 @@ module SecretSharing
170
179
  def recover_secret
171
180
  @secret = OpenSSL::BN.new('0')
172
181
  @received_shares.each do |share|
173
- summand = share.y * l(share.x, @received_shares)
182
+ l_x = l(share.x, @received_shares)
183
+ summand = share.y * l_x
174
184
  summand %= share.prime
175
185
  @secret += summand
176
186
  @secret %= share.prime
177
187
  end
188
+ @secret
178
189
  end
179
190
 
180
191
  # Part of the Lagrange interpolation.
@@ -183,10 +194,12 @@ module SecretSharing
183
194
  # for more information compare Wikipedia:
184
195
  # http://en.wikipedia.org/wiki/Lagrange_form
185
196
  def l(x, shares)
186
- shares.select { |s| s.x != x }.map do |s|
187
- OpenSSL::BN.new((-s.x).to_s) *
188
- OpenSSL::BN.new((x - s.x).to_s).mod_inverse(shares[0].prime)
189
- end.inject { |p, f| p.mod_mul(f, shares[0].prime) }
197
+ (shares.select { |s| s.x != x }.map do |s|
198
+ minus_xi = OpenSSL::BN.new((-s.x).to_s)
199
+ one_over_xj_minus_xi = OpenSSL::BN.new((x - s.x).to_s) \
200
+ .mod_inverse(shares[0].prime)
201
+ minus_xi.mod_mul(one_over_xj_minus_xi, shares[0].prime)
202
+ end.inject { |p, f| p.mod_mul(f, shares[0].prime) })
190
203
  end
191
204
  end
192
205
 
data/test/test_shamir.rb CHANGED
@@ -59,6 +59,17 @@ class TestShamir < Test::Unit::TestCase
59
59
  assert_equal(s.secret, s2.secret)
60
60
  end
61
61
 
62
+ def test_recover_secret_k_eq_n_strings
63
+ s = SecretSharing::Shamir.new(2)
64
+ s.create_random_secret()
65
+
66
+ s2 = SecretSharing::Shamir.new(2)
67
+ s2 << s.shares[0].to_s
68
+ s2 << s.shares[1].to_s
69
+
70
+ assert_equal(s.secret, s2.secret)
71
+ end
72
+
62
73
  def test_recover_secret_k_le_n
63
74
  s = SecretSharing::Shamir.new(5, 3)
64
75
  s.create_random_secret()
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: secretsharing
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ version: "0.2"
5
10
  platform: ruby
6
11
  authors:
7
12
  - Alexander Klink
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-09-19 00:00:00 +02:00
17
+ date: 2010-09-26 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -41,21 +46,27 @@ rdoc_options: []
41
46
  require_paths:
42
47
  - lib
43
48
  required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
51
  - - ">="
46
52
  - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
47
56
  version: "0"
48
- version:
49
57
  required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
50
59
  requirements:
51
60
  - - ">="
52
61
  - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
53
65
  version: "0"
54
- version:
55
66
  requirements: []
56
67
 
57
68
  rubyforge_project:
58
- rubygems_version: 1.3.5
69
+ rubygems_version: 1.3.7
59
70
  signing_key:
60
71
  specification_version: 3
61
72
  summary: A library to share secrets in an information-theoretically secure way.