class OpenPGP::Cipher

OpenPGP cipher algorithm.

Constants

DEFAULT

Attributes

engine[RW]

@return [String]

key[RW]

@return [S2K]

options[RW]

@return [Hash]

Public Class Methods

for(identifier) click to toggle source

@param [Symbol, String, Integer] identifier @return [Class] @see tools.ietf.org/html/rfc4880#section-9.2

# 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
identifier() click to toggle source

@return [Integer]

# File lib/openpgp/cipher.rb, line 63
def self.identifier
  const_get(:IDENTIFIER)
end
new(key, options = {}) click to toggle source

@param [S2K] key @param [Hash{Symbol => Object}] options

# 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
to_i() click to toggle source

@return [Integer]

# File lib/openpgp/cipher.rb, line 59
def self.to_i() identifier end

Public Instance Methods

block_size() click to toggle source

@return [Integer]

# File lib/openpgp/cipher.rb, line 81
def block_size
  @block_size ||= engine.block_size
end
decrypt(ciphertext) click to toggle source

@param [String] ciphertext @return [String]

# File lib/openpgp/cipher.rb, line 132
def decrypt(ciphertext)
  # TODO
  engine.reset
  engine.decrypt
end
encrypt(plaintext) click to toggle source

@param [String] plaintext @return [String] @see tools.ietf.org/html/rfc4880#section-13.9

# 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
encrypt_block(block) click to toggle source

@param [String] block @return [String]

# 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
identifier() click to toggle source

@return [Integer]

# File lib/openpgp/cipher.rb, line 69
def identifier()
  self.class.identifier
end
key_size() click to toggle source

@return [Integer]

# File lib/openpgp/cipher.rb, line 75
def key_size
  @key_size ||= engine.key_len
end