Permalink
Browse files

Method to get signature+data packet from message, and to do verificat…

…ions
  • Loading branch information...
singpolyma committed Aug 12, 2010
1 parent 2188810 commit caa79d149ad26362fc111d97b06973df2734b0d7
Showing with 29 additions and 0 deletions.
  1. +29 −0 lib/openpgp/message.rb
View
@@ -86,6 +86,35 @@ def initialize(*packets, &block)
block.call(self) if block_given?
end
+ def signature_and_data(index=0)
+ msg = self
+ msg = msg.first while msg.first.is_a?(OpenPGP::Packet::CompressedData)
+ signature_packet = data_packet = nil
+ i = 0
+ msg.each { |packet|
+ if packet.is_a?(OpenPGP::Packet::Signature)
+ signature_packet = packet if i == index
+ i += 1
+ elsif packet.is_a?(OpenPGP::Packet::LiteralData)
+ data_packet = packet
+ end
+ break if signature_packet && data_packet
+ }
+ [signature_packet, data_packet]
+ end
+
+ ##
+ # @param verifiers a Hash of callables formatted like {'RSA' => {'SHA256' => callable}} that take two parameters: message and signature
+ # @param index signature number to verify (if more than one)
+ def verify(verifiers, index=0)
+ signature_packet, data_packet = signature_and_data(index)
+ return nil unless signature_packet && data_packet # No signature or no data
+ verifier = verifiers[signature_packet.key_algorithm_name][signature_packet.hash_algorithm_name]
+ return nil unless verifier # No verifier
+ data_packet.normalize
+ verifier.call(data_packet.data + signature_packet.trailer, signature_packet.fields)
+ end
+
##
# @yield [packet]
# @yieldparam [Packet] packet

0 comments on commit caa79d1

Please sign in to comment.