summarylogtreecommitdiffstats
path: root/pswd-py3.diff
blob: 5fec7b2eb077bdb03480cf8867c5d2baa08755ba (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
--- pswd.bak	2014-12-05 03:17:30.000000000 +0200
+++ pswd	2024-04-22 12:27:24.795769267 +0300
@@ -130,14 +130,14 @@
 
   # Hash token K1 times using SHA256
   if QUIET == False:
-    print "Generating User Token... (This is only done once)"
+    print("Generating User Token... (This is only done once)")
   for i in range(0, K1):
     if QUIET == False:
       if i % 1000 == 0:
         per = float(i) / float(K1)
         pb(per)
 
-    token = hashlib.sha256(token).hexdigest()
+    token = hashlib.sha256(token.encode("ascii")).hexdigest()
 
   # Save token to cache
   f = open(CACHE_FILE,'a')
@@ -145,7 +145,7 @@
   f.close()
 
   if QUIET == False:
-    print "";
+    print("");
 
 
   # Return Final Step 1 Hash
@@ -183,33 +183,31 @@
   key = DOMAIN + PASSWORD + USER_TOKEN
 
   # Hash the pswd K2-1 times using SHA256
-  pswd = key
+  pswd = key.encode("ascii")
   for i in range(1, K2):
-    pswd = hashlib.sha256(pswd).hexdigest()
+    pswd = hashlib.sha256(pswd).hexdigest().encode("ascii")
 
   # for the last hash: concatenate 2 hashes of pswd to lengthen the result
-  pswd = hashlib.sha256(pswd + "1").hexdigest() + hashlib.sha256(pswd + "2").hexdigest()
-
+  pswd = hashlib.sha256(pswd + b"1").hexdigest() + hashlib.sha256(pswd + b"2").hexdigest()
+  pswd = pswd.encode("ascii")
 
   # convert to Base64
-  pswd = base64.b64encode(pswd.decode("hex"))
+  pswd = base64.b64encode(pswd).decode("ascii")
 
   # remove base64 characters: + / =
   pswd = pswd.replace("+", "")
   pswd = pswd.replace("/", "")
   pswd = pswd.replace("=", "")
 
-
   # Trim the password to LENGTH
   pswd = pswd[:LENGTH]
 
-
-
   # generate nums string from the key
-  temp = hashlib.sha256(key + "numbers1").hexdigest() + \
-    hashlib.sha256(key + "numbers2").hexdigest() + \
-    hashlib.sha256(key + "numbers3").hexdigest() + \
-    hashlib.sha256(key + "numbers4").hexdigest()
+  key = key.encode("ascii")
+  temp = hashlib.sha256(key + b"numbers1").hexdigest() + \
+    hashlib.sha256(key + b"numbers2").hexdigest() + \
+    hashlib.sha256(key + b"numbers3").hexdigest() + \
+    hashlib.sha256(key + b"numbers4").hexdigest()
 
 
   # pull out all digits from the hashed string
@@ -236,7 +234,7 @@
     num_index = num_index + 1   # increase num index
     if (div <= 3):              # <= 3 creates too many symbols
       div = 4                   # divide by at least 4
-    num_of_symbols = LENGTH / div
+    num_of_symbols = LENGTH // div
 
     # loop to add each symbol
     for i in range(0, num_of_symbols):
@@ -431,9 +429,9 @@
   # define command line options
   try:
     opts, args = getopt.getopt(argv, "u:p:d:n:csa:t:x:y:qh", ["user=", "password=", "domain=", "token=", "cache=", "gettoken", "help"])
-  except getopt.GetoptError, err:
-    print "ERROR: " + str(err)
-    print USAGE
+  except getopt.GetoptError as err:
+    print("ERROR: " + str(err))
+    print(USAGE)
     sys.exit(2)
 
 
@@ -449,7 +447,7 @@
       try:
         LENGTH = int(arg)
       except ValueError:
-        print "ERROR: length must be an integer"
+        print("ERROR: length must be an integer")
         sys.exit(2)
     elif opt in ("-c"):
       UPPERCASE = not UPPERCASE
@@ -461,13 +459,13 @@
       try:
         K1 = int(arg)
       except ValueError:
-        print "ERROR: -x must be an integer"
+        print("ERROR: -x must be an integer")
         sys.exit(2)
     elif opt in ("-y"):
       try:
         K2 = int(arg)
       except ValueError:
-        print "ERROR: -y must be an integer"
+        print("ERROR: -y must be an integer")
         sys.exit(2)
     elif opt in ("-q"):
       QUIET = True
@@ -478,7 +476,7 @@
     elif opt in ("--token", "-t"):
       USER_TOKEN = arg
     elif opt in ("--help", "-h"):
-      print USAGE
+      print(USAGE)
       sys.exit(0)
 
 
@@ -487,7 +485,7 @@
 
 
   # Get User ID based on hash of USER
-  USER_ID = hashlib.sha256(USER).hexdigest()
+  USER_ID = hashlib.sha256(USER.encode("ascii")).hexdigest()
 
 
 
@@ -500,16 +498,16 @@
     # Parameter Checks
     # Need USER and PASSWORD
     if USER == "":
-      print "ERROR: user name must be provided with --user [user name]"
+      print("ERROR: user name must be provided with --user [user name]")
       sys.exit(2)
     elif PASSWORD == "":
-      print "ERROR: master password must be provided with --password [master password]"
+      print("ERROR: master password must be provided with --password [master password]")
       sys.exit(2)
 
     # Get or Generate a USER TOKEN
     USER_TOKEN = getToken()
 
-    print USER_TOKEN
+    print(USER_TOKEN)
 
     sys.exit(0)
 
@@ -524,22 +522,22 @@
 
   # Make sure USER and PASSWORD and DOMAIN are supplied
   if USER == "":
-    print "ERROR: user name must be provided with --user [user name]"
+    print("ERROR: user name must be provided with --user [user name]")
     sys.exit(2)
   elif PASSWORD == "":
-    print "ERROR: master password must be provided with --password [master password]"
+    print("ERROR: master password must be provided with --password [master password]")
     sys.exit(2)
   elif DOMAIN == "":
-    print "ERROR: the domain must be provided with --domain [domain]"
+    print("ERROR: the domain must be provided with --domain [domain]")
     sys.exit(2)
 
 
   # Check LENGTH is within range
   if LENGTH < MIN_LENGTH:
-    print "ERROR: the length cannot be less than " + str(MIN_LENGTH)
+    print("ERROR: the length cannot be less than " + str(MIN_LENGTH))
     sys.exit(2)
   elif LENGTH > MAX_LENGTH:
-    print "ERROR: the length cannot be greater than " + str(MAX_LENGTH)
+    print("ERROR: the length cannot be greater than " + str(MAX_LENGTH))
     sys.exit(2)
 
 
@@ -571,7 +569,7 @@
 
 
   if QUIET == False:
-    print FINAL
+    print(FINAL)
 
   # Attempt to copy the password to the user's clipboard
   copy(FINAL)