-
Notifications
You must be signed in to change notification settings - Fork 0
/
radioDASH_account.py
executable file
·142 lines (116 loc) · 3.41 KB
/
radioDASH_account.py
1
2
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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python3
#
# radioDASH Account
#
# USAGE: radioDASH-account.py -s -c -u USER -p PASSWORD
#
# -s, --store a new user and password
# -c, --check (authenticate) yuser and password
# -u, --user
# -p, --password
#
#
#
#
# IMPORT LIBRARIES
#
import argparse
import hashlib
import binascii
import os
import sys
import time
print (" ")
#
# IMPORT ARGS
#
parser = argparse.ArgumentParser(description='radioDASH Hashgen')
parser.add_argument('-s','--store', help='Store Password', action='store_true')
parser.add_argument('-c','--check', help='Check Password', action='store_true')
parser.add_argument('-d','--delete', help='Delete User', action='store_true')
parser.add_argument('-u','--user', help='Username', required=True)
parser.add_argument('-p','--password', help='Password')
args = vars(parser.parse_args())
actionStore = args['store']
actionCheck = args['check']
actionDelete = args['delete']
user = args['user']
entered_password = args['password']
#
# VARIABLES
#
radioDASHpwf = "cfg/radioDASH.pwf"
#
# FUNCTIONS
#
def hash_password(entered_password):
"""Hash a password for storing."""
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
pwdhash = hashlib.pbkdf2_hmac('sha512', entered_password.encode('utf-8'), salt, 100000)
pwdhash = binascii.hexlify(pwdhash)
return (salt + pwdhash).decode('ascii')
def save_password(user, stored_password):
pwfLine = user + ":" + stored_password + "\n"
f = open(radioDASHpwf, "a")
f.write(pwfLine)
f.close()
def verify_password(stored_password, provided_password):
"""Verify a stored password against one provided by user"""
salt = stored_password[:64]
stored_password = stored_password[64:]
pwdhash = hashlib.pbkdf2_hmac('sha512', provided_password.encode('utf-8'), salt.encode('ascii'), 100000)
pwdhash = binascii.hexlify(pwdhash).decode('ascii')
return pwdhash == stored_password
def find_user(user):
userfound=""
f = open(radioDASHpwf, "r")
flines = f.readlines()
for fl in flines:
fluser = fl.split(":")
if user == fluser[0]:
userfound = fluser[0]
f.close()
return (userfound)
def delete_user(user):
with open(radioDASHpwf, "r") as f:
lines = f.readlines()
with open(radioDASHpwf, "w") as f:
for line in lines:
fluser = line.split(":")
if user != fluser[0]:
f.write(line)
return
def find_password(user):
userpassword = ""
f = open(radioDASHpwf, "r")
flines = f.readlines()
for fl in flines:
fluser = fl.split(":")
if user == fluser[0]:
userpassword = (fluser[1]).rstrip()
f.close()
return (userpassword)
#
# MAIN
#
if ((actionCheck == True) and (actionStore == True)):
sys.exit("Usage error: Choose Store or Check")
if actionCheck == True:
if find_user(user) == "":
sys.exit("User not found")
stored_password = find_password(user)
verdict = verify_password(stored_password, entered_password)
if verdict == True:
print ("True")
else:
print ("False")
if actionStore == True:
provided_hashed = hash_password(entered_password)
save_password(user, provided_hashed)
print("Stored in " + radioDASHpwf)
if actionDelete == True:
if find_user(user) == "":
sys.exit("User not found")
else:
delete_user(user)
print (user + " deleted")