I have created an HTTP server in python that executes some cryptographic algorithms. So I try to create vital variables for my cryptography outside the GET method, as I dont want them to change after each request.
Here is a sample of the code:
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
path = self.path
print 'request for ' + path
rootdir = '/Server'
client_mac = path.split('?')[-1]
file_name = path.split('?')[0]
if self.path.split('?')[0].endswith('.txt'):
'''create client ID'''
print 'Printing mac address of connected device ----> %s' % client_mac
id1_secret_key = pre.keyGen(master_secret_key, client_mac)
fr = open (rootdir + file_name)
file_data = fr.read()
sym_key_ciphertext = pre.encrypt(params, client_mac, sym_key);
#more code follows
try:
httpd = HTTPServer(('',8080), Handler)
print 'Server is on and listening to port...7000'
group = PairingGroup('SS512', secparam=1024)
pre = PreGA(group)
(master_secret_key, params) = pre.setup()
print params
sym_key = OpenSSLRand().getRandomBytes(128) #128 bits
sym_cipher = AuthenticatedCryptoAbstraction(sym_key)
server_mac = get_mac()
id2_secret_key = pre.keyGen(master_secret_key, str(server_mac))
httpd.serve_forever()
except KeyboardInterrupt:
print '^C received,shutting down the web server'
server.socket.close()
After the excution of a GET request i get the following Error:
File "server.py", line 157, in do_GET
ciphertext = pre.encrypt(params, client_mac, file_data);
UnboundLocalError: local variable 'params' referenced before assignment
I should mention that variable params has a value after the execution of pre.setup().Any advice would be helpful and deeply appreciated.