Module: MailAuth::Dkim::Algorithm

Defined in:
lib/mailauth/dkim/algorithm.rb

Overview

Both directions of the same maths, in one place: RFC 8463 ยง3 has EdDSA sign the hash of the canonicalized headers where RSA signs the headers themselves, and a signer and a verifier that disagree about that produce signatures nobody can check.

Class Method Summary collapse

Class Method Details

.sign(data, key:, digest: OpenSSL::Digest::SHA256) ⇒ Object



10
11
12
13
14
15
# File 'lib/mailauth/dkim/algorithm.rb', line 10

def self.sign(data, key:, digest: OpenSSL::Digest::SHA256)
  case KEY_TYPES.fetch(key.oid)
  when "rsa"     then key.sign(digest.new, data)
  when "ed25519" then key.sign(nil, digest.digest(data))
  end
end

.verified?(data, key:, signature:, digest: OpenSSL::Digest::SHA256) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
# File 'lib/mailauth/dkim/algorithm.rb', line 17

def self.verified?(data, key:, signature:, digest: OpenSSL::Digest::SHA256)
  case KEY_TYPES.fetch(key.oid)
  when "rsa"     then key.verify(digest.new, signature, data)
  when "ed25519" then key.verify(nil, signature, digest.digest(data))
  end
rescue OpenSSL::PKey::PKeyError
  # A signature of the wrong length for the key never reaches the maths.
  false
end