Class: HexaPDF::Encryption::FastAES

Inherits:
Object
  • Object
show all
Includes:
AES
Defined in:
lib/hexapdf/encryption/fast_aes.rb

Overview

Implementation of the general encryption algorithm AES using OpenSSL as backend.

Since OpenSSL is a native Ruby extension (that comes bundled with Ruby) it is much faster than the pure Ruby version and it can use the AES-NI instruction set on CPUs when available.

This implementation is using AES in Cipher Block Chaining (CBC) mode.

See: PDF2.0 s7.6.3

Constant Summary

Constants included from AES

AES::BLOCK_SIZE, AES::VALID_KEY_LENGTH

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AES

prepended

Constructor Details

#initialize(key, iv, mode) ⇒ FastAES

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

The mode must either be :encrypt or :decrypt.



66
67
68
69
70
71
72
# File 'lib/hexapdf/encryption/fast_aes.rb', line 66

def initialize(key, iv, mode)
  @cipher = OpenSSL::Cipher.new("AES-#{key.length << 3}-CBC")
  @cipher.send(mode)
  @cipher.key = key
  @cipher.iv = iv
  @cipher.padding = 0 # Padding handled by HexaPDF, also no @cipher.final call needed
end

Class Method Details

.random_bytes(n) ⇒ Object

Uses OpenSSL to generate the requested random bytes.

See AES::ClassMethods#random_bytes for more information.



59
60
61
# File 'lib/hexapdf/encryption/fast_aes.rb', line 59

def self.random_bytes(n)
  OpenSSL::Random.random_bytes(n)
end

Instance Method Details

#process(data) ⇒ Object

Encrypts or decrypts the given data whose length must be a multiple of 16.



75
76
77
# File 'lib/hexapdf/encryption/fast_aes.rb', line 75

def process(data)
  @cipher.update(data)
end