mirror of
https://github.com/aljazceru/python-teos.git
synced 2025-12-17 22:24:23 +01:00
Integrates encryption/decryption within the Cryptographer. Close #63
Includes unittests. Also reformats test_inspector to avoid using cli functions
This commit is contained in:
@@ -12,29 +12,3 @@ class Blob:
|
||||
raise ValueError("Non-Hex character found in txid.")
|
||||
|
||||
self.data = data
|
||||
|
||||
def encrypt(self, tx_id):
|
||||
if len(tx_id) != 64:
|
||||
raise ValueError("txid does not matches the expected size (32-byte / 64 hex chars).")
|
||||
|
||||
elif re.search(r"^[0-9A-Fa-f]+$", tx_id) is None:
|
||||
raise ValueError("Non-Hex character found in txid.")
|
||||
|
||||
# Transaction to be encrypted
|
||||
# FIXME: The blob data should contain more things that just the transaction. Leaving like this for now.
|
||||
tx = unhexlify(self.data)
|
||||
|
||||
# sk is the H(txid) (32-byte) and nonce is set to 0 (12-byte)
|
||||
sk = sha256(unhexlify(tx_id)).digest()
|
||||
nonce = bytearray(12)
|
||||
|
||||
# Encrypt the data
|
||||
cipher = ChaCha20Poly1305(sk)
|
||||
encrypted_blob = cipher.encrypt(nonce=nonce, data=tx, associated_data=None)
|
||||
encrypted_blob = hexlify(encrypted_blob).decode()
|
||||
|
||||
logger.info(
|
||||
"Creating new blob", sk=hexlify(sk).decode(), nonce=hexlify(nonce).decode(), encrypted_blob=encrypted_blob
|
||||
)
|
||||
|
||||
return encrypted_blob
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import re
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
@@ -29,6 +28,8 @@ from apps.cli import (
|
||||
)
|
||||
|
||||
from common.constants import LOCATOR_LEN_HEX
|
||||
from common.cryptographer import Cryptographer
|
||||
from common.tools import check_sha256_hex_format
|
||||
|
||||
|
||||
HTTP_OK = 200
|
||||
@@ -162,7 +163,7 @@ def add_appointment(args):
|
||||
logger.error("The provided JSON is empty.")
|
||||
return False
|
||||
|
||||
valid_locator = check_txid_format(appointment_data.get("tx_id"))
|
||||
valid_locator = check_sha256_hex_format(appointment_data.get("tx_id"))
|
||||
|
||||
if not valid_locator:
|
||||
logger.error("The provided locator is not valid.")
|
||||
@@ -288,7 +289,7 @@ def get_appointment(args):
|
||||
sys.exit(help_get_appointment())
|
||||
else:
|
||||
locator = arg_opt
|
||||
valid_locator = check_txid_format(locator)
|
||||
valid_locator = check_sha256_hex_format(locator)
|
||||
|
||||
if not valid_locator:
|
||||
logger.error("The provided locator is not valid: {}".format(locator))
|
||||
@@ -317,7 +318,7 @@ def build_appointment(tx, tx_id, start_time, end_time, dispute_delta):
|
||||
|
||||
# FIXME: The blob data should contain more things that just the transaction. Leaving like this for now.
|
||||
blob = Blob(tx)
|
||||
encrypted_blob = blob.encrypt(tx_id)
|
||||
encrypted_blob = Cryptographer.encrypt(blob, tx_id)
|
||||
|
||||
appointment = {
|
||||
"locator": locator,
|
||||
@@ -330,14 +331,6 @@ def build_appointment(tx, tx_id, start_time, end_time, dispute_delta):
|
||||
return appointment
|
||||
|
||||
|
||||
def check_txid_format(txid):
|
||||
if len(txid) != 64:
|
||||
sys.exit("locator does not matches the expected size (32-byte / 64 hex chars).")
|
||||
|
||||
# TODO: #12-check-txid-regexp
|
||||
return re.search(r"^[0-9A-Fa-f]+$", txid) is not None
|
||||
|
||||
|
||||
def show_usage():
|
||||
return (
|
||||
"USAGE: "
|
||||
|
||||
Reference in New Issue
Block a user