Class: OpenPGP::Message

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/openpgp/message.rb

Overview

OpenPGP message.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#packetsObject

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

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/openpgp/message.rb', line 41

def self.decrypt(data, options = {}, &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, options = {}, &block)
  if options[:symmetric]
    key    = (options[:key]    || S2K::DEFAULT.new(options[:passphrase]))
    cipher = (options[: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

Returns:

  • (Boolean)


86
87
88
# File 'lib/openpgp/message.rb', line 86

def empty?
  packets.empty?
end

#sizeObject



90
91
92
# File 'lib/openpgp/message.rb', line 90

def size
  inject(0) { |sum, packet| sum + packet.size }
end

#to_aObject



78
79
80
# File 'lib/openpgp/message.rb', line 78

def to_a
  packets.to_a
end

#to_sObject



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