Module: AESX
- Defined in:
- lib/aesx.rb
Overview
AESX - AES eXtended encryption library
A lightweight encryption library that provides an extended version of the popular AES gem interface with modern ciphers. The default cipher is AES-256-GCM.
Defined Under Namespace
Classes: AESX
Constant Summary collapse
- CIPHER_SPECS =
Mapping of cipher names to [key_length, iv_length]
{ 'AES-128-CTR' => [16, 16], 'AES-192-CTR' => [24, 16], 'AES-256-CTR' => [32, 16], 'AES-128-GCM' => [16, 12], 'AES-192-GCM' => [24, 12], 'AES-256-GCM' => [32, 12], 'ARIA-128-CTR' => [16, 16], 'ARIA-192-CTR' => [24, 16], 'ARIA-256-CTR' => [32, 16], 'SM4-CTR' => [16, 16], 'SM4-GCM' => [16, 12], 'CHACHA20-POLY1305' => [32, 12] }.freeze
Class Method Summary collapse
-
.available_compression ⇒ Array<Symbol>
Returns an array of available compression algorithms.
-
.cipher_list ⇒ Array<String>
Returns a list of supported ciphers available in the current OpenSSL installation.
-
.decrypt(ciphertext, key, opts = {}) ⇒ String
Decrypts ciphertext using the specified key and options.
-
.default_compression ⇒ Symbol?
Returns the default compression algorithm.
-
.encrypt(plaintext, key, opts = {}) ⇒ String, Array
Encrypts plaintext using the specified key and options.
-
.iv(format = :plain, cipher: 'AES-256-GCM') ⇒ String
Generates a random initialization vector of appropriate length for the specified cipher.
-
.key(length = nil, format = :plain, cipher: 'AES-256-GCM') ⇒ String
Generates a random key of appropriate length for the specified cipher.
Class Method Details
.available_compression ⇒ Array<Symbol>
Returns an array of available compression algorithms
125 126 127 |
# File 'lib/aesx.rb', line 125 def available_compression AESCompression.available_algorithms end |
.cipher_list ⇒ Array<String>
Returns a list of supported ciphers available in the current OpenSSL installation
50 51 52 53 |
# File 'lib/aesx.rb', line 50 def cipher_list openssl_ciphers = OpenSSL::Cipher.ciphers.map(&:upcase) CIPHER_SPECS.keys & openssl_ciphers end |
.decrypt(ciphertext, key, opts = {}) ⇒ String
Decrypts ciphertext using the specified key and options
85 86 87 88 |
# File 'lib/aesx.rb', line 85 def decrypt(ciphertext, key, opts={}) cipher = AESX.new(key, opts) cipher.decrypt(ciphertext) end |
.default_compression ⇒ Symbol?
Returns the default compression algorithm
118 119 120 |
# File 'lib/aesx.rb', line 118 def default_compression AESCompression.default_algorithm end |
.encrypt(plaintext, key, opts = {}) ⇒ String, Array
Encrypts plaintext using the specified key and options
68 69 70 71 |
# File 'lib/aesx.rb', line 68 def encrypt(plaintext, key, opts={}) cipher = AESX.new(key, opts) cipher.encrypt(plaintext) end |
.iv(format = :plain, cipher: 'AES-256-GCM') ⇒ String
Generates a random initialization vector of appropriate length for the specified cipher
109 110 111 112 113 |
# File 'lib/aesx.rb', line 109 def iv(format = :plain, cipher: 'AES-256-GCM') iv_length = CIPHER_SPECS[cipher.upcase][1] iv = OpenSSL::Random.random_bytes(iv_length) format == :base_64 ? Base64.encode64(iv).chomp : iv end |
.key(length = nil, format = :plain, cipher: 'AES-256-GCM') ⇒ String
Generates a random key of appropriate length for the specified cipher
97 98 99 100 101 |
# File 'lib/aesx.rb', line 97 def key(length = nil, format = :plain, cipher: 'AES-256-GCM') key_length = length ? length / 8 : CIPHER_SPECS[cipher.upcase][0] key = OpenSSL::Random.random_bytes(key_length) format == :base_64 ? Base64.encode64(key).chomp : key end |