Class: Ccrypto::Java::BCryptEngine

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

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) ⇒ BCryptEngine

Returns a new instance of BCryptEngine.

Raises:

  • (KDFEngineException)


11
12
13
14
15
16
17
# File 'lib/ccrypto/java/engines/bcrypt_engine.rb', line 11

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

  raise KDFEngineException, "Ccrypto::BCryptConfig is expected. Given #{@config}" if not @config.is_a?(Ccrypto::BCryptConfig)

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

Instance Method Details

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



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
44
# File 'lib/ccrypto/java/engines/bcrypt_engine.rb', line 19

def derive(input, output = :binary)
  
  begin

    binput = to_java_bytes(input)

    #logger.debug "bcrypt input : #{binput.inspect}"
    logger.debug "bcrypt salt : #{to_hex(@config.salt)}"
    logger.debug "bcrypt cost : #{@config.cost}"

    res = org.bouncycastle.crypto.generators.BCrypt.generate(binput, to_java_bytes(@config.salt), @config.cost)

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

  rescue Exception => ex
    raise KDFEngineException, ex
  end
  
end