Commit | Line | Data |
---|---|---|
ea4e6725 FT |
1 | #!/usr/bin/python |
2 | ||
3 | import sys, hmac, hashlib, getopt | |
4 | ||
5 | def usage(out): | |
6 | out.write("usage: vhtpasswd [-h] FILE\n") | |
7 | ||
8 | opts, args = getopt.getopt(sys.argv[1:], "h") | |
9 | for o, a in opts: | |
10 | if o == "-h": | |
11 | usage(sys.stdout) | |
12 | sys.exit(0) | |
13 | if len(args) < 1: | |
14 | usage(sys.stderr) | |
15 | sys.exit(1) | |
16 | ||
17 | def hashpw(usr, pw): | |
18 | dig = hmac.new(pw, digestmod=hashlib.sha1) | |
19 | dig.update(usr) | |
20 | return dig.hexdigest() | |
21 | ||
22 | def findpw(fn, name): | |
23 | with open(fn) as fp: | |
24 | for line in fp: | |
25 | usr, pw = line.strip().split(':') | |
26 | if usr == name: | |
27 | return pw | |
28 | return None | |
29 | ||
30 | usr = sys.stdin.readline().strip() | |
31 | gpw = sys.stdin.readline().strip() | |
32 | if findpw(args[0], usr) == hashpw(usr, gpw): | |
33 | sys.exit(0) | |
34 | sys.exit(1) |