lib/ccrypto/ruby/engines/digest_engine.rb in ccrypto-ruby-0.1.0 vs lib/ccrypto/ruby/engines/digest_engine.rb in ccrypto-ruby-0.1.1

- old
+ new

@@ -25,29 +25,37 @@ 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") + Ccrypto::SM3.provider_info("SM3") + # deprecated starting OpenSSL v3.0 + #Ccrypto::RIPEMD160.provider_info("RIPEMD160"), + #Ccrypto::WHIRLPOOL.provider_info("whirlpool") ] def self.supported SupportedDigest end def self.is_supported?(eng) - res = supported.include?(eng) - begin - res = digest(eng) if not res - rescue DigestEngineException => ex - res = false + + 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 - res end def self.instance(*args, &block) conf = args.first if not_empty?(conf.provider_config) @@ -57,19 +65,33 @@ 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 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}" + + 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 - teLogger.debug "Found digest #{key.to_sym}" - DigestEngine.new(OpenSSL::Digest.new(res.provider_config)) + raise DigestEngineException, "Not supported digest engine #{key}" end - + end def self.engineKeys if @engineKeys.nil? @engineKeys = {}