File: sign_part.rb

package info (click to toggle)
ruby-mail-gpg 0.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 328 kB
  • sloc: ruby: 2,289; makefile: 6
file content (32 lines) | stat: -rw-r--r-- 1,099 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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

      # true if all signatures are valid
      def self.signature_valid?(plain_part, signature_part, options = {})
        verify_signature(plain_part, signature_part, options)[0]
      end

      # will return [success(boolean), verify_result(as returned by gpgme)]
      def self.verify_signature(plain_part, signature_part, options = {})
        if !(signature_part.has_content_type? &&
             ('application/pgp-signature' == signature_part.mime_type))
          return false
        end

        signature = signature_part.body.encoded
        GpgmeHelper.sign_verify(plain_part.encoded, signature, options)
      end
    end
  end
end