Module: CcipherFactory::KDF::Scrypt

Includes:
Common, TR::CondUtils
Defined in:
lib/ccipher_factory/kdf/scrypt.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.



17
18
19
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 17

def attachedDigest
  @attachedDigest
end

#attachedValueObject

Returns the value of attribute attachedValue.



17
18
19
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 17

def attachedValue
  @attachedValue
end

#blocksizeObject

Mixin methods



15
16
17
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 15

def blocksize
  @blocksize
end

#costObject

Mixin methods



15
16
17
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 15

def cost
  @cost
end

#derivedValObject (readonly)

Returns the value of attribute derivedVal.



18
19
20
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 18

def derivedVal
  @derivedVal
end

#digestObject

Returns the value of attribute digest.



16
17
18
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 16

def digest
  @digest
end

#digestAlgoObject

Returns the value of attribute digestAlgo.



16
17
18
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 16

def digestAlgo
  @digestAlgo
end

#outByteLengthObject

Mixin methods



15
16
17
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 15

def outByteLength
  @outByteLength
end

#parallelObject

Mixin methods



15
16
17
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 15

def parallel
  @parallel
end

#saltObject

Mixin methods



15
16
17
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 15

def salt
  @salt
end

Instance Method Details

#derive_final(&block) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 67

def derive_final(&block)

  raise KDFError, "outByteLength is required" if is_empty?(@outByteLength)

  digMeta = @digest.digest_final 

  sconf = Ccrypto::ScryptConfig.new
  sconf.outBitLength = @outByteLength*8
  sconf.salt = @salt
  sconf.cost = @cost
  sconf.parallel = @parallel
  sconf.blockSize = @blocksize

  scrypt = Ccrypto::AlgoFactory.engine(sconf)

  @derivedVal = scrypt.derive(intOutputBuf.bytes)

  if is_output_given?
    write_to_output(@derivedVal)
  end

  ts = BinStruct.instance.struct(:kdf_scrypt) 
  ts.digest = digMeta
  ts.salt = @salt
  ts.cost = @cost
  ts.blocksize = @blocksize
  ts.parallel = @parallel
  ts.outByteLength = @outByteLength
  if is_bool?(@attachedDigest) and @attachedDigest
    ts.value = @derivedVal
  else
    ts.value = ""
  end
  ts.encoded

end

#derive_init(*args, &block) ⇒ 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 19

def derive_init(*args, &block)

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

  @cost = 65536 if is_empty?(@cost)
  @parallel = 1 if is_empty?(@parallel)
  @blocksize = 8 if is_empty?(@blocksize)
  @salt = SecureRandom.random_bytes(16) if is_empty?(@salt)

  logger.debug "Cost : #{@cost}"
  logger.debug "Parallel : #{@parallel}"
  logger.debug "Blocksize : #{@blocksize}"
  logger.debug "Salt : #{@salt.inspect}"
  logger.debug "Digest Algo : #{@digestAlgo}"
  logger.debug "Digest : #{@digest}"

  if @digest.nil?
    logger.debug "Initializing digest with algo #{@digestAlgo}"
    @digestAlgo = Digest::SupportedDigest.instance.default_digest if is_empty?(@digestAlgo)
    @digest = Digest.instance
    @digest.digest_init(@digestAlgo)
  else
    logger.debug "Setting digest algo value from digest #{@digest}"
    @digestAlgo = @digest.algo
  end

  @digest.output(intOutputBuf)

  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



63
64
65
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 63

def derive_update(val)
  @digest.digest_update(val) 
end

#is_attached_mode?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
# File 'lib/ccipher_factory/kdf/scrypt.rb', line 104

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