Class: Ccrypto::Java::SecretSharingEngine

Inherits:
Object
  • Object
show all
Includes:
DataConversion
Defined in:
lib/ccrypto/java/engines/secret_sharing_engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_b64_mime, #from_hex, included, #logger, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str

Constructor Details

#initialize(*args, &block) ⇒ SecretSharingEngine

Returns a new instance of SecretSharingEngine.



13
14
15
16
17
18
19
20
21
22
# File 'lib/ccrypto/java/engines/secret_sharing_engine.rb', line 13

def initialize(*args, &block)
  @config = args.first

  raise SecretSharingException, "SecretSharingConfig is required" if not @config.is_a?(Ccrypto::SecretSharingConfig)
  sit = @config.split_into.to_i
  rp = @config.required_parts.to_i
  raise SecretSharingException, "split_into value must be more than 1" if not sit > 1 
  raise SecretSharingException, "required_parts value must be more than 0" if not rp > 0
  raise SecretSharingException, "required_parts value (#{@config.required_parts}) must be less than or equal split_into value (#{@config.split_into})." if not rp <= sit.to_i
end

Class Method Details

.combine(req, parts) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ccrypto/java/engines/secret_sharing_engine.rb', line 39

def self.combine(req, parts)
  jhash = java.util.HashMap.new
  case parts
  when Hash
    # need to lock the key to java.lang.Integer
    # as the automated conversion of JRuby will turn the key into
    # java.lang.Long instead of java.lang.Integer
    # Using Map with parameterize auto conversion will failed inside the Java
    partLength = -1
    parts.each do |k,v|
      if not v.is_a?(::Java::byte[])
        vv = to_java_bytes(v)
      else
        vv = v
      end

      if partLength == -1
        partLength = vv.length
      else
        raise SecretSharingException, "Invalid parts are given. Inconsistant part length (#{partLength} vs. #{vv.length})" if partLength != vv.length
      end

      jhash.put(java.lang.Integer.new(k),vv)
    end
  when java.util.Map
    jhash = parts
  else
    raise SecretSharingException, "Unsupported parts of #{parts.class}"
  end

  com.codahale.shamir.Scheme.join(jhash)
end

Instance Method Details

#split(secVal) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ccrypto/java/engines/secret_sharing_engine.rb', line 24

def split(secVal)
  eng = com.codahale.shamir.Scheme.new(java.security.SecureRandom.new, @config.split_into.to_i, @config.required_parts.to_i) 
  case secVal
  when Ccrypto::SecretKey
    val = secVal.to_bin
  when ::Java::byte[]
    val = secVal
  when String
    val = to_java_bytes(secVal)
  else
    raise SecretSharingException, "Unknown secret value to split (#{secVal.class})"
  end
  eng.split(val)
end