class RubyEventStore::Mappers::EncryptionKey

Attributes

cipher[R]
key[R]

Public Class Methods

new(cipher:, key:) click to toggle source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 6
def initialize(cipher:, key:)
  @cipher = cipher
  @key = key
end

Public Instance Methods

decrypt(message, iv) click to toggle 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 = crypto.authenticated? ? ciphertext_from_authenticated(crypto, message) : message
  (crypto.update(ciphertext) + crypto.final).force_encoding("UTF-8")
end
encrypt(message, iv) click to toggle 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
random_iv() click to toggle source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 27
def random_iv
  crypto = prepare_encrypt(cipher)
  crypto.random_iv
end

Private Instance Methods

ciphertext_from_authenticated(crypto, message) click to toggle source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 36
def ciphertext_from_authenticated(crypto, message)
  prepare_auth_data(crypto)
  crypto.auth_tag = message[-16...message.length]
  message[0...-16]
end
encrypt_authenticated(crypto, message) click to toggle source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 42
def encrypt_authenticated(crypto, message)
  prepare_auth_data(crypto)
  crypto.update(message) + crypto.final + crypto.auth_tag
end
prepare_auth_data(crypto) click to toggle source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 47
def prepare_auth_data(crypto)
  crypto.auth_data = ""
  crypto
end
prepare_decrypt(cipher) click to toggle source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 58
def prepare_decrypt(cipher)
  crypto = OpenSSL::Cipher.new(cipher)
  crypto.decrypt
  crypto
end
prepare_encrypt(cipher) click to toggle source
# File lib/ruby_event_store/mappers/encryption_key.rb, line 52
def prepare_encrypt(cipher)
  crypto = OpenSSL::Cipher.new(cipher)
  crypto.encrypt
  crypto
end