lib/ccrypto/java/engines/secret_sharing_engine.rb in ccrypto-java-0.1.0 vs lib/ccrypto/java/engines/secret_sharing_engine.rb in ccrypto-java-0.2.0

- old
+ new

@@ -1,18 +1,26 @@ require_relative '../data_conversion' module Ccrypto module Java + + class SecretSharingException < Ccrypto::SecretSharingException; end + class SecretSharingEngine include DataConversion + 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 + 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 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 @@ -34,14 +42,21 @@ 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