def createDatabase(self, count = 100, startFrom=1000): """ Description: creates the database """ # only allow 4 digit PINs if startFrom + count > 9999: startFrom = 1000 if count > 1000: count = 100 # 'privatekeys : where private keys are stored' if not os.path.exists('privatekeys'): os.makedirs('privatekeys') # database is again Database.pkl serverDB = open('Database.pkl','ab') for counter in range(startFrom, startFrom + count): # generates RSAkey pair, length = 1024 bits rsakey = RSAKeyHandling.generateRSAkeypair() # now we can write the public and private keys to different files # for the efficiency in verification and debugging, the values # have been chosen as such. # One must NOT confuse them as real values as they will be entirely different and generated # using a different set of algorithms voterID = 'voteid' + str(counter) PIN = counter # generate privatekey file name for voter keyfilename = 'privatekeys/' + voterID +'.pem' rsakey.save_pem(keyfilename, None, empty_callback) # public key part of RSA in base64 publickey_inbase64 = RSAKeyHandling.save_public_rsakey_to_b64string(rsakey) # sha256 of VoterID || PIN in base64 hash_of_voterID_PIN = RSAKeyHandling.sha256hash_base64( voterID + str(PIN) ) # create dictionary for a particular userID userDict = { hash_of_voterID_PIN : { 'pkey' : publickey_inbase64 , 'voted' : 0 } } # save dictionary; save userID info in the database pickle.dump(userDict, serverDB) serverDB.flush() serverDB.close()
def generate_voters(count=100,startfrom=1000): # only allow 4 digit PINs if startfrom+count > 9999: startfrom = 1000 if count > 100: count = 100 'privatekeys : where private keys are stores' if not os.path.exists('privatekeys'): os.makedirs('privatekeys') # 'publickeys : where the server DB will be saved' # if not os.path.exists('publickeys'): # os.makedirs('publickeys') serverDB = open('Database.pkl','ab') # start generating count RSA keypairs for counter in range(startfrom,startfrom+count): # generates RSAkey pair, length = 1024 bits rsakey = RSAKeyHandling.generateRSAkeypair() # now we can write the public and private keys to different files voterID = 'voters' + str(counter) PIN = counter # generate privatekey file name for voter keyfilename = 'privatekeys/' + voterID +'.pem' rsakey.save_pem(keyfilename, None, empty_callback) # public key part of RSA in base64 publickey_inbase64 = RSAKeyHandling.save_public_rsakey_to_b64string(rsakey) # sha256 of VoterID || PIN in base64 hash_of_voterID_PIN = RSAKeyHandling.sha256hash_base64( voterID + str(PIN) ) # final string to write to DB in a line # public key in base 64 SPACE hash of voterID||PIN SPACE 0 (0 because he has not voted yet) # final_db_record = publickey_inbase64 + ' ' + hash_of_voterID_PIN + ' '+'0\n' # print final_db_record # serverDB.write(final_db_record) userdict = { hash_of_voterID_PIN : { 'pkey' : publickey_inbase64 , 'voted' : 0 } } pickle.dump(userdict, serverDB) serverDB.flush() serverDB.close() return 1