class RubyEventStore::Mappers::EncryptionKey
Attributes
Public Class Methods
Source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 6 def initialize(cipher:, key:) @cipher = cipher @key = key end
Public Instance Methods
Source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 19 def decrypt(message, iv) crypto = prepare_decrypt(cipher) crypto.iv = iv crypto.key = key ciphertext = ( if crypto.authenticated? ciphertext_from_authenticated(crypto, message) else message end ) (crypto.update(ciphertext) + crypto.final).force_encoding("UTF-8") end
Source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 11 def encrypt(message, iv) crypto = prepare_encrypt(cipher) crypto.iv = iv crypto.key = key crypto.authenticated? ? encrypt_authenticated(crypto, message) : crypto.update(message) + crypto.final end
Source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 34 def random_iv crypto = prepare_encrypt(cipher) crypto.random_iv end
Private Instance Methods
Source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 43 def ciphertext_from_authenticated(crypto, message) prepare_auth_data(crypto) crypto.auth_tag = message[-16...message.length] message[0...-16] end
Source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 49 def encrypt_authenticated(crypto, message) prepare_auth_data(crypto) crypto.update(message) + crypto.final + crypto.auth_tag end
Source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 54 def prepare_auth_data(crypto) crypto.auth_data = "" crypto end
Source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 65 def prepare_decrypt(cipher) crypto = OpenSSL::Cipher.new(cipher) crypto.decrypt crypto end
Source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 59 def prepare_encrypt(cipher) crypto = OpenSSL::Cipher.new(cipher) crypto.encrypt crypto end