Class: Tcat::EncryptionService

Inherits:
Object
  • Object
show all
Defined in:
lib/tcat/encryption_service.rb

Defined Under Namespace

Classes: EncryptionError

Constant Summary collapse

CIPHER_ALGORITHM =
'AES-128-ECB'

Instance Method Summary collapse

Constructor Details

#initialize(secret_key) ⇒ EncryptionService

Returns a new instance of EncryptionService.



12
13
14
15
# File 'lib/tcat/encryption_service.rb', line 12

def initialize(secret_key)
  @secret_key = secret_key
  validate_key!
end

Instance Method Details

#encrypt(message) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/tcat/encryption_service.rb', line 17

def encrypt(message)
  cipher = setup_cipher
  padded_message = pad_message(message, cipher.block_size)
  encrypted = cipher.update(padded_message)
  Base64.strict_encode64(encrypted)
rescue OpenSSL::Cipher::CipherError => e
  raise EncryptionError, "Encryption failed: #{e.message}"
rescue StandardError => e
  raise EncryptionError, "Unexpected error during encryption: #{e.message}"
end