Class: OpenPGP::Message
- Inherits:
-
Object
- Object
- OpenPGP::Message
- Includes:
- Enumerable
- Defined in:
- lib/openpgp/message.rb
Overview
OpenPGP message.
Instance Attribute Summary collapse
-
#packets ⇒ Object
Returns the value of attribute packets.
Class Method Summary collapse
- .decrypt(data, options = {}, &block) ⇒ Object
-
.encrypt(data, options = {}, &block) ⇒ Object
Creates an encrypted OpenPGP message.
-
.parse(data) ⇒ Object
Parses an OpenPGP message.
- .write(io = nil, &block) ⇒ Object
Instance Method Summary collapse
- #<<(packet) ⇒ Object
-
#each(&block) ⇒ Object
:yields: packet.
- #empty? ⇒ Boolean
-
#initialize(*packets, &block) ⇒ Message
constructor
A new instance of Message.
- #size ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*packets, &block) ⇒ Message
Returns a new instance of Message.
69 70 71 72 |
# File 'lib/openpgp/message.rb', line 69 def initialize(*packets, &block) @packets = packets.flatten block.call(self) if block_given? end |
Instance Attribute Details
#packets ⇒ Object
Returns the value of attribute packets.
11 12 13 |
# File 'lib/openpgp/message.rb', line 11 def packets @packets end |
Class Method Details
.decrypt(data, options = {}, &block) ⇒ Object
41 42 43 |
# File 'lib/openpgp/message.rb', line 41 def self.decrypt(data, = {}, &block) raise NotImplementedError # TODO end |
.encrypt(data, options = {}, &block) ⇒ Object
Creates an encrypted OpenPGP message.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/openpgp/message.rb', line 15 def self.encrypt(data, = {}, &block) if [:symmetric] key = ([:key] || S2K::DEFAULT.new([:passphrase])) cipher = ([:cipher] || Cipher::DEFAULT).new(key) msg = self.new do |msg| msg << Packet::SymmetricSessionKey.new(:algorithm => cipher.identifier, :s2k => key) msg << Packet::EncryptedData.new do |packet| plaintext = self.write do |msg| case data when Message then data.each { |packet| msg << packet } when Packet then msg << data else msg << Packet::LiteralData.new(:data => data) end end packet.data = cipher.encrypt(plaintext) end end block_given? ? block.call(msg) : msg else raise NotImplementedError # TODO end end |
.parse(data) ⇒ Object
Parses an OpenPGP message.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/openpgp/message.rb', line 50 def self.parse(data) data = Buffer.new(data.to_str) if data.respond_to?(:to_str) msg = self.new until data.eof? if packet = OpenPGP::Packet.parse(data) msg << packet else raise "Invalid OpenPGP message data at position #{data.pos}" end end msg end |
.write(io = nil, &block) ⇒ Object
64 65 66 67 |
# File 'lib/openpgp/message.rb', line 64 def self.write(io = nil, &block) data = self.new(&block).to_s io.respond_to?(:write) ? io.write(data) : data end |
Instance Method Details
#<<(packet) ⇒ Object
82 83 84 |
# File 'lib/openpgp/message.rb', line 82 def <<(packet) packets << packet end |
#each(&block) ⇒ Object
:yields: packet
74 75 76 |
# File 'lib/openpgp/message.rb', line 74 def each(&block) # :yields: packet packets.each(&block) end |
#empty? ⇒ Boolean
86 87 88 |
# File 'lib/openpgp/message.rb', line 86 def empty? packets.empty? end |
#size ⇒ Object
90 91 92 |
# File 'lib/openpgp/message.rb', line 90 def size inject(0) { |sum, packet| sum + packet.size } end |
#to_a ⇒ Object
78 79 80 |
# File 'lib/openpgp/message.rb', line 78 def to_a packets.to_a end |
#to_s ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/openpgp/message.rb', line 94 def to_s Buffer.write do |buffer| packets.each do |packet| if body = packet.body buffer.write_byte(packet.class.tag | 0xC0) buffer.write_byte(body.size) buffer.write_bytes(body) end end end end |