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.



41
42
43
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 41

def cipher
  @cipher
end

#keyObject (readonly)

Returns the value of attribute key.



41
42
43
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 41

def key
  @key
end

Instance Method Details

#decrypt(message, iv) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 23

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
18
19
20
21
# 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

  if crypto.authenticated?
    encrypt_authenticated(crypto, message)
  else
    crypto.update(message) + crypto.final
  end
end

#random_ivObject



36
37
38
39
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 36

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