Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys
- import hashlib
- import random
- import pymysql
- import time
- class keygen(object):
- def __init__(self, ip, usr, password, dbName):
- self.ip = ip
- self.usr = usr
- self.password = password
- self.dbName = dbName
- self.db = pymysql.connect(self.ip,self.usr,self.password,self.dbName)
- #getkey
- def getkey(self, value):
- IsOrNot = None
- key = hashlib.md5(str(random.getrandbits(16)).encode('utf-8')).hexdigest()
- Iv = hashlib.md5(str(random.getrandbits(16)).encode('utf-8')).hexdigest()
- # prepare a cursor object using cursor() method
- cursor = self.db.cursor()
- sql = "select * from KEYTABLE where id = %s" %str(value)
- cursor.execute(sql)
- results = cursor.fetchall()
- if not results:
- """print('Key/IV wasn\'t generated for this Id before')"""
- IsOrNot = False
- else:
- try:
- # Fetch all the rows in a list of lists.
- for row in results:
- KMS_KEY = row[1]
- KMS_IV = row[2]
- # Now print fetched result
- """print ("KEY = %s,IV = %s" % \
- (KMS_KEY, KMS_IV))"""
- except:
- print ("Error: unable to fetch data")
- IsOrNot = True
- if(bool(IsOrNot) == False):
- sql = "INSERT INTO KEYTABLE(ID, \
- KSM_KEY, KSM_IV) \
- VALUES ('%s', '%s', '%s' )" % \
- (value, key, Iv)
- results = (value, key, Iv)
- try:
- # Execute the SQL command
- cursor.execute(sql)
- # Commit your changes in the database
- self.db.commit()
- """print('New key & Iv successfully recorded ')
- print('ID : ' + value + ' Key: ' + key + ' Iv: ' + Iv)"""
- except:
- # Rollback in case there is any error
- self.db.rollback()
- else:
- """print('key was already generated')"""
- return results , IsOrNot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement