Module: CcipherFactory::KDF::PBKDF2

Includes:
Common, TR::CondUtils
Defined in:
lib/ccipher_factory/kdf/pbkdf2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#attach_mode, #cleanup_intOutputBuf, #cleanup_intOutputFile, #detach_mode, #disposeOutput, #intOutputBuf, #intOutputFile, #is_attach_mode?, #is_output_given?, #output, #output_obj, #sanitize_symbol, #write_to_output

Instance Attribute Details

#attachedDigestObject

Returns the value of attribute attachedDigest.



10
11
12
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 10

def attachedDigest
  @attachedDigest
end

#attachedValueObject

Returns the value of attribute attachedValue.



10
11
12
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 10

def attachedValue
  @attachedValue
end

#derivedValObject (readonly)

Returns the value of attribute derivedVal.



11
12
13
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 11

def derivedVal
  @derivedVal
end

#digestAlgoObject

Returns the value of attribute digestAlgo.



9
10
11
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 9

def digestAlgo
  @digestAlgo
end

#iterObject

Returns the value of attribute iter.



9
10
11
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 9

def iter
  @iter
end

#outByteLengthObject

Returns the value of attribute outByteLength.



9
10
11
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 9

def outByteLength
  @outByteLength
end

#saltObject

Returns the value of attribute salt.



9
10
11
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 9

def salt
  @salt
end

Instance Method Details

#derive_finalObject

Raises:



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
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 39

def derive_final
  
  raise KDFError, "outByteLength is required" if is_empty?(@outByteLength)
  
  #if is_empty?(@digestAlgo)
  #  digestVal = CcipherFactory::Digest::SupportedDigest.instance.default_digest
  #  digestId = digestVal

  #  logger.tdebug :pbkdf2_derive_final, "digest algo is nil. Using default digest #{digestVal}"

  #else
  #  raise KDFError, "Given digest algo '#{@digestAlgo}' is not supported" if not Digest::SupportedDigest.instance.is_supported?(@digestAlgo)

  #  logger.tdebug :pbkdf2_derive_final, "Using user given digest algo #{@digestAlgo}"

  #  digestId = @digestAlgo
  #end

  hconf = Ccrypto::PBKDF2Config.new
  #hconf.digest = digestId
  hconf.digest = @digestAlgo
  hconf.salt = @salt if not_empty?(@salt)
  hconf.iter = @iter if not_empty?(@iter)
  hconf.outBitLength = @outByteLength*8

  hkdf = Ccrypto::AlgoFactory.engine(hconf)

  @derivedVal = hkdf.derive(intOutputBuf.bytes)

  write_to_output(@derivedVal) if is_output_given?

  ts = BinStruct.instance.struct(:kdf_pbkdf2)
  #ts.digest = BTag.constant_value(digestId)
  ts.digest = BTag.constant_value(hconf.digest)
  ts.salt = @salt
  ts.outByteLength = @outByteLength
  ts.iterations = hconf.iter
  if is_bool?(@attachedDigest) and @attachedDigest
    ts.value = @derivedVal
  else
    ts.value = ""
  end
  ts.encoded

end

#derive_init(*args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 13

def derive_init(*args, &block)

  len = args.first
  @outByteLength = len/8 if not_empty?(len)

  @salt = SecureRandom.random_bytes(@outByteLength) if is_empty?(@salt)

  if is_empty?(@attachedValue)
    @attachedDigest = false if is_empty?(@attachedDigest)
  else
    @attachedDigest = true
  end

  if block
    instance_eval(&block)
    derive_final
  else
    self
  end
  
end

#derive_update(val) ⇒ Object



35
36
37
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 35

def derive_update(val)
  intOutputBuf.write(val)
end

#is_attached_mode?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 85

def is_attached_mode?
  if is_empty?(@attachedValue) 
    @attachedDigest
  else
    true
  end
end

#loggerObject



93
94
95
96
97
98
99
# File 'lib/ccipher_factory/kdf/pbkdf2.rb', line 93

def logger
  if @logger.nil?
    @logger = TeLogger::Tlogger.new
    @logger.tag = :pbkdf2
  end
  @logger
end