Class: Ccrypto::Java::PBKDF2Engine

Inherits:
Object
  • Object
show all
Includes:
DataConversion, TR::CondUtils
Defined in:
lib/ccrypto/java/engines/pbkdf2_engine.rb

Defined Under Namespace

Classes: PBKDF2EngineException, SupportedPBKDF2HMAC

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversion

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

Constructor Details

#initialize(*args, &block) ⇒ PBKDF2Engine

Returns a new instance of PBKDF2Engine.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ccrypto/java/engines/pbkdf2_engine.rb', line 55

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

  raise PBKDF2EngineException, "KDF config is expected. Given #{@config}" if not @config.is_a?(Ccrypto::PBKDF2Config)
  raise PBKDF2EngineException, "Output bit length (outBitLength) value is not given or not a positive value (#{@config.outBitLength})" if is_empty?(@config.outBitLength) or @config.outBitLength <= 0

  if is_empty?(@config.digest)
    @config.digest = default_digest
  else

    case @config.digest
    when String, Symbol
      dig = self.class.find_supported_hmac_by_digest(@config.digest)
      raise PBKDF2EngineException, "Cannot find digest '#{@config.digest}'" if is_empty?(dig)
      logger.warn "More than 1 result for supported hmac by digest found. Found #{dig.length}" if dig.length > 1
      @config.digest = dig.first

    when Ccrypto::HMACConfig

    else
      raise PBKDF2EngineException, "HMACConfig is expected instead got '#{@config.digest.class}'"

    end

    raise PBKDF2EngineException, "HMACConfig is required to be provider initialized HMACConfig. Please get the HMACConfig via the supported_hmac from PBKDF2" if is_empty?(@config.digest.provider_config[:hmac_algo]) 
  end

  #@config.salt = SecureRandom.random_bytes(16) if is_empty?(@config.salt)

end

Class Method Details

.find_supported_hmac_by_digest(algo) ⇒ Object



45
46
47
# File 'lib/ccrypto/java/engines/pbkdf2_engine.rb', line 45

def self.find_supported_hmac_by_digest(algo)
  supported_pbkdf2_digests.find( algo: algo )
end

.supported_pbkdf2_digestsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ccrypto/java/engines/pbkdf2_engine.rb', line 17

def self.supported_pbkdf2_digests

  if @supportedHmac.nil?
    @supportedHmac = SupportedPBKDF2HMAC.load("cj_pbkdf2")
    logger.debug "supported hmac : #{@supportedHmac.empty?}"
    if @supportedHmac.empty?
      HMACEngine.supported_hmac_configs.each do |hm|
        begin
          algo = "PBKDF2With#{hm.provider_config[:hmac_algo]}"
          prov = hm.provider_config[:jce_provider]
          javax.crypto.SecretKeyFactory.getInstance(algo, prov)

          logger.debug "PBKDF2 algo #{algo} is good"
          hm.provider_config[:hmac_algo] = algo
          logger.debug "Registering PBKDF2 config #{hm.inspect}"
          @supportedHmac.register(hm, { tag_under: :algo, tag_value: hm.digest_config.algo })
          #@supportedHmac[algo] = hm
        rescue Exception => ex
          logger.debug "HMAC algo #{algo} failed with PBKDF2 with error #{ex}"
        end
      end
      @supportedHmac.save("cj_pbkdf2")
    end
  end
  @supportedHmac

end

Instance Method Details

#default_digestObject



123
124
125
# File 'lib/ccrypto/java/engines/pbkdf2_engine.rb', line 123

def default_digest
  self.class.find_supported_hmac_by_digest("sha256").first
end

#derive(input, output = :binary) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ccrypto/java/engines/pbkdf2_engine.rb', line 86

def derive(input, output = :binary)

  cinput = java.lang.String.new(to_java_bytes(input))

  #raise KDFEngineException, "Given input is not a String" if not input.is_a?(String)

  begin

    algo = @config.digest.provider_config[:hmac_algo]
    prov = @config.digest.provider_config[:jce_provider]
    if not_empty?(prov)
      skf = javax.crypto.SecretKeyFactory.getInstance(algo, prov)
    else
      skf = javax.crypto.SecretKeyFactory.getInstance(algo)
    end

    # Java API 1st parameter is char[]
    keySpec = javax.crypto.spec.PBEKeySpec.new(cinput.to_java.toCharArray, to_java_bytes(@config.salt), @config.iter, @config.outBitLength)

    sk = skf.generateSecret(keySpec)
    out = sk.encoded

    case output
    when :b64
      to_b64(out)
    when :hex
      to_hex(out)
    else
      out
    end

  rescue Exception => ex
    raise KDFEngineException, ex
  end
  
end