Module: Passbox

Defined in:
lib/passbox/aes.rb,
lib/passbox/auth.rb,
lib/passbox/init.rb,
lib/passbox/accounts.rb,
lib/passbox/crud/read.rb,
lib/passbox/crud/create.rb,
lib/passbox/crud/delete.rb,
lib/passbox/crud/update.rb,
lib/passbox/helpers/options.rb,
lib/passbox/helpers/strings.rb

Instance Method Summary collapse

Instance Method Details

#account_already_existsObject



25
# File 'lib/passbox/helpers/strings.rb', line 25

def ; return "Account Name already exists, try different name!!\n"; end

#account_name_blankObject



3
# File 'lib/passbox/helpers/strings.rb', line 3

def ; return "Account Name cannot be empty, try again!!\n"; end

#account_name_invalidObject



26
# File 'lib/passbox/helpers/strings.rb', line 26

def ;   return "Alphabets, Numbers, Underscore and Dashes only, try again please!!\n"; end

#account_not_foundObject



18
# File 'lib/passbox/helpers/strings.rb', line 18

def ;  return "Account not found, Use 'passbox list' to see all your existing accounts.\n"; end

#account_update_successObject

update.rb



34
# File 'lib/passbox/helpers/strings.rb', line 34

def ; return "\nAccount details has been successfully updated!! \n"; end

#auth_failedObject



51
# File 'lib/passbox/helpers/strings.rb', line 51

def auth_failed;                return "Authentication Failed!!\n"; end

#auth_successObject



50
# File 'lib/passbox/helpers/strings.rb', line 50

def auth_success;               return "Authentication Successful!!\n"; end

#cc(acc, key, action = :create) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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_blankObject



9
# File 'lib/passbox/helpers/strings.rb', line 9

def cc_cvv_blank;       return "Card CVV cannot be empty, try again!!\n"; end

#cc_exp_blankObject



8
# File 'lib/passbox/helpers/strings.rb', line 8

def cc_exp_blank;       return "Card Expiry cannot be empty, try again!!\n"; end

#cc_no_blankObject



7
# File 'lib/passbox/helpers/strings.rb', line 7

def cc_no_blank;        return "Credit/Debit Card cannot be empty, try again!!\n"; end

#check_passboxObject



25
26
27
28
29
30
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/passbox/crud/create.rb', line 20

def (option, key)
    attempts = 0
    while(true)
        if attempts == 3
            print too_many_attempts.bold.red
            exit(0) 
        end
        print 
        acc = user_input.downcase
        if acc.empty?
            print .red
            attempts = attempts + 1
            next;
        end
        if (acc.count("a-z0-9_-") == acc.length)
             = (acc, option)
            if 
                print .red
                attempts = attempts + 1
                next;
            end
            break
        else
            print 
            attempts = attempts + 1
        end
    end
    return acc
end

#create_master_passwordObject



42
# File 'lib/passbox/helpers/strings.rb', line 42

def create_master_password;     return "Please create your master password (min 8 chars): "; end

#create_passObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/passbox/crud/create.rb', line 3

def create_pass
    check_passbox
    option = select_option
    key = passbox_auth
    acc = (option, key)
    case option
    when 1
        (acc, key)
    when 2
        pin(acc, key)
    when 3
        cc(acc, key)
    else
        exit(0)
    end
end

#decrypt(datafile, key) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# 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..]
    decipher.key = key[0..31]
    decrypted_data = decipher.update(data) + decipher.final
    return decrypted_data
end

#delete_passObject



3
4
5
6
7
8
9
10
11
# File 'lib/passbox/crud/delete.rb', line 3

def delete_pass
    check_passbox
    filename = 
    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_optionsObject



3
4
5
6
7
8
9
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/passbox/accounts.rb', line 63

def (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) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# 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_nameObject



17
# File 'lib/passbox/helpers/strings.rb', line 17

def ; return "Please enter you account name: "; end

#enter_account_passwordObject

auth.rb



38
# File 'lib/passbox/helpers/strings.rb', line 38

def ;     return "Please enter your Account Password: "; end

#enter_account_pinObject



39
# File 'lib/passbox/helpers/strings.rb', line 39

def ;          return "Please enter you Account Pin: "; end

#enter_cc_cvvObject



40
# File 'lib/passbox/helpers/strings.rb', line 40

def enter_cc_cvv;               return "Please enter you Card CVV: "; end

#enter_cc_expObject



31
# File 'lib/passbox/helpers/strings.rb', line 31

def enter_cc_exp;           return "Please enter your card expiry: "; end

#enter_cc_noObject



30
# File 'lib/passbox/helpers/strings.rb', line 30

def enter_cc_no;            return "Please enter in your credit/debit card number: "; end

#enter_cc_pinObject



41
# File 'lib/passbox/helpers/strings.rb', line 41

def enter_cc_pin;               return "Please enter you Card Pin: "; end

#enter_master_passwordObject



47
# File 'lib/passbox/helpers/strings.rb', line 47

def enter_master_password;      return "Please enter your Master Password: "; end

#enter_noteObject



29
# File 'lib/passbox/helpers/strings.rb', line 29

def enter_note;             return "Enter note to self (optional): "; end

#enter_urlObject



28
# File 'lib/passbox/helpers/strings.rb', line 28

def enter_url;              return "Please enter in the login url: "; end

#enter_usernameObject



27
# 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) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/passbox/auth.rb', line 5

def get_password_from_user(action=:account)
    case action
    when :account
        print 
        return password_input(action)
    when :pin
        print 
        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

#initObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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_passwordObject



49
# File 'lib/passbox/helpers/strings.rb', line 49

def invalid_password;           return "\nInvalid Password!!\n"; end

#invalid_selectionObject



20
# File 'lib/passbox/helpers/strings.rb', line 20

def invalid_selection;  return "\nInvalid selection. Try again. Bye!!\n\n"; end

#list_of_accountsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/passbox/crud/create.rb', line 50

def (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_accountsObject



19
# File 'lib/passbox/helpers/strings.rb', line 19

def multiple_accounts;  return "\nMultiple accounts found, please chose one: "; end

#no_accountsObject

accounts.rb



16
# 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_authObject



74
75
76
77
78
79
80
81
82
83
84
# 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_blankObject



5
# File 'lib/passbox/helpers/strings.rb', line 5

def password_blank;     return "Password cannot be empty, try again!!\n"; end

#password_input(action) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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_validationObject



46
# File 'lib/passbox/helpers/strings.rb', line 46

def password_validation;        return "\nPassword should be minimum 8 characters, try again!!\n"; end

#passwords_mismatchObject



45
# File 'lib/passbox/helpers/strings.rb', line 45

def passwords_mismatch;         return "\n\nPasswords don't match. Try again!!\n\n"; end

#pb_already_setupObject

init.rb



12
# 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_setupObject



13
# 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_completeObject



44
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# 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_blankObject



6
# File 'lib/passbox/helpers/strings.rb', line 6

def pin_blank;          return "Pin cannot be empty, try again!!\n"; end

#re_enter_master_passwordObject



43
# 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) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/passbox/crud/read.rb', line 3

def read_pass(action=:display)
    check_passbox
    filename =  
    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_optionObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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_youObject



48
# File 'lib/passbox/helpers/strings.rb', line 48

def thank_you;                  return "\n\nThank you for using passbox. Bye!!\n"; end

#too_many_attemptsObject

create.rb



23
# 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) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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 = (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_instructionsObject



35
# 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_passObject



3
4
5
6
7
# File 'lib/passbox/crud/update.rb', line 3

def update_pass
    existing, filename, key = read_pass(:update)
    update_details(existing, filename, key)
    print .green
end

#user_inputObject



28
29
30
31
32
33
34
35
# 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_blankObject



4
# File 'lib/passbox/helpers/strings.rb', line 4

def username_blank;     return "Username cannot be empty, try again!!\n"; end

#verify_accountObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/passbox/accounts.rb', line 28

def 
    print 
    acc = user_input
    files = Dir.glob("#{$pbdir}/#{acc}.{pb,pn,cc}")
    if files.size == 0
        print 
        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