Class: Omnizip::Crypto::Aes256

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/crypto/aes256.rb,
lib/omnizip/crypto/aes256/cipher.rb,
lib/omnizip/crypto/aes256/constants.rb,
lib/omnizip/crypto/aes256/key_derivation.rb

Overview

AES-256 encryption for 7-Zip archives

Implements 7-Zip's AES-256-CBC encryption with:

  • Password-based key derivation (SHA-256)
  • Configurable salt and cycles
  • CBC mode with IV

This follows 7-Zip's encryption specification.

Defined Under Namespace

Modules: Constants Classes: Cipher, KeyDerivation

Constant Summary

Constants included from Constants

Constants::BLOCK_SIZE, Constants::DEFAULT_CYCLES_POWER, Constants::IV_SIZE, Constants::KEY_SIZE, Constants::MAX_CYCLES_POWER, Constants::MIN_CYCLES_POWER, Constants::SALT_SIZE, Constants::SALT_SIZE_MAX

Class Method Summary collapse

Class Method Details

.decrypt(encrypted_data, password, salt, iv, cycles_power) ⇒ String

Decrypt data with AES-256

Parameters:

  • encrypted_data (String)

    Encrypted data

  • password (String)

    Decryption password

  • salt (String)

    Salt used during encryption

  • iv (String)

    IV used during encryption

  • cycles_power (Integer)

    Cycles power used during encryption

Returns:

  • (String)

    Decrypted data



79
80
81
82
83
84
# File 'lib/omnizip/crypto/aes256.rb', line 79

def self.decrypt(encrypted_data, password, salt, iv, cycles_power)
  key = KeyDerivation.derive_key(password, salt, cycles_power)
  cipher = Cipher.new(key, iv)

  cipher.decrypt(encrypted_data)
end

.encrypt(data, password, options = {}) ⇒ Hash

Encrypt data with AES-256

Parameters:

  • data (String)

    Data to encrypt

  • password (String)

    Encryption password

  • options (Hash) (defaults to: {})

    Encryption options

Options Hash (options):

  • :num_cycles_power (Integer)

    Power of 2 for key derivation iterations

  • :salt (String)

    Random salt (auto-generated if nil)

  • :iv (String)

    Initialization vector (auto-generated if nil)

Returns:

  • (Hash)

    Encrypted data with metadata



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/omnizip/crypto/aes256.rb', line 53

def self.encrypt(data, password, options = {})
  salt = options[:salt] || generate_salt
  iv = options[:iv] || generate_iv
  cycles_power = options[:num_cycles_power] || DEFAULT_CYCLES_POWER

  key = KeyDerivation.derive_key(password, salt, cycles_power)
  cipher = Cipher.new(key, iv)

  encrypted_data = cipher.encrypt(data)

  {
    data: encrypted_data,
    salt: salt,
    iv: iv,
    cycles_power: cycles_power,
  }
end

.generate_ivString

Generate random IV

Returns:

  • (String)

    Random IV



97
98
99
# File 'lib/omnizip/crypto/aes256.rb', line 97

def self.generate_iv
  OpenSSL::Random.random_bytes(IV_SIZE)
end

.generate_salt(size = SALT_SIZE) ⇒ String

Generate random salt

Parameters:

  • size (Integer) (defaults to: SALT_SIZE)

    Salt size in bytes (default 16)

Returns:

  • (String)

    Random salt



90
91
92
# File 'lib/omnizip/crypto/aes256.rb', line 90

def self.generate_salt(size = SALT_SIZE)
  OpenSSL::Random.random_bytes(size)
end