1
- #! coding:utf-8
1
+ # coding:utf-8
2
2
3
3
from hashlib import sha1
4
+ from base64 import b64encode
4
5
import sys
5
6
import hmac
6
7
import re
11
12
12
13
13
14
__author = 'lovedb0y'
14
- __version = '0.1 '
15
+ __version = '0.2 '
15
16
__package = 'pwm'
16
17
17
18
@@ -22,12 +23,15 @@ def __init__(self, key, db_path=None):
22
23
self .passwd_length = 15
23
24
self .db_path = db_path
24
25
self .table = 'pwm'
25
- self ._create_table ()
26
26
27
27
def gen_passwd (self , raw ):
28
28
29
- h = hmac .new (self .key , raw , sha1 )
30
- base64 = h .digest ().encode ("base64" )
29
+ if sys .version_info > (3 , 0 ):
30
+ h = hmac .new (self .key .encode (), raw .encode (), sha1 )
31
+ base64 = b64encode (h .digest ()).decode ()
32
+ else :
33
+ h = hmac .new (self .key , raw , sha1 )
34
+ base64 = h .digest ().encode ("base64" )
31
35
_passwd = base64 [0 : self .passwd_length ]
32
36
return self ._format_passwd (_passwd )
33
37
@@ -55,13 +59,12 @@ def _format_passwd(self, passwd):
55
59
56
60
def _get_conn (self ):
57
61
if self .db_path is None :
58
- print "You didn't set you PWD_DB_PATH ENV"
62
+ print ( "You didn't set you PWD_DB_PATH ENV" )
59
63
sys .exit (1 )
60
64
try :
61
65
conn = sqlite3 .connect (self .db_path )
62
66
except sqlite3 .OperationalError :
63
- print "PWD_DB_PATH: %s is invalid! " \
64
- "(Is it a directory or a file?)" % self .db_path
67
+ print ("PWD_DB_PATH: {} is invalid! (Is it a directory or a file?)" .format (self .db_path ))
65
68
sys .exit (1 )
66
69
return conn
67
70
@@ -129,12 +132,14 @@ def _delete(self, id):
129
132
sql = "delete from {} where id={}" .format (self .table , id )
130
133
with self :
131
134
cur = self .conn .cursor ()
132
- raw_count = cur .execute (sql )
133
- return raw_count
135
+ cur .execute (sql )
136
+ row_count = cur .rowcount
137
+ return row_count
134
138
135
139
def insert (self , domain , account , batch ):
140
+ self ._create_table ()
136
141
self ._insert_account (domain , account , batch or '' )
137
- print "save success"
142
+ print ( "save success" )
138
143
139
144
@staticmethod
140
145
def gen_sign_raw (domain , account , batch ):
@@ -149,35 +154,36 @@ def gen_account_passwd(self, domain, account, batch):
149
154
return self .gen_passwd (raw )
150
155
151
156
def delete (self , id ):
157
+ self ._create_table ()
152
158
row_count = self ._delete (id )
153
- print "remove success, %s record(s) removed" % (row_count )
159
+ print ( "remove success, {} record(s) removed" . format (row_count , ) )
154
160
155
161
def search (self , keyword ):
156
162
163
+ self ._create_table ()
157
164
if keyword == '*' :
158
165
keyword = ''
159
166
160
167
result = self ._query_account (keyword )
161
- fmt = "%-5s|%- 40s|%- 35s|%- 20s|%-5s "
162
- print fmt % ("ID" , "DOMAIN" , "ACCOUNT" , "PASWORD" , "BATCH" )
163
- print fmt % ("-" * 5 , "-" * 40 , "-" * 35 , "-" * 20 , "-" * 5 )
168
+ fmt = "{:5s}|{: 40s}|{: 35s}|{: 20s}|{:5s} "
169
+ print ( fmt . format ("ID" , "DOMAIN" , "ACCOUNT" , "PASWORD" , "BATCH" ) )
170
+ print ( fmt . format ("-" * 5 , "-" * 40 , "-" * 35 , "-" * 20 , "-" * 5 ) )
164
171
for item in result :
165
- print fmt % (item [0 ], item [1 ], item [2 ],
166
- self .gen_account_passwd (item [1 ], item [2 ], item [3 ]),
167
- item [3 ])
172
+ passwd = self .gen_account_passwd (item [1 ], item [2 ], item [3 ])
173
+ print (fmt .format (str (item [0 ]), item [1 ], item [2 ], passwd , item [3 ]))
168
174
169
- print "\n {} records found.\n " .format (len (result ))
175
+ print ( "\n {} records found.\n " .format (len (result ) ))
170
176
171
177
172
178
def main ():
173
179
174
180
db_path = os .getenv ("PWM_DB_PATH" , None )
175
181
if db_path is None :
176
- print "##########WARNING:############"
177
- print "You didn't set you PWD_DB_PATH ENV"
178
- print "echo \" export PWM_DB_PATH=your_path\" >> ~/.bashrc"
179
- print "source ~/.bashrc"
180
- print "###############################"
182
+ print ( "##########WARNING:############" )
183
+ print ( "You didn't set you PWD_DB_PATH ENV" )
184
+ print ( "echo \" export PWM_DB_PATH=your_path\" >> ~/.bashrc" )
185
+ print ( "source ~/.bashrc" )
186
+ print ( "###############################" )
181
187
parse = OptionParser (version = "{} {}" .format (__package , __version ))
182
188
183
189
parse .add_option ('-k' , '--key' , help = "your secret key" , nargs = 0 )
@@ -194,7 +200,7 @@ def main():
194
200
help = "remove your account and domain by id" , nargs = 1 , type = int )
195
201
parse .add_option (
196
202
'-b' , '--batch' ,
197
- help = "add batch to generate diffrent passwd with same domain and account" , # noqa
203
+ help = "add batch to generate different passwd with same domain and account" , # noqa
198
204
nargs = 1 , type = int )
199
205
(options , args ) = parse .parse_args ()
200
206
@@ -220,9 +226,9 @@ def main():
220
226
parse .print_help ()
221
227
return
222
228
223
- print "passwd is :\n {}" .format (
229
+ print ( "passwd is :{}" .format (
224
230
pwm .gen_account_passwd (
225
- options .domain , options .account , options .batch ))
231
+ options .domain , options .account , options .batch )))
226
232
227
233
# 保存
228
234
if options .save is not None :
0 commit comments