Class: Cryptor::SymmetricEncryption::Ciphers::MessageEncryptor

Inherits:
Cryptor::SymmetricEncryption::Cipher show all
Defined in:
lib/cryptor/symmetric_encryption/ciphers/message_encryptor.rb

Overview

Cryptor enforces the usage of independent keys for AES encryption and HMAC by mandating a 64-byte key (using 32-bytes for AES and 32-bytes for HMAC).

This scheme is probably safe to use, but less interoperable and more poorly designed than xsalsa20poly1305 from RbNaCl. It does, however, work using only ActiveSupport and the Ruby OpenSSL extension as dependencies, and should be available anywhere.

For the time being, this scheme is only supported for ActiveSupport 4.0+ although support for earlier versions of ActiveSupport should be possible.

Constant Summary collapse

SERIALIZER =
ActiveSupport::MessageEncryptor::NullSerializer
KEY_BYTES =
64

Constants inherited from Cryptor::SymmetricEncryption::Cipher

Cryptor::SymmetricEncryption::Cipher::REGISTRY

Instance Attribute Summary

Attributes inherited from Cryptor::SymmetricEncryption::Cipher

#algorithm, #key_bytes

Instance Method Summary collapse

Methods inherited from Cryptor::SymmetricEncryption::Cipher

[], #initialize, #random_key, register

Constructor Details

This class inherits a constructor from Cryptor::SymmetricEncryption::Cipher

Instance Method Details

#decrypt(key, ciphertext) ⇒ Object



34
35
36
37
38
# File 'lib/cryptor/symmetric_encryption/ciphers/message_encryptor.rb', line 34

def decrypt(key, ciphertext)
  encryptor(key).decrypt_and_verify(ciphertext)
rescue ActiveSupport::MessageVerifier::InvalidSignature => ex
  raise CorruptedMessageError, ex.to_s
end

#encrypt(key, plaintext) ⇒ Object



30
31
32
# File 'lib/cryptor/symmetric_encryption/ciphers/message_encryptor.rb', line 30

def encrypt(key, plaintext)
  encryptor(key).encrypt_and_sign(plaintext)
end