Class: Ccrypto::Java::Argon2Engine

Inherits:
Object
  • Object
show all
Includes:
DataConversion, TR::CondUtils
Defined in:
lib/ccrypto/java/engines/argon2_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(conf, &block) ⇒ Argon2Engine

Returns a new instance of Argon2Engine.

Raises:

  • (KDFEngineException)


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

def initialize(conf, &block)
  
  raise KDFEngineException, "Argon2 config is expected" if not conf.is_a?(Ccrypto::Argon2Config)
  raise KDFEngineException, "Output bit length (outBitLength) value is not given or not a positive value (Given #{conf.outBitLength})" if is_empty?(conf.outBitLength) or conf.outBitLength <= 0

  #logger.warn "Memory cost is less then 1GB (recommended value)" if conf.cost < 1024*1024*1024

  @config = conf
end

Instance Method Details

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ccrypto/java/engines/argon2_engine.rb', line 21

def derive(input, outFormat = :binary)
 
  gen = org.bouncycastle.crypto.generators.Argon2BytesGenerator.new
  builder = org.bouncycastle.crypto.params.Argon2Parameters::Builder.new

  logger.debug "outBitLength : #{@config.outBitLength} / #{@config.outBitLength/8} bytes"
  outBuf = ::Java::byte[@config.outBitLength/8].new

  logger.debug "Iteration : #{@config.iter}"
  builder.withIterations(@config.iter)

  logger.debug "Cost : #{2**@config.cost}"
  #builder.withMemoryAsKB(@config.cost)  # unit here is Kilobyte. Config standardize to byte length
  builder.withMemoryPowOfTwo(@config.cost)  

  logger.debug "Parallel : #{@config.parallel}"
  builder.withParallelism(@config.parallel)

  logger.debug "Salt : #{to_hex(@config.salt)}"
  builder.withSalt(to_java_bytes(@config.salt))

  #logger.debug "Secret : #{to_b64(to_java_bytes(@config.secret))}"
  if not (@config.secret.nil? or @config.secret.empty?)
    logger.debug "Secret given #{to_hex(@config.secret)}" 
    builder.withSecret(to_java_bytes(@config.secret)) 
  else
    logger.debug "Empty secret value" 
  end

  case @config.variant
  when :argon2d
    logger.debug "Variant : argon2d"
    builder.withVersion(0)
  when :argon2i
    logger.debug "Variant : argon2i"
    builder.withVersion(1)
  when :argon2id
    logger.debug "Variant : argon2id"
    builder.withVersion(2)
  when :argon2_version_10
    logger.debug "Variant : argon2_version_10"
    # 0x10
    builder.withVersion(16)
  when :argon2_version_13
    logger.debug "Variant : argon2_version_13"
    # 0x13
    builder.withVersion(19)
  else
    raise KDFEngineException, "Unknown variant '#{@config.variant}'"
  end

  gen.init(builder.build())

  gen.generateBytes(to_java_bytes(input), outBuf)

  case outFormat
  when :hex
    to_hex(outBuf)
  when :b64
    to_b64(outBuf)
  else
    outBuf
  end

end