Sha256: f0f733602f81c0fbcfa16093dfc725fb298e762817d04c4ef40b2cffdda0a41c

Contents?: true

Size: 1.74 KB

Versions: 23

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

# Authenticator for the "+CRAM-MD5+" SASL mechanism, specified in
# RFC2195[https://tools.ietf.org/html/rfc2195].  See Net::IMAP#authenticate.
#
# == Deprecated
#
# +CRAM-MD5+ is obsolete and insecure.  It is included for compatibility with
# existing servers.
# {draft-ietf-sasl-crammd5-to-historic}[https://tools.ietf.org/html/draft-ietf-sasl-crammd5-to-historic-00.html]
# recommends using +SCRAM-*+ or +PLAIN+ protected by TLS instead.
#
# Additionally, RFC8314[https://tools.ietf.org/html/rfc8314] discourage the use
# of cleartext and recommends TLS version 1.2 or greater be used for all
# traffic.  With TLS +CRAM-MD5+ is okay, but so is +PLAIN+
class Net::IMAP::SASL::CramMD5Authenticator
  def initialize(user = nil, pass = nil,
                 authcid: nil, username: nil,
                 password: nil, secret: nil,
                 warn_deprecation: true,
                 **)
    if warn_deprecation
      warn "WARNING: CRAM-MD5 mechanism is deprecated." # TODO: recommend SCRAM
    end
    require "digest/md5"
    @user = authcid || username || user
    @password = password || secret || pass
    @done = false
  end

  def initial_response?; false end

  def process(challenge)
    digest = hmac_md5(challenge, @password)
    return @user + " " + digest
  ensure
    @done = true
  end

  def done?; @done end

  private

  def hmac_md5(text, key)
    if key.length > 64
      key = Digest::MD5.digest(key)
    end

    k_ipad = key + "\0" * (64 - key.length)
    k_opad = key + "\0" * (64 - key.length)
    for i in 0..63
      k_ipad[i] = (k_ipad[i].ord ^ 0x36).chr
      k_opad[i] = (k_opad[i].ord ^ 0x5c).chr
    end

    digest = Digest::MD5.digest(k_ipad + text)

    return Digest::MD5.hexdigest(k_opad + digest)
  end

end

Version data entries

23 entries across 23 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/net-imap-0.4.14/lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.18 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.17 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.16 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.15 lib/net/imap/sasl/cram_md5_authenticator.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/net-imap-0.4.14/lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.14 lib/net/imap/sasl/cram_md5_authenticator.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/net-imap-0.4.11/lib/net/imap/sasl/cram_md5_authenticator.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/net-imap-0.4.11/lib/net/imap/sasl/cram_md5_authenticator.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/net-imap-0.4.11/lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.13 lib/net/imap/sasl/cram_md5_authenticator.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/net-imap-0.4.12/lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.12 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.10 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.9.1 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.9 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.8 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.7 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.6 lib/net/imap/sasl/cram_md5_authenticator.rb
net-imap-0.4.5 lib/net/imap/sasl/cram_md5_authenticator.rb