Class: OpenPGP::Cipher

Inherits:
Object
  • Object
show all
Defined in:
lib/openpgp/cipher.rb,
lib/openpgp/cipher/aes.rb,
lib/openpgp/cipher/3des.rb,
lib/openpgp/cipher/idea.rb,
lib/openpgp/cipher/cast5.rb,
lib/openpgp/cipher/twofish.rb,
lib/openpgp/cipher/blowfish.rb

Overview

OpenPGP cipher algorithm.

Direct Known Subclasses

AES, Blowfish, CAST5, IDEA, TripleDES, Twofish

Defined Under Namespace

Classes: AES, AES128, AES192, AES256, Blowfish, CAST5, IDEA, TripleDES, Twofish

Constant Summary collapse

DEFAULT =
AES128

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, options = {}) ⇒ Cipher

Returns a new instance of Cipher.



38
39
40
41
42
43
44
# File 'lib/openpgp/cipher.rb', line 38

def initialize(key, options = {})
  @key = case key
    when S2K then key.to_key(key_size)
    else S2K::Simple.new(key).to_key(key_size)
  end
  @options = options
end

Instance Attribute Details

#engineObject

Returns the value of attribute engine.



36
37
38
# File 'lib/openpgp/cipher.rb', line 36

def engine
  @engine
end

#keyObject

Returns the value of attribute key.



35
36
37
# File 'lib/openpgp/cipher.rb', line 35

def key
  @key
end

#optionsObject

Returns the value of attribute options.



35
36
37
# File 'lib/openpgp/cipher.rb', line 35

def options
  @options
end

Class Method Details

.for(identifier) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/openpgp/cipher.rb', line 20

def self.for(identifier)
  case identifier
    when Symbol then const_get(identifier.to_s.upcase)
    when String then const_get(identifier.upcase.to_sym)
    when 1      then IDEA
    when 2      then TripleDES
    when 3      then CAST5
    when 4      then Blowfish
    when 7      then AES128
    when 8      then AES192
    when 9      then AES256
    when 10     then Twofish
  end
end

.identifierObject



48
49
50
# File 'lib/openpgp/cipher.rb', line 48

def self.identifier
  const_get(:IDENTIFIER)
end

.to_iObject



46
# File 'lib/openpgp/cipher.rb', line 46

def self.to_i() identifier end

Instance Method Details

#block_sizeObject



60
61
62
# File 'lib/openpgp/cipher.rb', line 60

def block_size
  @block_size ||= engine.block_size
end

#decrypt(ciphertext) ⇒ Object



104
105
106
107
108
# File 'lib/openpgp/cipher.rb', line 104

def decrypt(ciphertext)
  # TODO
  engine.reset
  engine.decrypt
end

#encrypt(plaintext) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/openpgp/cipher.rb', line 72

def encrypt(plaintext)
  ciphertext = String.new

  engine.reset
  engine.encrypt

  # IV
  rblock = Random.bytes(block_size)
  iblock = encrypt_block("\0" * block_size)
  block_size.times do |i|
    ciphertext << (iblock[i] ^= rblock[i]).chr
  end

  # Checksum
  iblock = encrypt_block(iblock)
  ciphertext << (iblock[0] ^ rblock[block_size - 2]).chr
  ciphertext << (iblock[1] ^ rblock[block_size - 1]).chr

  # Resync
  iblock = ciphertext[2..-1]

  # Encrypt
  plaintext.size.times do |n|
    if (i = n % block_size) == 0
      iblock = encrypt_block(iblock)
    end
    ciphertext << (iblock[i] ^= plaintext[n]).chr
  end

  ciphertext
end

#encrypt_block(block) ⇒ Object



110
111
112
113
114
115
# File 'lib/openpgp/cipher.rb', line 110

def encrypt_block(block)
  engine.encrypt
  engine.key = @key
  engine.iv  = (@iv ||= "\0" * engine.iv_len)
  engine.update(block) << engine.final
end

#identifierObject



52
53
54
# File 'lib/openpgp/cipher.rb', line 52

def identifier()
  self.class.identifier
end

#key_sizeObject



56
57
58
# File 'lib/openpgp/cipher.rb', line 56

def key_size
  @key_size ||= engine.key_len
end