Module: CcipherFactory::Digest

Includes:
Common, TR::CondUtils
Defined in:
lib/ccipher_factory/digest/digest.rb,
lib/ccipher_factory/digest/supported_digest.rb

Defined Under Namespace

Classes: DigestEngine, DigestError, SupportedDigest

Instance Attribute Summary collapse

Class Method 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

#algoObject

Mixin methods



73
74
75
# File 'lib/ccipher_factory/digest/digest.rb', line 73

def algo
  @algo
end

#digestValObject

Mixin methods



73
74
75
# File 'lib/ccipher_factory/digest/digest.rb', line 73

def digestVal
  @digestVal
end

#saltObject

Mixin methods



73
74
75
# File 'lib/ccipher_factory/digest/digest.rb', line 73

def salt
  @salt
end

Class Method Details

.from_encoded(bin, &block) ⇒ Object



20
21
22
23
# File 'lib/ccipher_factory/digest/digest.rb', line 20

def self.from_encoded(bin, &block)
  ts = BinStruct.instance.struct_from_bin(bin)
  from_tspec(ts, &block)
end

.from_tspec(ts, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ccipher_factory/digest/digest.rb', line 25

def self.from_tspec(ts, &block)

  if ts.oid == BTag.constant_value(:digest_attached)
    dig = from_encoded(ts.digest_config)
    dig.digestVal = ts.digest_value
    dig
  else

    algo = BTag.value_constant(ts.digest_algo)
    logger.debug "from_encoded algo : #{algo}"
    dig = instance
    dig.salt = ts.salt
    dig.digest_init(algo, dig.salt, &block)
  end
end

.instanceObject

(eng = SupportedDigest.instance.default_digest, *args)



13
14
15
16
17
18
# File 'lib/ccipher_factory/digest/digest.rb', line 13

def self.instance #(eng = SupportedDigest.instance.default_digest, *args)
  #raise DigestEror, "Digest '#{eng}' is not supported" if not SupportedDigest.instance.is_supported?(eng)
  dig = DigestEngine.new
  dig.extend(Digest)
  dig
end

.loggerObject



62
63
64
65
66
67
68
# File 'lib/ccipher_factory/digest/digest.rb', line 62

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

.parse(bin, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ccipher_factory/digest/digest.rb', line 41

def self.parse(bin, &block)
 
  res = {  }
  ts = BinStruct.instance.struct_from_bin(bin)
  res[:type] = BTag.value_constant(ts.oid)

  if res[:type] == :digest_attached
    #conf = Encoding::ASN1Decoder.from_encoded(ts.value(:digest_config))
    conf = BinStruct.instance.struct_from_bin(ts.digest_config)
    res[:algo] = BTag.value_constant(conf.digest_algo)
    res[:salt] = conf.salt
    #res[:algo] = Tag.constant_key(conf.value(:digest_algo)) 
    #res[:salt] = conf.value(:salt)
  end

  res[:digest] = ts.digest_value

  res

end

.to_digest_string(sym) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/ccipher_factory/digest/digest.rb', line 154

def self.to_digest_string(sym)
  if not_empty?(sym)
    sym.to_s.gsub("_","-")
  else
    sym
  end
end

Instance Method Details

#digest_finalObject

Raises:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ccipher_factory/digest/digest.rb', line 122

def digest_final

  raise DigestError, "Please call digest_init first before call final() (#{@digest.inspect})" if @digest.nil?

  @digestVal = @digest.digest_final
  @digest = nil

  write_to_output(@digestVal)

  #ts = Encoding::ASN1Encoder.instance(:digest)
  ts = BinStruct.instance.struct(:digest)
  ts.digest_algo = BTag.constant_value(@algo)
  if not_empty?(@salt)
    ts.salt = @salt
  else
    ts.salt = ""
  end

  if is_attach_mode?
    tsd = BinStruct.instance.struct(:digest_attached)
    #tsd = Encoding::ASN1Encoder.instance(:digest_attached)
    tsd.digest_config = ts.encoded
    tsd.digest_value = @digestVal
    res = tsd.encoded
  else
    res = ts.encoded
  end

  res

end

#digest_init(*args, &block) ⇒ Object

Raises:



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
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ccipher_factory/digest/digest.rb', line 74

def digest_init(*args, &block)

  logger.debug "args : #{args}"

  @algo = args.first
  @algo = SupportedDigest.instance.default_digest if is_empty?(@algo)
  raise DigestError, "Given digest '#{@algo}' is not supported.\nPossible digest algo including: #{SupportedDigest.instance.supported.join(", ")}" if not SupportedDigest.instance.is_supported?(@algo)

  logger.debug "Digest algo in init : #{@algo}"

  @digest = Ccrypto::AlgoFactory.engine(Ccrypto::DigestConfig).digest(@algo)

  #@digest = OpenSSL::Digest.new(Digest.to_digest_string(@algo))

  salt = args[1]
  if not_empty?(salt)
    logger.debug "Salt given #{salt} / #{salt.length}"

    case salt
    when :random_salt, :random
      saltLen = args[2] || 16
      sre = Ccrypto::AlgoFactory.engine(Ccrypto::SecureRandomConfig)
      @salt = sre.random_bytes(saltLen)
      @digest.digest_update(@salt)
    else
      if salt.is_a?(String)
        @salt = salt
        @digest.digest_update(@salt)
      else
        raise DigestError, "Unknown option '#{salt}' for salt"
      end
    end
  end

  if block
    instance_eval(&block)
    digest_final
  else
    self
  end

end

#digest_update(val) ⇒ Object

Raises:



117
118
119
120
# File 'lib/ccipher_factory/digest/digest.rb', line 117

def digest_update(val)
  raise DigestError, "Please call digest_init first before call update() (#{@digest.inspect})" if @digest.nil?
  @digest.digest_update(val) 
end