Class: Gossiperl::Client::Encryption::Aes256

Inherits:
Resolution
  • Object
show all
Defined in:
lib/gossiperl_client/headers.rb,
lib/gossiperl_client/encryption/aes256.rb

Constant Summary

Constants inherited from Resolution

Resolution::UNDEFINED_VALUE

Instance Method Summary collapse

Methods inherited from Resolution

field

Constructor Details

#initialize(key_in) ⇒ Aes256

Returns a new instance of Aes256.



9
10
11
12
# File 'lib/gossiperl_client/encryption/aes256.rb', line 9

def initialize key_in
  # setup key:
  self.key = ::Digest::SHA256.digest(key_in)
end

Instance Method Details

#algorithmObject



14
15
16
# File 'lib/gossiperl_client/encryption/aes256.rb', line 14

def algorithm
  'AES-256-CBC'
end

#decrypt(cipher) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gossiperl_client/encryption/aes256.rb', line 29

def decrypt cipher
  iv = cipher[0...16]
  cipher_data = cipher[16..-1]
  decode_cipher = ::OpenSSL::Cipher::Cipher.new(algorithm)
  decode_cipher.decrypt
  decode_cipher.key = self.key
  decode_cipher.padding = 0
  decode_cipher.iv = iv
  plain = decode_cipher.update(cipher_data)
  plain << decode_cipher.final
  plain
end

#encrypt(data) ⇒ Object



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

def encrypt data
  random_iv = OpenSSL::Cipher::Cipher.new(algorithm).random_iv
  aes = ::OpenSSL::Cipher::Cipher.new(algorithm)
  aes.encrypt
  aes.key = self.key
  aes.iv = random_iv
  cipher = aes.update(data)
  cipher << aes.final
  random_iv + cipher
end