Module: Passbox

Defined in:
lib/passbox/aes.rb,
lib/passbox/auth.rb,
lib/passbox/crud.rb,
lib/passbox/init.rb

Instance Method Summary collapse

Instance Method Details

#check_passboxObject



24
25
26
27
28
# File 'lib/passbox/init.rb', line 24

def check_passbox
    if !File.exists?($passfile)
        print "Passbox is not setup, please start with 'passbox init' command to start using passbox\n"
    end
end

#create_passObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/passbox/crud.rb', line 23

def create_pass
    check_passbox
    key = passbox_auth
    if key
        while(true)
            print "\nEnter you account name (alphabets/numbers only): "
            acc = gets.chomp.downcase
            if (acc.count("a-z0-9") == acc.length) 
                break
            else
                "\nAccount name can only have Alphabets and Numbers (no special characters), try again!!"
            end
        end
    end
    creds(acc,key)
    print "Account #{acc} has been successfully created!! \n"
end

#creds(acc, key) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/passbox/crud.rb', line 14

def creds(acc, key)
    print "Please enter in your account username: "
    uname = gets.chomp
    pass = get_password_from_user(:account)
    hash = {:username => uname, :password => pass}
    json = hash.to_json
    encrypt(json, key, "#{$pbdir}/#{acc}.pb")
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



60
61
62
63
64
65
66
67
# File 'lib/passbox/crud.rb', line 60

def delete_pass
    check_passbox
    acc = 
    if key
        File.delete("#{$pbdir}/#{acc}.pb")
        print("\nAccount #{acc} has been deleted!!")
    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

#get_password_from_user(action = :account) ⇒ Object



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

def get_password_from_user(action=:account)
    if (action == :account)
        print "Please enter your account password: "
        return password_input(action)
    elsif (action == :master)
        while(true)
            print "Please create your master password (min 8 chars): "
            pass256 = password_input(action)
            return pass256 if pass256;
        end
    elsif (action == :auth)
        print "Please enter your master password: "
        return password_input(action)
    end
end

#initObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/passbox/init.rb', line 7

def init
    pass256=""
    
    if (Dir.exists?($pbdir))
        if(File.exists?($passfile))
            print "Your passbox is already setup. Please type 'passbox help' to see usage.\n"
            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

#list_of_accountsObject



69
70
71
72
73
74
75
# File 'lib/passbox/crud.rb', line 69

def list_of_accounts
    check_passbox
    files_ext = Dir["#{$pbdir}/*.pb"]
    files_ext.each_with_index do |file,i|
        print "#{i+1}. #{file.split('/').last.split('.').first}\n"
    end
end

#passbox_authObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/passbox/auth.rb', line 40

def passbox_auth
    pass256User = get_password_from_user(:auth)
    pass256File = decrypt($passfile, pass256User)
    if pass256File == pass256User
        print("Authentication Successful!!\n")
        return pass256File
    else
        print("Authentication Failed!!\n")
        return false
    end
end

#password_input(action) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/passbox/auth.rb', line 21

def password_input(action)
    pass = STDIN.noecho(&:gets).chomp
    if (pass.length < 8 && action != :account) 
        if (action == :master) 
            print "\nPassword should be minimum 8 characters, try again!!\n"
            return false
        elsif (action == :auth)
            print "\nInvalid Password!!\n"
            exit(0)
        end 
    elsif (action == :account)
        print("\n")
        return pass
    else
        print("\n")
        return Digest::SHA256.hexdigest(pass)
    end
end

#read_passObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/passbox/crud.rb', line 41

def read_pass
    check_passbox
    acc=
    key = passbox_auth
    if key
        data = JSON.parse(decrypt("#{$pbdir}/#{acc}.pb", key))
        print "username : #{data['username']}\n"
        print "password : #{data['password']}\n"
    end
end

#update_passObject



52
53
54
55
56
57
58
# File 'lib/passbox/crud.rb', line 52

def update_pass
    check_passbox
    acc=
    key = passbox_auth
    creds(acc,key)
    print "Account details has been successfully updated!! \n"
end

#verify_accountObject



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

def 
    print "Please enter you account name (case-sensitive): "
    acc = gets.chomp
    if (!File.exists?("#{$pbdir}/#{acc}.pb"))
        print "Account not found, Use 'passbox list' to see all your existing accounts.\n"
        exit(0)
    end
    return acc
end