Advertisement
beqa1923

Untitled

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