Module: Duse::Encryption::Symmetric

Extended by:
Encoding, Symmetric
Included in:
Symmetric
Defined in:
lib/duse/encryption.rb

Instance Method Summary collapse

Methods included from Encoding

decode, encode

Instance Method Details

#decrypt(key, iv, cipher_text) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/duse/encryption.rb', line 66

def decrypt(key, iv, cipher_text)
  key = decode(key)
  iv = decode(iv)
  cipher_text = decode(cipher_text)

  cipher = symmetric_algorithm
  cipher.decrypt
  cipher.key = key
  cipher.iv = iv

  plaintext = cipher.update(cipher_text)
  plaintext << cipher.final
  decode(plaintext)
end

#encrypt(plaintext) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/duse/encryption.rb', line 53

def encrypt(plaintext)
  plaintext = encode(plaintext)
  cipher = symmetric_algorithm
  cipher.encrypt
  key = cipher.random_key
  iv = cipher.random_iv

  cipher_text = cipher.update(plaintext)
  cipher_text << cipher.final

  [encode(key), encode(iv), encode(cipher_text)]
end

#symmetric_algorithmObject



81
82
83
# File 'lib/duse/encryption.rb', line 81

def symmetric_algorithm
  OpenSSL::Cipher.new('AES-256-CBC')
end