Sha256: 7550df42d06dbb4bb68f0c24d900f5799a93e096612fa7315142516efe83a301

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'openssl'

module R509
  #MessageDigest allows you to specify MDs in a more friendly fashion
  class MessageDigest
    # a list of message digests that this class understands
    KNOWN_MDS = ['SHA1','SHA224','SHA256','SHA384','SHA512','DSS1','MD5']

    # this constant defines the default message digest if it is not supplied
    # or an invalid digest is passed
    DEFAULT_MD = 'SHA1'

    attr_reader :name, :digest

    # @param [String,OpenSSL::Digest] arg
    def initialize(arg=nil)
      if arg.kind_of?(String)
        @name = arg.downcase
        @digest = translate_name_to_digest
      elsif arg.nil?
        @name = DEFAULT_MD
        @digest = translate_name_to_digest
      else
        @digest = arg
        @name = translate_digest_to_name
      end
    end

    private

    # @return [OpenSSL::Digest]
    def translate_name_to_digest
      case @name
      when 'sha1' then OpenSSL::Digest::SHA1.new
      when 'sha224' then OpenSSL::Digest::SHA224.new
      when 'sha256' then OpenSSL::Digest::SHA256.new
      when 'sha384' then OpenSSL::Digest::SHA384.new
      when 'sha512' then OpenSSL::Digest::SHA512.new
      when 'md5' then OpenSSL::Digest::MD5.new
      when 'dss1' then OpenSSL::Digest::DSS1.new
      else
        @name = DEFAULT_MD.downcase
        translate_name_to_digest
      end
    end

    # @return [String]
    def translate_digest_to_name
      case @digest
      when OpenSSL::Digest::SHA1 then 'sha1'
      when OpenSSL::Digest::SHA224 then 'sha224'
      when OpenSSL::Digest::SHA256 then 'sha256'
      when OpenSSL::Digest::SHA384 then 'sha384'
      when OpenSSL::Digest::SHA512 then 'sha512'
      when OpenSSL::Digest::MD5 then 'md5'
      when OpenSSL::Digest::DSS1 then 'dss1'
      else
        raise ArgumentError, "Unknown digest"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
r509-0.10.0 lib/r509/message_digest.rb