Class: OpenPGP::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/openpgp/packet.rb

Overview

OpenPGP packet.

Defined Under Namespace

Classes: AsymmetricSessionKey, CompressedData, EncryptedData, Experimental, IntegrityProtectedData, LiteralData, Marker, ModificationDetectionCode, OnePassSignature, PublicKey, PublicSubkey, SecretKey, SecretSubkey, Signature, SymmetricSessionKey, Trust, UserAttribute, UserID

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Packet

Returns a new instance of Packet.



87
88
89
90
# File 'lib/openpgp/packet.rb', line 87

def initialize(options = {}, &block)
  options.each { |k, v| send("#{k}=", v) }
  block.call(self) if block_given?
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/openpgp/packet.rb', line 8

def data
  @data
end

#sizeObject

def to_s() body end



94
95
96
# File 'lib/openpgp/packet.rb', line 94

def size
  @size
end

#tagObject

Returns the value of attribute tag.



8
9
10
# File 'lib/openpgp/packet.rb', line 8

def tag
  @tag
end

Class Method Details

.for(tag) ⇒ Object

Returns the implementation class for a packet tag.



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

def self.for(tag)
  @@tags[tag.to_i] || self
end

.parse(data) ⇒ Object

Parses an OpenPGP packet.



26
27
28
29
30
31
32
33
34
# File 'lib/openpgp/packet.rb', line 26

def self.parse(data)
  data = Buffer.new(data.to_str) if data.respond_to?(:to_str)

  unless data.eof?
    new = ((tag = data.getc) & 64).nonzero? # bit 6 indicates new packet format if set
    data.ungetc(tag)
    send(new ? :parse_new_format : :parse_old_format, data)
  end
end

.parse_body(body, options = {}) ⇒ Object



83
84
85
# File 'lib/openpgp/packet.rb', line 83

def self.parse_body(body, options = {})
  self.new(options)
end

.parse_new_format(data) ⇒ Object

Parses a new-format (RFC 4880) OpenPGP packet.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/openpgp/packet.rb', line 40

def self.parse_new_format(data)
  tag = data.getc & 63
  len = data.getc

  case len
    when 0..191   # 4.2.2.1. One-Octet Lengths
      data_length = len
    when 192..223 # 4.2.2.2. Two-Octet Lengths
      data_length = ((len - 192) << 8) + data.getc + 192
    when 224..254 # 4.2.2.4. Partial Body Lengths
      data_length = 1 << (len & 0x1f)
    when 255      # 4.2.2.3. Five-Octet Lengths
      data_length = (data.getc << 24) | (data.getc << 16) | (data.getc << 8) | data.getc
  end

  Packet.for(tag).parse_body(Buffer.new(data.read(data_length)), :tag => tag)
end

.parse_old_format(data) ⇒ Object

Parses an old-format (PGP 2.6.x) OpenPGP packet.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openpgp/packet.rb', line 62

def self.parse_old_format(data)
  len = (tag = data.getc) & 3
  tag = (tag >> 2) & 15

  case len
    when 0 # The packet has a one-octet length. The header is 2 octets long.
      data_length = data.getc
    when 1 # The packet has a two-octet length. The header is 3 octets long.
      data_length = data.read(2).unpack('n').first
    when 2 # The packet has a four-octet length. The header is 5 octets long.
      data_length = data.read(4).unpack('N').first
    when 3 # The packet is of indeterminate length. The header is 1 octet long.
      data_length = false # read to EOF
    else
      raise "Invalid OpenPGP packet length-type: expected 0..3 but got #{len}"
  end

  Packet.for(tag).parse_body(Buffer.new(data_length ? data.read(data_length) : data.read), :tag => tag)
end

.tagObject

Returns the packet tag for this class.



18
19
20
# File 'lib/openpgp/packet.rb', line 18

def self.tag
  @@tags.index(self)
end

Instance Method Details

#bodyObject



96
97
98
# File 'lib/openpgp/packet.rb', line 96

def body
  respond_to?(:write_body) ? Buffer.write { |buffer| write_body(buffer) } : ""
end