diff options
author | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2019-10-31 16:23:48 +1300 |
---|---|---|
committer | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2019-11-01 09:42:10 +1300 |
commit | 27f1eab0c178bb74e2a961283fa9c6e49fa8d500 (patch) | |
tree | 34813621ec3be0dee3ebcefd38cca1a6a4404951 /lib | |
parent | 72bd9fe74e62db205865d19a9ddea2e1b89fcf37 (diff) | |
download | ruby-openssl-27f1eab0c178bb74e2a961283fa9c6e49fa8d500.tar.gz |
Add support for SHA3. Resolves #272.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/openssl/digest.rb | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/lib/openssl/digest.rb b/lib/openssl/digest.rb index b6744de6..d283c98b 100644 --- a/lib/openssl/digest.rb +++ b/lib/openssl/digest.rb @@ -15,11 +15,18 @@ module OpenSSL class Digest - alg = %w(MD2 MD4 MD5 MDC2 RIPEMD160 SHA1 SHA224 SHA256 SHA384 SHA512) + ALGORITHMS = %w(MD4 MD5 MDC2 RIPEMD160 SHA1 SHA224 SHA256 SHA384 SHA512) + if OPENSSL_VERSION_NUMBER < 0x10100000 - alg += %w(DSS DSS1 SHA) + ALGORITHMS.concat %w(DSS DSS1 SHA) + end + + if !OPENSSL_VERSION.include?("LibreSSL") && OPENSSL_VERSION_NUMBER > 0x10101000 + ALGORITHMS.concat %w(SHA3-224 SHA3-256 SHA3-384 SHA3-512) end + ALGORITHMS.freeze + # Return the hash value computed with _name_ Digest. _name_ is either the # long name or short name of a supported digest algorithm. # @@ -35,17 +42,20 @@ module OpenSSL super(data, name) end - alg.each{|name| + ALGORITHMS.each do |name| klass = Class.new(self) { define_method(:initialize, ->(data = nil) {super(name, data)}) } + singleton = (class << klass; self; end) + singleton.class_eval{ - define_method(:digest){|data| new.digest(data) } - define_method(:hexdigest){|data| new.hexdigest(data) } + define_method(:digest) {|data| new.digest(data)} + define_method(:hexdigest) {|data| new.hexdigest(data)} } - const_set(name, klass) - } + + const_set(name.tr('-', '_'), klass) + end # Deprecated. # |