Class: RubyEventStore::Mappers::EncryptionKey

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/mappers/encryption_key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cipher:, key:) ⇒ EncryptionKey

Returns a new instance of EncryptionKey.



6
7
8
9
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 6

def initialize(cipher:, key:)
  @cipher = cipher
  @key = key
end

Instance Attribute Details

#cipherObject (readonly)

Returns the value of attribute cipher.



39
40
41
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 39

def cipher
  @cipher
end

#keyObject (readonly)

Returns the value of attribute key.



39
40
41
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 39

def key
  @key
end

Instance Method Details

#decrypt(message, iv) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

#encrypt(message, iv) ⇒ Object



11
12
13
14
15
16
17
# 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_ivObject



34
35
36
37
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 34

def random_iv
  crypto = prepare_encrypt(cipher)
  crypto.random_iv
end