def send_transaction(self, message): logging.info(f'sending transaction with message {message}') encrypted_message = Crypt.encrypt(self.public_key, message) signed_data = Crypt.sign(self.private_key, encrypted_message) # need to encode the data and public key as string to send through HTTP # first pickle the data, then encode as base64 pickled_signed_data = Client.pickle_data(signed_data) pickled_public_key = Client.pickle_data(self.public_key) data = { 'encrypted_message': encrypted_message.decode(), 'signed_data': pickled_signed_data, 'public_key': pickled_public_key } response = requests.post(f'{self.server}/record', json=json.dumps(data)) logging.info(f'response from server {response}') return response
def verify_broadcast(block): header = block['header'] previous_hash = header['previousHash'] transaction = block['transaction'] tid = transaction['id'] if not previous_hash == get_previous_hash(): return False if not tid == Crypt.sha256(json.dumps(header)): return False return True
def __init__(self, server, public_key=None, private_key=None): try: self.public_key = Crypt.get_key(public_key) self.private_key = Crypt.get_key(private_key) except: Crypt.create_keys() self.public_key = Crypt.get_key(public_key) self.private_key = Crypt.get_key(private_key) self.server = server
def get_transaction(self, tid): logging.info(f'getting transaction {tid}') response = requests.get(f'{self.server}/record', params={'tid': tid}) logging.info(f'response from server {response}') try: r = response.json() s = json.loads(r['response']) data = s['transaction']['data'] except: logging.error('could not decrypt server response') try: message = Crypt.decrypt(self.private_key, data) return message except: logging.error('unauthorized')
def get_user_transactions(self, user=None): if user is None: user = Client.pickle_data(self.public_key) logging.info(f'getting transactions for user {user}') response = requests.get(f'{self.server}/record/user', params={'user': user}) logging.info(f'response from server {response}') r = response.json() s = r['response'] messages = [] for record in s: record_dict = json.loads(record) data = record_dict['transaction']['data'] id = record_dict['transaction']['id'] message = Crypt.decrypt(self.private_key, data) message = f'{message}\n{id}' messages.append(message) return messages
"--encrypt", action="store_true", help="Whether to encrypt the file, only -e or -d can be specified.") parser.add_argument( "-d", "--decrypt", action="store_true", help="Whether to decrypt the file, only -e or -d can be specified.") args = parser.parse_args() keyfilepath = args.keyfile file = args.file generate_key = args.generate_key keyfilename = "key.key" keyfile = os.path.join(keyfilepath, keyfilename) c = Crypt() try: if keyfilepath == "": raise Exception("Missing key file path argument.") elif not os.path.exists(keyfile) and not generate_key: raise Exception(f"File Not Found: {keyfile}") if file == "": raise Exception("Missing file argument.") elif not os.path.exists(file): raise Exception(f"File Not Found: {file}") if generate_key: if not os.path.exists(keyfile): print(f"Writing key file: {keyfile}") c.write_key(keyfile) else:
async def add(request): logging.info(f'handling add request {request}') if request.body_exists: try: request_body = await request.json() except: logging.error(f'could not parse {request}') error = {'message': 'error parsing json body'} return web.json_response(error, status=web.HTTPBadRequest.status_code) request_body = json.loads(request_body) encrypted_message = request_body['encrypted_message'] pickled_signed_data = request_body['signed_data'] pickled_public_key = request_body['public_key'] signed_data = unpickle_data(pickled_signed_data) public_key = unpickle_data(pickled_public_key) verified = Crypt.verify(public_key, signed_data, encrypted_message.encode()) timestamp = str(datetime.utcnow()) # full message consists of a header, with hash and time, and body, with user and data if verified: try: logging.info(f'writing message {encrypted_message} to db') merkle_hash = Crypt.sha256(encrypted_message) header = { "previousHash": get_previous_hash(), "merkleHash": merkle_hash, "user": pickled_public_key, "time": timestamp } tid = Crypt.sha256(json.dumps(header)) transaction = { "data": encrypted_message, "id": tid } block = { "header": header, "transaction": transaction } block_string = json.dumps(block) db.write(block_string) send_broadcast(block_string) response = {'message': 'ok', 'tid': tid} return web.json_response(response) except: logging.info(f'error writing message {encrypted_message} to db') response = {'message': 'error writing message to database'} return web.json_response(response, status=web.HTTPError.status_code) else: response = {'message': 'error verifying data'} return web.json_response(response, status=web.HTTPBadRequest.status_code) else: logging.error(f'no body in request {request}') error= {'message': 'body required'} return web.json_response(error, status=web.HTTPBadRequest.status_code)