Class: CcipherFactory::KCV

Inherits:
Object
  • Object
show all
Includes:
SymKeyCipher::SymKeyEncrypt, TR::CondUtils
Defined in:
lib/ccipher_factory/kcv/kcv.rb

Defined Under Namespace

Classes: KCVError

Instance Attribute Summary collapse

Attributes included from SymKeyCipher::SymKeyEncrypt

#iv, #key, #mode

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SymKeyCipher::SymKeyEncrypt

#encrypt_final, #encrypt_init, #encrypt_update

Methods included from Compression::CompressionHelper

#compress_data_if_active, #compression_off, #compression_on, #compressor, #decompress_data_if_active, #decompressor, #decompressor_from_encoded, #encode_null_compressor, #is_compression_on?

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

#check_valueObject

Returns the value of attribute check_value.



12
13
14
# File 'lib/ccipher_factory/kcv/kcv.rb', line 12

def check_value
  @check_value
end

#nonceObject

Returns the value of attribute nonce.



12
13
14
# File 'lib/ccipher_factory/kcv/kcv.rb', line 12

def nonce
  @nonce
end

Class Method Details

.converterObject



27
28
29
30
31
32
# File 'lib/ccipher_factory/kcv/kcv.rb', line 27

def self.converter
  if @conv.nil?
    @conv = Ccrypto::UtilFactory.instance(:data_converter)
  end
  @conv
end

.from_encoded(bin) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ccipher_factory/kcv/kcv.rb', line 14

def self.from_encoded(bin) 

  ts = BinStruct.instance.struct_from_bin(bin)
  kcv = KCV.new
  kcv.mode = BTag.constant_value(ts.mode)
  kcv.iv = ts.iv
  kcv.nonce = ts.nonce
  kcv.check_value = ts.check_value

  kcv

end

.loggerObject



77
78
79
80
81
82
83
# File 'lib/ccipher_factory/kcv/kcv.rb', line 77

def self.logger
  if @logger.nil?
    @logger = Tlogger.new
    @logger.tag = :kcv
  end
  @logger
end

Instance Method Details

#encoded(&block) ⇒ Object

Raises:



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
# File 'lib/ccipher_factory/kcv/kcv.rb', line 42

def encoded(&block)

  raise KCVError, "Key must be given" if is_empty?(@key)

  logger.debug "Generating KCV"
  compression_off
  output(intOutputBuf)

  if block
    logger.debug "Block given"
    @iv = block.call(:kcv_cipher_iv)
  end

  encrypt_init(@key)

  if is_empty?(@nonce)
    logger.debug "Random nounce"
    @nonce = SecureRandom.random_bytes(@key.keysize)
  else
    logger.debug "Nounce is given"
  end

  encrypt_update(@nonce)
  encrypt_final

  ts = BinStruct.instance.struct(:kcv)
  ts.mode = BTag.constant_value(@mode)
  ts.iv = @iv
  ts.nonce = @nonce
  ts.check_value = intOutputBuf.bytes
  
  ts.encoded

end

#is_matched?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/ccipher_factory/kcv/kcv.rb', line 34

def is_matched?
  logger.tdebug :kcv_match, "Check if KCV matched"
  encoded
  res = intOutputBuf.bytes
  comp = Ccrypto::UtilFactory.instance(:comparator)
  comp.is_equal?(@check_value, res)
end

#loggerObject



84
85
86
# File 'lib/ccipher_factory/kcv/kcv.rb', line 84

def logger
  self.class.logger
end