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"),
  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.


84
85
86
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 84

def initialize(inst)
  @inst = inst
end

Class Method Details

.digest(key) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 61

def self.digest(key)
  
  res = engineKeys[key]
  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

end

.engineKeysObject


74
75
76
77
78
79
80
81
82
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 74

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


51
52
53
54
55
56
57
58
59
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 51

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)

40
41
42
43
44
45
46
47
48
49
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 40

def self.is_supported?(eng)
  res = supported.include?(eng)
  begin
    res = digest(eng) if not res
  rescue DigestEngineException => ex
    res = false
  end

  res
end

.supportedObject


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

def self.supported
  SupportedDigest
end

Instance Method Details

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


93
94
95
96
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 93

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

#digest_final(output = :binary) ⇒ Object


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 107

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


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

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


88
89
90
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 88

def native_digest_engine
  @inst
end

#resetObject


121
122
123
# File 'lib/ccrypto/ruby/engines/digest_engine.rb', line 121

def reset
  @inst.reset
end