Class: Aldagai::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/aldagai/encryptor.rb

Instance Method Summary collapse

Constructor Details

#initialize(secret) ⇒ Encryptor

Returns a new instance of Encryptor.



4
5
6
# File 'lib/aldagai/encryptor.rb', line 4

def initialize(secret)
  @secret = secret
end

Instance Method Details

#decrypt(encrypted) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/aldagai/encryptor.rb', line 15

def decrypt(encrypted)
  cipher = OpenSSL::Cipher::AES256.new(:CBC).decrypt
  cipher.key = Digest::SHA256.digest(@secret)

  string = [encrypted].pack('H*').unpack('C*').pack('c*')

  cipher.update(string) + cipher.final
end

#encrypt(plain) ⇒ Object



8
9
10
11
12
13
# File 'lib/aldagai/encryptor.rb', line 8

def encrypt(plain)
  cipher = OpenSSL::Cipher::AES256.new(:CBC).encrypt
  cipher.key = Digest::SHA256.digest(@secret)

  (cipher.update(plain) + cipher.final).unpack('H*')[0].upcase
end