Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import gnupg
  3. import json
  4. import logging
  5. import MySQLdb
  6.  
  7. DB_HOST = 'localhost'
  8. DB_PORT = 3306
  9. DB_NAME = 'db'
  10. DB_USER = 'root'
  11. DB_PASS = ''
  12. DB_FIELDS = ['id', 'name']
  13. GPG_HOME = '~/.gnupg'
  14. GPG_FINGERPRINTS = '~/keys.txt'
  15.  
  16.  
  17. def get_db_data():
  18. db = MySQLdb.connect(host=DB_HOST, port=int(DB_PORT), db=DB_NAME, user=DB_USER, passwd=DB_PASS)
  19. cursor = db.cursor(MySQLdb.cursors.DictCursor)
  20.  
  21. fields = ', '.join(map(str, DB_FIELDS))
  22. query = ('SELECT {0} FROM base_satellite'.format(fields))
  23. cursor.execute(query)
  24.  
  25. data = []
  26. for row in cursor:
  27. item = {}
  28. for field in DB_FIELDS:
  29. item[field] = row[field]
  30. data.append(row)
  31. text = json.dumps(data)
  32.  
  33. cursor.close()
  34. db.close()
  35.  
  36. return text
  37.  
  38.  
  39. def encrypt_data(text):
  40. gpg = gnupg.GPG(gnupghome=GPG_HOME)
  41. keys = []
  42. with open(GPG_FINGERPRINTS, 'r') as keysfile:
  43. for key in keysfile:
  44. keys.append(key.rstrip('\n'))
  45. cipher = gpg.encrypt(text, keys)
  46.  
  47. return cipher
  48.  
  49.  
  50. if __name__ == '__main__':
  51. # Setup logger
  52. logging.basicConfig()
  53. log = logging.getLogger('dbenc')
  54. log.setLevel(logging.DEBUG)
  55. log.info('Starting the app')
  56.  
  57. # Get the data from the DB
  58. log.info('Getting the data from the DB')
  59. text = get_db_data()
  60.  
  61. # Encrypt it
  62. log.info('Encrypting...')
  63. cipher = encrypt_data(text)
  64. log.info('Cipher: \n{0}'.format(cipher))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement