class Humpass::Database

Attributes

file_path[RW]

Public Class Methods

new(file_path = __dir__ + '/humpass.dat') click to toggle source
# File lib/humpass/database.rb, line 16
def initialize(file_path = __dir__ + '/humpass.dat')
  raise 'Configuration not sat!' if Humpass.configuration.nil?
  @file_path = file_path
end

Public Instance Methods

database_password() click to toggle source
# File lib/humpass/database.rb, line 35
def database_password
  Humpass.configuration.master_password = gets.chomp if Humpass.configuration.master_password.nil?
  Humpass.configuration.master_password
end
decrypt(key, data) click to toggle source
# File lib/humpass/database.rb, line 46
def decrypt(key, data)
  cipher = OpenSSL::Cipher::AES.new(128, :CBC).decrypt
  cipher.key = Digest::SHA2.hexdigest(key)[0..15]
  s = data.unpack("C*").pack("c*")

  begin
    s.empty? ? "" : cipher.update(s) + cipher.final
  rescue
    abort("Wrong Master Password!")
  end
end
encrypt(key, data) click to toggle source
# File lib/humpass/database.rb, line 40
def encrypt(key, data)
  cipher = OpenSSL::Cipher::AES.new(128, :CBC).encrypt
  cipher.key = Digest::SHA2.hexdigest(key)[0..15]
  cipher.update(data) + cipher.final
end
read() click to toggle source
# File lib/humpass/database.rb, line 27
def read
  begin
    Base64.decode64(decrypt(database_password, File.open(file_path, 'r').read))
  rescue
    ''
  end
end
write(data) click to toggle source
# File lib/humpass/database.rb, line 21
def write(data)
  File.open(file_path, 'w+') { |file|
    file.write(encrypt(database_password, Base64.encode64(data)))
  }
end