Module: Clandestine::Crypt

Defined in:
lib/clandestine/crypt.rb

Class Method Summary collapse

Class Method Details

.decrypt(data, password) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/clandestine/crypt.rb', line 25

def self.decrypt(data, password)
  data = Base64.decode64 data
  cipher = aes(:decrypt)
  cipher.iv = data.slice! -16..-1
  key_len = cipher.key_len
  salt = data.slice! -16..-1
  cipher.key = get_key(password,key_len, salt)
  cipher.update(data) << cipher.final
rescue OpenSSL::Cipher::CipherError
  raise ClandestineError, 'Invalid password!'
end

.encrypt(data, password) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/clandestine/crypt.rb', line 15

def self.encrypt(data, password)
  cipher = aes(:encrypt)
  cipher.iv = iv = cipher.random_iv
  key_len = cipher.key_len
  salt = OpenSSL::Random.random_bytes 16
  cipher.key = key = get_key(password, key_len, salt)
  encrypted = cipher.update(data) << cipher.final
  Base64.encode64 encrypted << salt << iv
end

.hash_password(password) ⇒ Object



7
8
9
# File 'lib/clandestine/crypt.rb', line 7

def self.hash_password(password)
  BCrypt::Password.create(password).b
end

.matches(hash, password) ⇒ Object



11
12
13
# File 'lib/clandestine/crypt.rb', line 11

def self.matches(hash, password)
  BCrypt::Password.new(hash) == password
end