Module: PWS::Encryptor

Defined in:
lib/pws/encryptor.rb

Constant Summary collapse

CIPHER =
'aes-256-cbc'

Class Method Summary collapse

Class Method Details

.decrypt(iv_and_data, pwhash) ⇒ Object



7
8
9
10
# File 'lib/pws/encryptor.rb', line 7

def decrypt( iv_and_data, pwhash )
  iv, data = iv_and_data[0,16], iv_and_data[16..-1]
  crypt :decrypt, data, pwhash, iv
end

.encrypt(data, pwhash) ⇒ Object



12
13
14
15
16
# File 'lib/pws/encryptor.rb', line 12

def encrypt( data, pwhash )
  iv = random_iv
  encrypted_data = crypt :encrypt, data, pwhash, iv
  iv + encrypted_data
end

.hash(plaintext) ⇒ Object



18
19
20
# File 'lib/pws/encryptor.rb', line 18

def hash( plaintext )
  OpenSSL::Digest::SHA512.new( plaintext ).digest
end

.random_ivObject

you need a random iv for cbc mode. It is prepended to the encrypted text.



23
24
25
26
# File 'lib/pws/encryptor.rb', line 23

def random_iv
  a = OpenSSL::Cipher.new CIPHER
  a.random_iv
end