class Humpass::DataStructure

Attributes

structure[RW]

Public Class Methods

new() click to toggle source
# File lib/humpass/data_structure.rb, line 14
def initialize
  raise 'Configuration not sat!' if Humpass.configuration.nil?
  new_database_initialize if database.read.empty?
  get_structure_from_database
  Humpass.set_lock_words(self.lock_words)
end

Public Instance Methods

add_password(place, password) click to toggle source
# File lib/humpass/data_structure.rb, line 61
def add_password(place, password)
  if self.passwords.select { |password| password.first.eql?(place) }.empty?
    self.passwords << [place, password.encrypt(self.master_password)]
    update_database
  else
    if authorize_replacement?
      remove_place(place)
      add_password(place, password)
    else
      abort('OK!')
    end
  end
end
authorize_replacement?() click to toggle source
# File lib/humpass/data_structure.rb, line 106
def authorize_replacement?
  puts "This place seems to be already registered. Overwrite?[Y/n]"
  overwrite = gets.chomp
  authorize_replacement? unless %w[Y n].include?(overwrite)
  overwrite.eql?('Y') ? true : false
end
database() click to toggle source
# File lib/humpass/data_structure.rb, line 102
def database
  Humpass.database
end
get_password(place) click to toggle source
# File lib/humpass/data_structure.rb, line 75
def get_password(place)
  place = self.passwords.detect { |password| password.first.eql?(place) }
  if place
    place.last.decrypt(self.master_password)
  else
    "Password Not Found!"
  end
end
get_structure_from_database() click to toggle source
# File lib/humpass/data_structure.rb, line 88
def get_structure_from_database
  database_read = database.read
  @structure = database_read.empty? ? '' : JSON.parse(database.read)
end
init_structure() click to toggle source
# File lib/humpass/data_structure.rb, line 26
def init_structure
  {
    master_password: Digest::SHA2.new(256).hexdigest(Humpass.configuration.master_password),
    lock_words: Humpass::LockWord.new.words,
    passwords: []
  }
end
list_passwords() click to toggle source
# File lib/humpass/data_structure.rb, line 84
def list_passwords
  passwords.map{|password| [password.first, password.last.decrypt(self.master_password)]}
end
lock_words() click to toggle source
# File lib/humpass/data_structure.rb, line 48
def lock_words
  @structure["lock_words"]
end
lock_words=(lock_words) click to toggle source
# File lib/humpass/data_structure.rb, line 43
def lock_words=(lock_words)
  @structure["lock_words"] = lock_words
  update_database
end
master_password() click to toggle source
# File lib/humpass/data_structure.rb, line 39
def master_password
  @structure["master_password"]
end
master_password=(master_password) click to toggle source
# File lib/humpass/data_structure.rb, line 34
def master_password=(master_password)
  @structure["master_password"] = Digest::SHA2.new(256).hexdigest(master_password)
  update_database
end
new_database_initialize() click to toggle source
# File lib/humpass/data_structure.rb, line 21
def new_database_initialize
  @structure = init_structure
  update_database
end
passwords() click to toggle source
# File lib/humpass/data_structure.rb, line 57
def passwords
  @structure["passwords"]
end
passwords=(passwords) click to toggle source
# File lib/humpass/data_structure.rb, line 52
def passwords=(passwords)
  @structure["passwords"] = passwords
  update_database
end
remove_place(place) click to toggle source
# File lib/humpass/data_structure.rb, line 93
def remove_place(place)
  self.passwords.delete_if { |password| password.first.eql?(place) }
  update_database
end
update_database() click to toggle source
# File lib/humpass/data_structure.rb, line 98
def update_database
  database.write(@structure.to_json)
end