Module: Adamantite::PWUtils

Included in:
Base::Editor::PasswordObjectEditor
Defined in:
lib/pw_utils/pw_utils.rb

Instance Method Summary collapse

Instance Method Details

#decrypt_pw(iv, pw_hash, master_pw, master_pw_salt) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/pw_utils/pw_utils.rb', line 20

def decrypt_pw(iv, pw_hash, master_pw, master_pw_salt)
  decrypt_cipher = OpenSSL::Cipher::AES256.new(:CBC)
  decrypt_cipher.decrypt
  iv = Base64.decode64(iv.encode('ascii-8bit'))
  decrypt_cipher.iv = iv
  decrypt_cipher.key = Digest::MD5.hexdigest(master_pw + master_pw_salt)
  decrypt_text = Base64.decode64(pw_hash.encode('ascii-8bit'))
  decrypt_cipher.update(decrypt_text) + decrypt_cipher.final
end

#generate_master_pw_comparator(master_pw_hash) ⇒ Object



36
37
38
# File 'lib/pw_utils/pw_utils.rb', line 36

def generate_master_pw_comparator(master_pw_hash)
  BCrypt::Password.new(master_pw_hash)
end

#generate_master_pw_hash(master_pw) ⇒ Object



30
31
32
33
34
# File 'lib/pw_utils/pw_utils.rb', line 30

def generate_master_pw_hash(master_pw)
  salt = BCrypt::Engine.generate_salt
  master_pw_hash = BCrypt::Password.create(master_pw + salt)
  {'salt': salt, 'master_pw_hash': master_pw_hash}
end

#make_pw_info(username, pw, master_pw, master_pw_salt) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pw_utils/pw_utils.rb', line 8

def make_pw_info(username, pw, master_pw, master_pw_salt)
  cipher = OpenSSL::Cipher::AES256.new(:CBC)
  cipher.encrypt
  iv = cipher.random_iv
  cipher.key = Digest::MD5.hexdigest(master_pw + master_pw_salt)
  cipher_text = cipher.update(pw) + cipher.final
  utf8_cipher_text = Base64.encode64(cipher_text).encode('utf-8')
  utf8_iv = Base64.encode64(iv).encode('utf-8')

  {username: username, password: utf8_cipher_text, iv: utf8_iv}
end