Class: Classifieds::Encryptor

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

Instance Method Summary collapse

Constructor Details

#initialize(password, salt) ⇒ Encryptor

Returns a new instance of Encryptor.



3
4
5
6
7
# File 'lib/classifieds/encryptor.rb', line 3

def initialize(password, salt)
  @cipher = OpenSSL::Cipher.new('AES-256-CBC')
  @password = password
  @salt = salt
end

Instance Method Details

#decrypt(data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/classifieds/encryptor.rb', line 22

def decrypt(data)
  @cipher.decrypt
  key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(
    @password,
    @salt,
    1000,
    @cipher.key_len + @cipher.iv_len
  )
  @cipher.key = key_iv[0, @cipher.key_len]
  @cipher.iv = key_iv[@cipher.key_len, @cipher.iv_len]
  @cipher.update(Base64.decode64(data)) + @cipher.final
end

#encrypt(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/classifieds/encryptor.rb', line 9

def encrypt(data)
  @cipher.encrypt
  key_iv = OpenSSL::PKCS5.pbkdf2_hmac_sha1(
    @password,
    @salt,
    1000,
    @cipher.key_len + @cipher.iv_len
  )
  @cipher.key = key_iv[0, @cipher.key_len]
  @cipher.iv = key_iv[@cipher.key_len, @cipher.iv_len]
  Base64.encode64(@cipher.update(data) + @cipher.final)
end