Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / tlslite / scripts / tlsdb.py
blob476d61899f6272a9b2d91691d2db9834502a9a89
1 #!/usr/bin/env python
3 # Authors:
4 # Trevor Perrin
5 # Martin von Loewis - python 3 port
7 # See the LICENSE file for legal information regarding use of this file.
9 from __future__ import print_function
10 import sys
11 import os
12 import socket
13 import math
15 if __name__ != "__main__":
16 raise "This must be run as a command, not used as a module!"
19 from tlslite import *
20 from tlslite import __version__
22 if len(sys.argv) == 1 or (len(sys.argv)==2 and sys.argv[1].lower().endswith("help")):
23 print("")
24 print("Version: %s" % __version__)
25 print("")
26 print("RNG: %s" % prngName)
27 print("")
28 print("Modules:")
29 if m2cryptoLoaded:
30 print(" M2Crypto : Loaded")
31 else:
32 print(" M2Crypto : Not Loaded")
33 if pycryptoLoaded:
34 print(" pycrypto : Loaded")
35 else:
36 print(" pycrypto : Not Loaded")
37 if gmpyLoaded:
38 print(" GMPY : Loaded")
39 else:
40 print(" GMPY : Not Loaded")
41 print("")
42 print("Commands:")
43 print("")
44 print(" createsrp <db>")
45 print("")
46 print(" add <db> <user> <pass> [<bits>]")
47 print(" del <db> <user>")
48 print(" check <db> <user> [<pass>]")
49 print(" list <db>")
50 sys.exit()
52 cmd = sys.argv[1].lower()
54 class Args:
55 def __init__(self, argv):
56 self.argv = argv
57 def get(self, index):
58 if len(self.argv)<=index:
59 raise SyntaxError("Not enough arguments")
60 return self.argv[index]
61 def getLast(self, index):
62 if len(self.argv)>index+1:
63 raise SyntaxError("Too many arguments")
64 return self.get(index)
66 args = Args(sys.argv)
68 def reformatDocString(s):
69 lines = s.splitlines()
70 newLines = []
71 for line in lines:
72 newLines.append(" " + line.strip())
73 return "\n".join(newLines)
75 try:
76 if cmd == "help":
77 command = args.getLast(2).lower()
78 if command == "valid":
79 print("")
80 else:
81 print("Bad command: '%s'" % command)
83 elif cmd == "createsrp":
84 dbName = args.get(2)
86 db = VerifierDB(dbName)
87 db.create()
89 elif cmd == "add":
90 dbName = args.get(2)
91 username = args.get(3)
92 password = args.get(4)
94 db = VerifierDB(dbName)
95 db.open()
96 if username in db:
97 print("User already in database!")
98 sys.exit()
99 bits = int(args.getLast(5))
100 N, g, salt, verifier = VerifierDB.makeVerifier(username, password, bits)
101 db[username] = N, g, salt, verifier
103 elif cmd == "del":
104 dbName = args.get(2)
105 username = args.getLast(3)
106 db = VerifierDB(dbName)
107 db.open()
108 del(db[username])
110 elif cmd == "check":
111 dbName = args.get(2)
112 username = args.get(3)
113 if len(sys.argv)>=5:
114 password = args.getLast(4)
115 else:
116 password = None
118 db = VerifierDB(dbName)
119 db.open()
121 try:
122 db[username]
123 print("Username exists")
125 if password:
126 if db.check(username, password):
127 print("Password is correct")
128 else:
129 print("Password is wrong")
130 except KeyError:
131 print("Username does not exist")
132 sys.exit()
134 elif cmd == "list":
135 dbName = args.get(2)
136 db = VerifierDB(dbName)
137 db.open()
139 print("Verifier Database")
140 def numBits(n):
141 if n==0:
142 return 0
143 return int(math.floor(math.log(n, 2))+1)
144 for username in db.keys():
145 N, g, s, v = db[username]
146 print(numBits(N), username)
147 else:
148 print("Bad command: '%s'" % cmd)
149 except:
150 raise