module Passbox

Public Instance Methods

account_already_exists() click to toggle source
# File lib/passbox/helpers/strings.rb, line 25
def account_already_exists; return "Account Name already exists, try different name!!\n"; end
account_name_blank() click to toggle source
# File lib/passbox/helpers/strings.rb, line 3
def account_name_blank; return "Account Name cannot be empty, try again!!\n"; end
account_name_invalid() click to toggle source
# File lib/passbox/helpers/strings.rb, line 26
def account_name_invalid;   return "Alphabets, Numbers, Underscore and Dashes only, try again please!!\n"; end
account_not_found() click to toggle source
# File lib/passbox/helpers/strings.rb, line 18
def account_not_found;  return "Account not found, Use 'passbox list' to see all your existing accounts.\n"; end
account_update_success() click to toggle source

update.rb

# File lib/passbox/helpers/strings.rb, line 34
def account_update_success; return "\nAccount details has been successfully updated!! \n"; end
auth_failed() click to toggle source
# File lib/passbox/helpers/strings.rb, line 51
def auth_failed;                return "Authentication Failed!!\n"; end
auth_success() click to toggle source
# File lib/passbox/helpers/strings.rb, line 50
def auth_success;               return "Authentication Successful!!\n"; end
cc(acc, key, action = :create) click to toggle source
# File lib/passbox/crud/create.rb, line 80
def cc(acc, key, action = :create)
    attempts = 0
    hash = {}
    hash["card_number"] = fill_manadatory_field(enter_cc_no, cc_no_blank, action)
    hash["card_expiry"] = fill_manadatory_field(enter_cc_exp, cc_exp_blank, action)
    hash["card_cvv"] = fill_manadatory_field("", cc_cvv_blank, action, :cvv)
    cc_pin = get_password_from_user(:card_pin)
    hash["card_pin"] = cc_pin unless cc_pin.empty?
    print enter_note
    note = user_input
    hash["note"] = note unless note.empty?
    return hash if action == :update
    json = hash.to_json
    encrypt(json, key, "#{$pbdir}/#{acc}.cc")
    print "Account #{acc} has been successfully created!! \n\n".green
end
cc_cvv_blank() click to toggle source
# File lib/passbox/helpers/strings.rb, line 9
def cc_cvv_blank;       return "Card CVV cannot be empty, try again!!\n"; end
cc_exp_blank() click to toggle source
# File lib/passbox/helpers/strings.rb, line 8
def cc_exp_blank;       return "Card Expiry cannot be empty, try again!!\n"; end
cc_no_blank() click to toggle source
# File lib/passbox/helpers/strings.rb, line 7
def cc_no_blank;        return "Credit/Debit Card cannot be empty, try again!!\n"; end
check_passbox() click to toggle source
# File lib/passbox/init.rb, line 25
def check_passbox
    if !File.exists?($passfile)
        print pb_not_setup
        exit(0)
    end
end
create_account(option, key) click to toggle source
# File lib/passbox/crud/create.rb, line 20
def create_account(option, key)
    attempts = 0
    while(true)
        if attempts == 3
            print too_many_attempts.bold.red
            exit(0) 
        end
        print enter_account_name
        acc = user_input.downcase
        if acc.empty?
            print account_name_blank.red
            attempts = attempts + 1
            next;
        end
        if (acc.count("a-z0-9_-") == acc.length)
            account_exists = does_account_exists(acc, option)
            if account_exists
                print account_already_exists.red
                attempts = attempts + 1
                next;
            end
            break
        else
            print account_name_invalid
            attempts = attempts + 1
        end
    end
    return acc
end
create_master_password() click to toggle source
# File lib/passbox/helpers/strings.rb, line 42
def create_master_password;     return "Please create your master password (min 8 chars): "; end
create_pass() click to toggle source
# File lib/passbox/crud/create.rb, line 3
def create_pass
    check_passbox
    option = select_option
    key = passbox_auth
    acc = create_account(option, key)
    case option
    when 1
        login(acc, key)
    when 2
        pin(acc, key)
    when 3
        cc(acc, key)
    else
        exit(0)
    end
end
decrypt(datafile, key) click to toggle source
# File lib/passbox/aes.rb, line 14
def decrypt(datafile, key)
    file = File.open(datafile, 'rb')
    data = file.read
    file.close
    decipher = OpenSSL::Cipher::AES256.new(:CTR)
    decipher.decrypt
    decipher.iv = data[0..15]
    data = data[16..-1]
    decipher.key = key[0..31]
    decrypted_data = decipher.update(data) + decipher.final
    return decrypted_data
end
delete_pass() click to toggle source
# File lib/passbox/crud/delete.rb, line 3
def delete_pass
    check_passbox
    filename = verify_account
    key = passbox_auth
    if key
        File.delete(filename)
        print "\nYour account #{filename.split("/").last.split(".").first} has been deleted!!\n\n".bold.yellow
    end
end
display_add_options() click to toggle source
# File lib/passbox/helpers/options.rb, line 3
def display_add_options
    print "\n1. Password"
    print "\n2. Pin"
    print "\n3. Debit/Credit Card"
    print "\nPlease select one of the above options: " 
    return user_input.to_i
end
does_account_exists(acc, type) click to toggle source
# File lib/passbox/accounts.rb, line 63
def does_account_exists(acc, type)
    files = Dir.glob("#{$pbdir}/#{acc}.{pb,pn,cc}")
    if files.size == 0
        return false
    else
        files.each do |file| 
            return true if (file.split(".").last == $options[type])
        end
        return false
    end
end
encrypt(data, key, file) click to toggle source
# File lib/passbox/aes.rb, line 3
def encrypt(data, key, file)
    cipher = OpenSSL::Cipher::AES256.new(:CTR)
    cipher.encrypt
    $iv = cipher.random_iv
    cipher.key = key[0..31]
    encrypted_data = $iv + cipher.update(data) + cipher.final
    file = File.open(file, 'wb')
    file.write(encrypted_data)
    file.close
end
enter_account_name() click to toggle source
# File lib/passbox/helpers/strings.rb, line 17
def enter_account_name; return "Please enter you account name: "; end
enter_account_password() click to toggle source

auth.rb

# File lib/passbox/helpers/strings.rb, line 38
def enter_account_password;     return "Please enter your Account Password: "; end
enter_account_pin() click to toggle source
# File lib/passbox/helpers/strings.rb, line 39
def enter_account_pin;          return "Please enter you Account Pin: "; end
enter_cc_cvv() click to toggle source
# File lib/passbox/helpers/strings.rb, line 40
def enter_cc_cvv;               return "Please enter you Card CVV: "; end
enter_cc_exp() click to toggle source
# File lib/passbox/helpers/strings.rb, line 31
def enter_cc_exp;           return "Please enter your card expiry: "; end
enter_cc_no() click to toggle source
# File lib/passbox/helpers/strings.rb, line 30
def enter_cc_no;            return "Please enter in your credit/debit card number: "; end
enter_cc_pin() click to toggle source
# File lib/passbox/helpers/strings.rb, line 41
def enter_cc_pin;               return "Please enter you Card Pin: "; end
enter_master_password() click to toggle source
# File lib/passbox/helpers/strings.rb, line 47
def enter_master_password;      return "Please enter your Master Password: "; end
enter_note() click to toggle source
# File lib/passbox/helpers/strings.rb, line 29
def enter_note;             return "Enter note to self (optional): "; end
enter_url() click to toggle source
# File lib/passbox/helpers/strings.rb, line 28
def enter_url;              return "Please enter in the login url: "; end
enter_username() click to toggle source
# File lib/passbox/helpers/strings.rb, line 27
def enter_username;         return "Please enter in your username: "; end
fill_manadatory_field(enter_message, blank_message, action=:create, type=nil) click to toggle source
# File lib/passbox/crud/create.rb, line 97
def fill_manadatory_field(enter_message, blank_message, action=:create, type=nil)
    attempts = 0
    while(true)
        if attempts == 3
            print too_many_attempts.bold.red
            exit(0) 
        end
        if (type.nil?)
            print enter_message
            field = user_input
        else
            field = get_password_from_user(type)
        end
        if field.empty?
            if (action == :create)
                print blank_message.red
                attempts = attempts + 1
                next;
            end
        end
        return field  
    end
end
get_password_from_user(action=:account) click to toggle source
# File lib/passbox/auth.rb, line 5
def get_password_from_user(action=:account)
    case action
    when :account
        print enter_account_password
        return password_input(action)
    when :pin
        print enter_account_pin
        return password_input(action)
    when :cvv
        print enter_cc_cvv
        return password_input(action)
    when :card_pin
        print enter_cc_pin
        return password_input(action)
    when :master
        attempt = 0
        while(true)
            attempt = attempt + 1;
            print create_master_password
            pass256 = password_input(action)
            if pass256
                print re_enter_master_password
                re_pass256 = password_input(action)
                if re_pass256 == pass256
                    print pb_setup_complete.bold.green
                    return pass256 
                else
                    print passwords_mismatch.bold.red
                    exit(0);
                end
            else
                print password_validation.red
            end
            if attempt == 3
                print too_many_attempts.bold.red
                exit(0) 
            end
        end
    when :auth
        print enter_master_password
        return password_input(action)
    end
end
init() click to toggle source
# File lib/passbox/init.rb, line 8
def init
    pass256=""
    
    if (Dir.exists?($pbdir))
        if(File.exists?($passfile))
            print pb_already_setup
            return
        else
            pass256 = get_password_from_user(:master)
        end
    else
        pass256 = get_password_from_user(:master)
        Dir.mkdir($pbdir)
    end
    encrypt(pass256, pass256, $passfile)
end
invalid_password() click to toggle source
# File lib/passbox/helpers/strings.rb, line 49
def invalid_password;           return "\nInvalid Password!!\n"; end
invalid_selection() click to toggle source
# File lib/passbox/helpers/strings.rb, line 20
def invalid_selection;  return "\nInvalid selection. Try again. Bye!!\n\n"; end
list_of_accounts() click to toggle source
# File lib/passbox/accounts.rb, line 4
def list_of_accounts
    check_passbox
    empty = true
    files_ext = Dir["#{$pbdir}/*.pb"]
    print "\nLogin & Passwords\n".bold.cyan if files_ext.size != 0
    files_ext.each_with_index do |file,i|
        print "#{i+1}. #{file.split('/').last.split('.').first}\n".cyan
        empty = false
    end
    files_ext = Dir["#{$pbdir}/*.pn"]
    print "\nAccount Pins\n".bold.magenta if files_ext.size != 0
    files_ext.each_with_index do |file,i|
        print "#{i+1}. #{file.split('/').last.split('.').first}\n".magenta
        empty = false
    end
    files_ext = Dir["#{$pbdir}/*.cc"]
    print "\nCredit & Debit Cards\n".bold.yellow if files_ext.size != 0
    files_ext.each_with_index do |file,i|
        print "#{i+1}. #{file.split('/').last.split('.').first}\n".yellow
        empty = false
    end
    print no_accounts.yellow if empty
end
login(acc, key, action=:create) click to toggle source
# File lib/passbox/crud/create.rb, line 50
def login(acc, key, action=:create)
    attempts = 0
    hash = {}
    hash["username"] = fill_manadatory_field(enter_username, username_blank, action)
    hash["password"] = fill_manadatory_field("", password_blank, action, :account)
    print enter_url
    url = user_input
    hash["url"] = url unless url.empty?
    print enter_note
    note = user_input
    hash["note"] = note unless note.empty?
    return hash if action == :update
    json = hash.to_json
    encrypt(json, key, "#{$pbdir}/#{acc}.pb")
    print "Account #{acc} has been successfully created!! \n\n".green
end
multiple_accounts() click to toggle source
# File lib/passbox/helpers/strings.rb, line 19
def multiple_accounts;  return "\nMultiple accounts found, please chose one: "; end
no_accounts() click to toggle source

accounts.rb

# File lib/passbox/helpers/strings.rb, line 16
def no_accounts;        return "\nIts all empty here!! Use 'passbox add' to create new account.\n\n"; end
passbox_auth() click to toggle source
# File lib/passbox/auth.rb, line 74
def passbox_auth
    pass256User = get_password_from_user(:auth)
    pass256File = decrypt($passfile, pass256User)
    if pass256File == pass256User
        print auth_success.bold.green
        return pass256File
    else
        print auth_failed.bold.red
        exit(0)
    end
end
password_blank() click to toggle source
# File lib/passbox/helpers/strings.rb, line 5
def password_blank;     return "Password cannot be empty, try again!!\n"; end
password_input(action) click to toggle source
# File lib/passbox/auth.rb, line 49
def password_input(action)
    begin
        pass = STDIN.noecho(&:gets).chomp
    rescue Interrupt
        puts thank_you.cyan
        exit(0)
    end
    case action
    when :master, :auth
        if (pass.length < 8) 
            if (action == :master) 
                return false
            elsif (action == :auth)
                print invalid_password.bold.red
                exit(0)
            end 
        else
            return Digest::SHA256.hexdigest(pass)
        end
    else
        print("\n")
        return pass
    end
end
password_validation() click to toggle source
# File lib/passbox/helpers/strings.rb, line 46
def password_validation;        return "\nPassword should be minimum 8 characters, try again!!\n"; end
passwords_mismatch() click to toggle source
# File lib/passbox/helpers/strings.rb, line 45
def passwords_mismatch;         return "\n\nPasswords don't match. Try again!!\n\n"; end
pb_already_setup() click to toggle source

init.rb

# File lib/passbox/helpers/strings.rb, line 12
def pb_already_setup;   return "Your passbox is already setup. Please type 'passbox help' to see usage.\n"; end
pb_not_setup() click to toggle source
# File lib/passbox/helpers/strings.rb, line 13
def pb_not_setup;       return "Passbox is not setup, use 'passbox init' command to start.\n"; end
pb_setup_complete() click to toggle source
# File lib/passbox/helpers/strings.rb, line 44
def pb_setup_complete;          return "\n\nPassbox setup complete. Use 'passbox help' to explore.\n\n"; end
pin(acc, key, action = :create) click to toggle source
# File lib/passbox/crud/create.rb, line 67
def pin(acc, key, action = :create)
    attempts = 0
    hash = {}
    hash["pin"] = fill_manadatory_field("", pin_blank, action, :pin)
    print enter_note
    note = user_input
    hash["note"] = note unless note.empty?
    return hash if action == :update
    json = hash.to_json
    encrypt(json, key, "#{$pbdir}/#{acc}.pn")
    print "Account #{acc} has been successfully created!! \n\n".green
end
pin_blank() click to toggle source
# File lib/passbox/helpers/strings.rb, line 6
def pin_blank;          return "Pin cannot be empty, try again!!\n"; end
re_enter_master_password() click to toggle source
# File lib/passbox/helpers/strings.rb, line 43
def re_enter_master_password;   return "\nPlease re-enter your master password: "; end
read_pass(action=:display) click to toggle source
# File lib/passbox/crud/read.rb, line 3
def read_pass(action=:display)
    check_passbox
    filename = verify_account 
    key = passbox_auth

    if key
        data = JSON.parse(decrypt(filename, key))
        return data, filename, key if (action == :update)
        case filename.split(".").last
        when "pb"
            print "\nusername : #{data['username']}\n"
            print "password : #{data['password']}\n"
            print "url : #{data['url']}\n" if data['url']
            print "note : #{data['note']}\n" if data['note']
            print "\n"
        when "pn"
            print "\npin : #{data['pin']}\n"
            print "note : #{data['note']}\n" if data['note']
            print "\n"
        when "cc"
            print "\ncard number : #{data['card_number']}\n"
            print "expiry : #{data['card_expiry']}\n"
            print "cvv : #{data['card_cvv']}\n" if data['card_cvv']
            print "card pin : #{data['card_pin']}\n" if data['card_pin']
            print "note : #{data['note']}\n" if data['note']
            print "\n"
        end
    end
end
select_option() click to toggle source
# File lib/passbox/helpers/options.rb, line 11
def select_option
    attempt = 0
    while(true)
        attempt = attempt + 1
        option = display_add_options
        if (1..3).include?(option)
            return option
        else
            print "\nInvalid selection. Please try again!!\n".red
        end
        if attempt==3
            print "\nToo many invalid attempts. Bye!!\n\n".bold.red
            exit(0)
        end
    end
end
thank_you() click to toggle source
# File lib/passbox/helpers/strings.rb, line 48
def thank_you;                  return "\n\nThank you for using passbox. Bye!!\n"; end
too_many_attempts() click to toggle source

create.rb

# File lib/passbox/helpers/strings.rb, line 23
def too_many_attempts;      return "\nToo many attempts. Start again!!\n\n"; end
update_details(existing, filename, key) click to toggle source
# File lib/passbox/crud/update.rb, line 9
def update_details(existing, filename, key)
    print update_instructions.yellow
    change_flag = false
    case filename.split(".").last
    when "pb"
        updated = login(nil, key, :update)
        existing["username"] = updated["username"] unless updated["username"].empty?
        existing["password"] = updated["password"] unless updated["password"].empty?
        existing["url"] = updated["url"] unless (updated["url"].nil? || updated["url"].empty?)
        existing["note"] = updated["note"] unless (updated["note"].nil? || updated["note"].empty?)
    when "pn"
        updated = pin(nil, key, :update)
        existing["pin"] = updated["pin"] unless updated["pin"].empty?
        existing["note"] = updated["note"] unless (updated["note"].nil? || updated["note"].empty?)
    when "cc"
        updated = cc(nil, key, :update)
        existing["card_number"] = updated["card_number"] unless updated["card_number"].empty?
        existing["card_expiry"] = updated["card_expiry"] unless updated["card_expiry"].empty?
        existing["card_cvv"] = updated["card_cvv"] unless updated["card_cvv"].empty?
        existing["card_pin"] = updated["card_pin"] unless (updated["card_pin"].nil? || updated["card_pin"].empty?)
        existing["note"] = updated["note"] unless (updated["note"].nil? || updated["note"].empty?)
    else
        # do nothing
    end
    encrypt(existing.to_json, key, filename)
end
update_instructions() click to toggle source
# File lib/passbox/helpers/strings.rb, line 35
def update_instructions;    return "\nHit enter for no change or input a new value. \n"; end
update_pass() click to toggle source
# File lib/passbox/crud/update.rb, line 3
def update_pass
    existing, filename, key = read_pass(:update)
    update_details(existing, filename, key)
    print account_update_success.green
end
user_input() click to toggle source
# File lib/passbox/helpers/options.rb, line 28
def user_input
    begin
        return gets.chomp
    rescue Interrupt
        puts "\n\nThank you for using passbox. Bye!!\n".cyan
        exit(0)
    end
end
username_blank() click to toggle source
# File lib/passbox/helpers/strings.rb, line 4
def username_blank;     return "Username cannot be empty, try again!!\n"; end
verify_account() click to toggle source
# File lib/passbox/accounts.rb, line 28
def verify_account
    print enter_account_name
    acc = user_input
    files = Dir.glob("#{$pbdir}/#{acc}.{pb,pn,cc}")
    if files.size == 0
        print account_not_found
        exit(0)
    elsif files.size == 1
        return files.first
    else
        files.each_with_index do |file, i|
            filename = file.split("/").last.split(".").first
            fileext = file.split("/").last.split(".").last
            case fileext
            when "pb"
                print "\n#{i+1}: #{filename} - Login & Password Category"
            when "pn"
                print "\n#{i+1}: #{filename} - Account Pins Category"
            when "cc"
                print "\n#{i+1}: #{filename} - Credit/Debit Card Category"
            else
                # do nothing
            end
        end
        print multiple_accounts
        option = user_input.to_i
        if (1..files.size).include?(option)
            return files[option-1]
        else
            print invalid_selection.red
            exit(0)
        end
    end
end