Module: HexaPDF::Encryption::AES

Included in:
FastAES, RubyAES
Defined in:
lib/hexapdf/encryption/aes.rb

Overview

Common interface for AES algorithms

This module defines the common interface that is used by the security handlers to encrypt or decrypt data with AES. It has to be prepended by any specific AES algorithm class.

See the ClassMethods module for available class level methods of AES algorithms.

Implementing an AES Class

An AES class needs to define at least the following methods:

initialize(key, iv, mode)

Initializes the AES algorithm with the given key and initialization vector. The mode determines how the AES algorithm object works: If the mode is :encrypt, the object encrypts the data, if the mode is :decrypt, the object decrypts the data.

process(data)

Processes the data and returns the encrypted/decrypted data. The method can assume that the passed in data always has a length that is a multiple of BLOCK_SIZE.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VALID_KEY_LENGTH =

Valid AES key lengths

[16, 24, 32].freeze
BLOCK_SIZE =

The AES block size

16

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(klass) ⇒ Object

Automatically extends the klass with the necessary class level methods.



203
204
205
# File 'lib/hexapdf/encryption/aes.rb', line 203

def self.prepended(klass) # :nodoc:
  klass.extend(ClassMethods)
end

Instance Method Details

#initialize(key, iv, mode) ⇒ Object

Creates a new AES object using the given encryption key and initialization vector.

The mode must either be :encrypt or :decrypt.

Classes prepending this module have to have their own initialization method as this method just performs basic checks.



213
214
215
216
217
218
219
220
221
222
# File 'lib/hexapdf/encryption/aes.rb', line 213

def initialize(key, iv, mode)
  unless VALID_KEY_LENGTH.include?(key.length)
    raise HexaPDF::EncryptionError, "AES key length must be 128, 192 or 256 bit"
  end
  unless iv.length == BLOCK_SIZE
    raise HexaPDF::EncryptionError, "AES initialization vector length must be 128 bit"
  end
  mode = mode.intern
  super
end