Advertisement
beqa1923

keygen

Aug 12th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import sys
  3. import hashlib
  4. import random
  5. import pymysql
  6. import time
  7.  
  8.  
  9. class keygen(object):
  10.  
  11. def __init__(self, ip, usr, password, dbName):
  12. self.ip = ip
  13. self.usr = usr
  14. self.password = password
  15. self.dbName = dbName
  16. self.db = pymysql.connect(self.ip,self.usr,self.password,self.dbName)
  17.  
  18. #getkey
  19. def getkey(self, value):
  20.  
  21. IsOrNot = None
  22. key = hashlib.md5(str(random.getrandbits(16)).encode('utf-8')).hexdigest()
  23. Iv = hashlib.md5(str(random.getrandbits(16)).encode('utf-8')).hexdigest()
  24.  
  25. # prepare a cursor object using cursor() method
  26. cursor = self.db.cursor()
  27. sql = "select * from KEYTABLE where id = %s" %str(value)
  28.  
  29. cursor.execute(sql)
  30. results = cursor.fetchall()
  31. if not results:
  32. """print('Key/IV wasn\'t generated for this Id before')"""
  33. IsOrNot = False
  34. else:
  35. try:
  36. # Fetch all the rows in a list of lists.
  37. for row in results:
  38. KMS_KEY = row[1]
  39. KMS_IV = row[2]
  40. # Now print fetched result
  41. """print ("KEY = %s,IV = %s" % \
  42. (KMS_KEY, KMS_IV))"""
  43. except:
  44. print ("Error: unable to fetch data")
  45. IsOrNot = True
  46.  
  47. if(bool(IsOrNot) == False):
  48.  
  49. sql = "INSERT INTO KEYTABLE(ID, \
  50. KSM_KEY, KSM_IV) \
  51. VALUES ('%s', '%s', '%s' )" % \
  52. (value, key, Iv)
  53. results = (value, key, Iv)
  54. try:
  55. # Execute the SQL command
  56. cursor.execute(sql)
  57. # Commit your changes in the database
  58. self.db.commit()
  59. """print('New key & Iv successfully recorded ')
  60. print('ID : ' + value + ' Key: ' + key + ' Iv: ' + Iv)"""
  61. except:
  62. # Rollback in case there is any error
  63. self.db.rollback()
  64. else:
  65. """print('key was already generated')"""
  66.  
  67. return results , IsOrNot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement