Class: Ccrypto::Ruby::DigestEngine

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

Constant Summary collapse

SupportedDigest =
[
  Ccrypto::SHA1.provider_info("sha1"),
  Ccrypto::SHA224.provider_info("sha224"),
  Ccrypto::SHA256.provider_info("sha256"),
  Ccrypto::SHA384.provider_info("sha384"),
  Ccrypto::SHA512.provider_info("sha512"),
  Ccrypto::SHA512_224.provider_info("sha512-224"),
  Ccrypto::SHA512_256.provider_info("sha512-256"),
  Ccrypto::SHA3_224.provider_info("sha3-224"),
  Ccrypto::SHA3_256.provider_info("sha3-256"),
  Ccrypto::SHA3_384.provider_info("sha3-384"),
  Ccrypto::SHA3_512.provider_info("sha3-512"),
  Ccrypto::SHAKE128.provider_info("shake128"),
  Ccrypto::SHAKE256.provider_info("shake256"),
  Ccrypto::BLAKE2b512.provider_info("BLAKE2b512"),
  Ccrypto::BLAKE2s256.provider_info("BLAKE2s256"),
  Ccrypto::SM3.provider_info("SM3")
  # deprecated starting OpenSSL v3.0
  #Ccrypto::RIPEMD160.provider_info("RIPEMD160"),
  #Ccrypto::WHIRLPOOL.provider_info("whirlpool")
]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversion

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

Constructor Details

#initialize(inst) ⇒ DigestEngine

Returns a new instance of DigestEngine.



106
107
108
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 106

def initialize(inst)
  @inst = inst
end

Class Method Details

.digest(key) ⇒ Object



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
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 69

def self.digest(key)

  case key
  when Ccrypto::DigestConfig
    DigestEngine.new(OpenSSL::Digest.new(key.provider_config))
  when String, Symbol
    if key.is_a?(Symbol)
      res = engineKeys[key]
    else
      k = key.gsub("-","_")
      res = engineKeys[k.to_sym]
    end

    if is_empty?(res)
      teLogger.debug "No digest available for #{key}"
      raise DigestEngineException, "Not supported digest engine #{key}"
    else
      teLogger.debug "Found digest #{key.to_sym}"
      DigestEngine.new(OpenSSL::Digest.new(res.provider_config))
    end

  else
    raise DigestEngineException, "Not supported digest engine #{key}"
  end
  
end

.engineKeysObject



96
97
98
99
100
101
102
103
104
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 96

def self.engineKeys
  if @engineKeys.nil?
    @engineKeys = {}
    supported.map do |e|
      @engineKeys[e.algo.to_sym] = e
    end
  end
  @engineKeys
end

.instance(*args, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 59

def self.instance(*args, &block)
  conf = args.first
  if not_empty?(conf.provider_config)
    teLogger.debug "Creating digest engine #{conf.provider_config}"
    DigestEngine.new(OpenSSL::Digest.new(conf.provider_config))
  else
    raise DigestEngineException, "Given digest config #{conf.algo} does not have provider key mapping. Most likely this config is not supported by provider #{Ccrypto::Ruby::Provider.provider_name}"
  end
end

.is_supported?(eng) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 41

def self.is_supported?(eng)

  case eng
  when Ccrypto::DigestConfig
    SupportedDigest.include?(eng) 
  when String, Symbol
    if eng.is_a?(Symbol)
      engineKeys.include?(eng)
    else
      e = eng.gsub("-","_")
      engineKeys.include?(e.to_sym)
    end
  else
    raise DigestEngineException, "Unknown engine '#{eng}'"
  end

end

.supportedObject



37
38
39
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 37

def self.supported
  SupportedDigest
end

Instance Method Details

#digest(val, output = :binary) ⇒ Object



115
116
117
118
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 115

def digest(val, output = :binary)
  digest_update(val)
  digest_final(output)
end

#digest_final(output = :binary) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 129

def digest_final(output = :binary)
  
  res = @inst.digest
  @inst.reset
  case output
  when :hex
    to_hex(res)
  when :b64
    to_b64(res)
  else
    res
  end
end

#digest_update(val) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 120

def digest_update(val)
  case val
  when MemoryBuffer
    @inst.update(val.bytes)
  else
    @inst.update(val)
  end
end

#native_digest_engineObject Also known as: native_instance



110
111
112
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 110

def native_digest_engine
  @inst
end

#resetObject



143
144
145
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 143

def reset
  @inst.reset
end