Class: OpenPGP::Cipher

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

Overview

OpenPGP cipher algorithm.

Direct Known Subclasses

Cipher::AES, Cipher::Blowfish, Cipher::CAST5, Cipher::IDEA, Cipher::TripleDES, Cipher::Twofish

Defined Under Namespace

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

Constant Summary

DEFAULT =
AES128

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Cipher) initialize(key, options = {})

A new instance of Cipher

Parameters:

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


49
50
51
52
53
54
55
# File 'lib/openpgp/cipher.rb', line 49

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

- (String) engine

Returns:

  • (String)


44
45
46
47
48
# File 'lib/openpgp/cipher.rb', line 44

def engine
  @engine ||= Engine::OpenSSL.use do
    OpenSSL::Cipher.new(self.class.const_get(:ENGINE))
  end
end

- (S2K) key

Returns:



38
39
40
# File 'lib/openpgp/cipher.rb', line 38

def key
  @key
end

- (Hash) options

Returns:

  • (Hash)


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

def options
  @options
end

Class Method Details

+ (Class) for(identifier)

Parameters:

  • (Symbol, String, Integer) identifier

Returns:

  • (Class)

See Also:



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

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

+ (Integer) identifier

Returns:

  • (Integer)


63
64
65
# File 'lib/openpgp/cipher.rb', line 63

def self.identifier
  const_get(:IDENTIFIER)
end

+ (Integer) to_i

Returns:

  • (Integer)


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

def self.to_i() identifier end

Instance Method Details

- (Integer) block_size

Returns:

  • (Integer)


81
82
83
# File 'lib/openpgp/cipher.rb', line 81

def block_size
  @block_size ||= engine.block_size
end

- (String) decrypt(ciphertext)

Parameters:

  • (String) ciphertext

Returns:

  • (String)


132
133
134
135
136
# File 'lib/openpgp/cipher.rb', line 132

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

- (String) encrypt(plaintext)

Parameters:

  • (String) plaintext

Returns:

  • (String)

See Also:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/openpgp/cipher.rb', line 97

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

- (String) encrypt_block(block)

Parameters:

  • (String) block

Returns:

  • (String)


141
142
143
144
145
146
# File 'lib/openpgp/cipher.rb', line 141

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

- (Integer) identifier

Returns:

  • (Integer)


69
70
71
# File 'lib/openpgp/cipher.rb', line 69

def identifier()
  self.class.identifier
end

- (Integer) key_size

Returns:

  • (Integer)


75
76
77
# File 'lib/openpgp/cipher.rb', line 75

def key_size
  @key_size ||= engine.key_len
end