Class: Ccrypto::Ruby::SecretSharingEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/ccrypto/ruby/engines/secret_sharing_engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ SecretSharingEngine

Returns a new instance of SecretSharingEngine.

Raises:

  • (SecretSharingException)


8
9
10
11
12
13
# File 'lib/ccrypto/ruby/engines/secret_sharing_engine.rb', line 8

def initialize(*args, &block)
  @config = args.first 
  raise SecretSharingException, "SecretSharingConfig is required" if not @config.is_a?(Ccrypto::SecretSharingConfig)
  raise SecretSharingException, "split_into value must be more than 1" if not @config.split_into.to_i > 1
  raise SecretSharingException, "required_parts value (#{@config.required_parts}) must be less than or equal split_into value (#{@config.split_into})." if not @config.required_parts.to_i < @config.split_into.to_i
end

Class Method Details

.combine(req, parts) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/ccrypto/ruby/engines/secret_sharing_engine.rb', line 38

def self.combine(req, parts)

  parts.each do |k,v|
    parts[k] = v.chars.map(&:ord)
  end

  ss = ShamirSharing.new(req)
  ss.recover_secretdata(parts.to_a)
end

Instance Method Details

#split(secVal) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ccrypto/ruby/engines/secret_sharing_engine.rb', line 15

def split(secVal)

  case secVal
  when MemoryBuffer
    data = secVal.bytes
  when String
    data = secVal
  when Ccrypto::SecretKey
    data = secVal.to_bin
  else
    raise SecretSharingException, "Unknown how to process split for #{secVal.class}"
  end

  eng = ShamirSharing.new(@config.required_parts.to_i, data)
  shares = []
  (1..@config.split_into.to_i).each do |i|
    res = eng.compute_share(i)
    res[1] = res[1].map { |v| v.chr }.join
    shares << res
  end        
  shares
end