Class: Slosilo::Migration::Symmetric

Inherits:
Object
  • Object
show all
Defined in:
lib/slosilo/migration/symmetric.rb

Instance Method Summary collapse

Constructor Details

#initializeSymmetric

Returns a new instance of Symmetric.



3
4
5
# File 'lib/slosilo/migration/symmetric.rb', line 3

def initialize
  @cipher = OpenSSL::Cipher.new 'AES-256-CBC'
end

Instance Method Details

#cipher_nameObject

This lets us do a final sanity check in migrations from older encryption versions



8
9
10
# File 'lib/slosilo/migration/symmetric.rb', line 8

def cipher_name
  @cipher.name
end

#decrypt(ciphertext, opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/slosilo/migration/symmetric.rb', line 21

def decrypt ciphertext, opts = {}
  @cipher.reset
  @cipher.decrypt
  @cipher.key = opts[:key]
  @cipher.iv, ctxt = ciphertext.unpack("a#{@cipher.iv_len}a*")
  ptxt = @cipher.update(ctxt)
  ptxt + @cipher.final
end

#encrypt(plaintext, opts = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/slosilo/migration/symmetric.rb', line 12

def encrypt plaintext, opts = {}
  @cipher.reset
  @cipher.encrypt
  @cipher.key = opts[:key]
  @cipher.iv = iv = random_iv
  ctxt = @cipher.update(plaintext)
  iv + ctxt + @cipher.final
end

#random_ivObject



30
31
32
# File 'lib/slosilo/migration/symmetric.rb', line 30

def random_iv
  @cipher.random_iv
end

#random_keyObject



34
35
36
# File 'lib/slosilo/migration/symmetric.rb', line 34

def random_key
  @cipher.random_key
end