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.



32
33
34
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 32

def cipher
  @cipher
end

#keyObject (readonly)

Returns the value of attribute key.



32
33
34
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 32

def key
  @key
end

Instance Method Details

#decrypt(message, iv) ⇒ Object



19
20
21
22
23
24
25
# 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) ⇒ 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



27
28
29
30
# File 'lib/ruby_event_store/mappers/encryption_key.rb', line 27

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