Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import gnupg
- import json
- import logging
- import MySQLdb
- DB_HOST = 'localhost'
- DB_PORT = 3306
- DB_NAME = 'db'
- DB_USER = 'root'
- DB_PASS = ''
- DB_FIELDS = ['id', 'name']
- GPG_HOME = '~/.gnupg'
- GPG_FINGERPRINTS = '~/keys.txt'
- def get_db_data():
- db = MySQLdb.connect(host=DB_HOST, port=int(DB_PORT), db=DB_NAME, user=DB_USER, passwd=DB_PASS)
- cursor = db.cursor(MySQLdb.cursors.DictCursor)
- fields = ', '.join(map(str, DB_FIELDS))
- query = ('SELECT {0} FROM base_satellite'.format(fields))
- cursor.execute(query)
- data = []
- for row in cursor:
- item = {}
- for field in DB_FIELDS:
- item[field] = row[field]
- data.append(row)
- text = json.dumps(data)
- cursor.close()
- db.close()
- return text
- def encrypt_data(text):
- gpg = gnupg.GPG(gnupghome=GPG_HOME)
- keys = []
- with open(GPG_FINGERPRINTS, 'r') as keysfile:
- for key in keysfile:
- keys.append(key.rstrip('\n'))
- cipher = gpg.encrypt(text, keys)
- return cipher
- if __name__ == '__main__':
- # Setup logger
- logging.basicConfig()
- log = logging.getLogger('dbenc')
- log.setLevel(logging.DEBUG)
- log.info('Starting the app')
- # Get the data from the DB
- log.info('Getting the data from the DB')
- text = get_db_data()
- # Encrypt it
- log.info('Encrypting...')
- cipher = encrypt_data(text)
- log.info('Cipher: \n{0}'.format(cipher))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement