Class: Encryption::Symmetric

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

Constant Summary collapse

AES_BLOCKSIZE =
16

Instance Method Summary collapse

Instance Method Details

#decrypt(key, encrypted) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/symmetric/encryption.rb', line 19

def decrypt(key, encrypted)
  cipher = OpenSSL::Cipher::AES128.new(:CBC)
  cipher.padding = 0
  cipher.decrypt
  cipher.key = get_encryption_key(key)
  cipher.iv = encrypted[0..AES_BLOCKSIZE-1]

  unpad_buffer(cipher.update(encrypted[AES_BLOCKSIZE..encrypted.length]) + cipher.final)
end

#digest(value) ⇒ Object



29
30
31
# File 'lib/symmetric/encryption.rb', line 29

def digest(value)
  Digest::SHA256.digest(value)
end

#encrypt(key, message) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/symmetric/encryption.rb', line 9

def encrypt(key, message)
  cipher = OpenSSL::Cipher::AES128.new(:CBC)
  cipher.encrypt
  cipher.key = get_encryption_key(key)
  cipher.padding = 0
  iv = cipher.random_iv

  iv + cipher.update(pad_buffer(message)) + cipher.final
end