Class: OpenPGP::Message

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

Overview

OpenPGP message.

See Also:

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Message) initialize(*packets, &block)

A new instance of Message

Parameters:



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

def initialize(*packets, &block)
  @packets = packets.flatten
  block.call(self) if block_given?
end

Instance Attribute Details

- (Array<Packet>) packets

Returns:



12
13
14
# File 'lib/openpgp/message.rb', line 12

def packets
  @packets
end

Class Method Details

+ (Object) decrypt(data, options = {}, &block)

Parameters:

  • (Object) data
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (Object)

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/openpgp/message.rb', line 49

def self.decrypt(data, options = {}, &block)
  raise NotImplementedError # TODO
end

+ (Message) encrypt(data, options = {}, &block)

Creates an encrypted OpenPGP message.

Parameters:

  • (Object) data
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/openpgp/message.rb', line 20

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

+ (Message) parse(data)

Parses an OpenPGP message.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/openpgp/message.rb', line 60

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

+ (IO, ...) write(io = nil, &block)

Returns:

  • (IO, #write)

    io

  • (void)


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

def self.write(io = nil, &block)
  data = self.new(&block).to_s
  io.respond_to?(:write) ? io.write(data) : data
end

Instance Method Details

- (self) <<(packet)

Parameters:

Returns:

  • (self)


106
107
108
# File 'lib/openpgp/message.rb', line 106

def <<(packet)
  packets << packet
end

- (Enumerator) each {|packet| ... }

Yields:

  • (packet)

Yield Parameters:

Returns:

  • (Enumerator)


93
94
95
# File 'lib/openpgp/message.rb', line 93

def each(&block) # :yields: packet
  packets.each(&block)
end

- (Boolean) empty?

Returns:

  • (Boolean)


112
113
114
# File 'lib/openpgp/message.rb', line 112

def empty?
  packets.empty?
end

- (Integer) size

Returns:

  • (Integer)


118
119
120
# File 'lib/openpgp/message.rb', line 118

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

- (Array<Packet>) to_a

Returns:



99
100
101
# File 'lib/openpgp/message.rb', line 99

def to_a
  packets.to_a
end

- (String) to_s

Returns:

  • (String)


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/openpgp/message.rb', line 124

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