Class: CyberplatPKI::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberplat_pki/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



9
10
11
12
13
14
15
16
17
# File 'lib/cyberplat_pki/document.rb', line 9

def initialize
  @engine = nil
  @type = nil
  @subject = nil
  @ca = nil
  @body = nil
  @signature = nil
  @unknown1 = nil
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/cyberplat_pki/document.rb', line 7

def body
  @body
end

#caObject

Returns the value of attribute ca.



7
8
9
# File 'lib/cyberplat_pki/document.rb', line 7

def ca
  @ca
end

#data_lengthObject

Returns the value of attribute data_length.



7
8
9
# File 'lib/cyberplat_pki/document.rb', line 7

def data_length
  @data_length
end

#engineObject

Returns the value of attribute engine.



7
8
9
# File 'lib/cyberplat_pki/document.rb', line 7

def engine
  @engine
end

#signatureObject

Returns the value of attribute signature.



7
8
9
# File 'lib/cyberplat_pki/document.rb', line 7

def signature
  @signature
end

#subjectObject

Returns the value of attribute subject.



7
8
9
# File 'lib/cyberplat_pki/document.rb', line 7

def subject
  @subject
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/cyberplat_pki/document.rb', line 7

def type
  @type
end

Class Method Details

.decode64(data) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cyberplat_pki/document.rb', line 60

def self.decode64(data)
  lines = data.split "\r\n"

  data = ""
  crc = nil

  lines.each do |line|
    if line[0] == '='
      crc, = "\0".concat(Base64.decode64(line[1..-1])).unpack('N')
    else
      data << line
    end
  end

  data = Base64.decode64 data

  if !crc.nil?
    calculated_crc = Digest::CRC24.checksum data

    raise "CyberplatPKI: CRYPT_ERR_RADIX_DECODE (invalid data checksum)" if calculated_crc != crc
  end

  data
end

.encode64(data) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cyberplat_pki/document.rb', line 47

def self.encode64(data)
  encoded = Base64.encode64(data)
  encoded.gsub!(/\n/, "")

  encoded = encoded.scan(/.{1,64}/).join("\r\n") + "\r\n"

  crc = Digest::CRC24.checksum data

  encoded << "=#{Base64.encode64([ crc ].pack("N")[1..-1])[0..-2]}"

  encoded
end

.load(source) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cyberplat_pki/document.rb', line 19

def self.load(source)
  source = source.sub /^[ \t\n\r]*/, ''

  io = StringIO.new source, "rb"
  io.extend DocumentIORoutines

  documents = []

  until io.eof?
    begin
      documents << io.read_document
    rescue EOFError => e
      raise "CyberplatPKI: CRYPT_ERR_INVALID_FORMAT (unexpected end of document)"
    end
  end

  documents
end

.save(documents) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/cyberplat_pki/document.rb', line 38

def self.save(documents)
  io = StringIO.new '', "wb"
  io.extend DocumentIORoutines

  documents.each { |document| io.write_document document }

  io.string[0...-2] # Strip trailing CRLF of last document
end