Class: Ccrypto::Ruby::HKDFEngine

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

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_hex, included, #to_b64, #to_hex, #to_int_array

Constructor Details

#initialize(*args, &block) ⇒ HKDFEngine

Returns a new instance of HKDFEngine.

Raises:

  • (KDFEngineException)


10
11
12
13
14
15
16
17
18
# File 'lib/ccrypto/ruby/engines/hkdf_engine.rb', line 10

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

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


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

Instance Method Details

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ccrypto/ruby/engines/hkdf_engine.rb', line 20

def derive(input, output = :binary)

  digest = init_digest(@config.digest)

  @config.info = "" if @config.info.nil?

  res = OpenSSL::KDF.hkdf(input, salt: @config.salt, info: @config.info, length: @config.outBitLength/8, hash: digest)

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