<commit_before>module Mail
  module Gpg
    class SignPart < Mail::Part

      def initialize(cleartext_mail, options = {})
        signature = GpgmeHelper.sign(cleartext_mail.encoded, options)
        super() do
          body signature.to_s
          content_type "application/pgp-signature; name=\"signature.asc\""
          content_disposition 'attachment; filename="signature.asc"'
          content_description 'OpenPGP digital signature'
        end
      end

      def self.signature_valid?(plain, signature, options = {})
        if !(signature.has_content_type? && ('application/pgp-signature' == signature.mime_type))
          return false
        end

        GpgmeHelper.sign_verify(plain.encoded, signature.body.encoded, options)
      end
    end
  end
end<commit_msg>Fix signature verification for external messages.<commit_after>module Mail
  module Gpg
    class SignPart < Mail::Part

      def initialize(cleartext_mail, options = {})
        signature = GpgmeHelper.sign(cleartext_mail.encoded, options)
        super() do
          body signature.to_s
          content_type "application/pgp-signature; name=\"signature.asc\""
          content_disposition 'attachment; filename="signature.asc"'
          content_description 'OpenPGP digital signature'
        end
      end

      def self.signature_valid?(plain_part, signature_part, options = {})
        if !(signature_part.has_content_type? &&
             ('application/pgp-signature' == signature_part.mime_type))
          return false
        end

        # Work around the problem that plain_part.raw_source prefixes an
        # erronous CRLF, <https://github.com/mikel/mail/issues/702>.
        if ! plain_part.raw_source.empty?
          plaintext = [ plain_part.header.raw_source,
                        "\r\n\r\n",
                        plain_part.body.raw_source
                      ].join
        else
          plaintext = plain_part.encoded
        end

        signature = signature_part.body.encoded
        GpgmeHelper.sign_verify(plaintext, signature, options)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08895265423242468, 'fraction_numerical': 0.0014347202295552368, 'mean_word_length': 2.3312101910828025, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6782665', 'n_tokens_mistral': 570, 'n_tokens_neox': 564, 'n_words': 122}