Fix file retrieve

Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2024-12-18 14:41:51 +00:00
parent c5744de19a
commit 97c1236f44
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
6 changed files with 21 additions and 21 deletions

View File

@ -34,16 +34,8 @@ def decryptFile(args):
logger.error("File '" + args.encrypted + "' not found.")
sys.exit(1)
# if (not os.path.isfile(BASE_DIR + args.metadata)):
# logger.error("File '" + args.metadata + "' not found.")
# sys.exit(1)
#Decrypt file
# print(args.metadata)
metadata = json.loads(args.metadata)
print(BASE_DIR + args.encrypted)
content = symmetric_encryption.decrypt_file(bytes.fromhex(metadata['key']), BASE_DIR + args.encrypted)
with open(BASE_DIR + 'decryped_file.txt', 'w') as f:

View File

@ -69,13 +69,17 @@ def getDoc(args):
logger.error("Failed to obtain response from server.")
sys.exit(-1)
file = file.json()
file = file.content
if not digest.get_hash(file) == metadata['file_handle']:
logger.error("Files integrity is lost.")
sys.exit(-1)
content = symmetric_encryption.decrypt_file(file)
with open(BASE_DIR + 'encrypted_file', 'wb') as f:
f.write(file)
content = symmetric_encryption.decrypt_file(bytes.fromhex(metadata['key']), BASE_DIR + 'encrypted_file')
os.remove(BASE_DIR + 'encrypted_file')
if args.output:
with open(BASE_DIR + args.output, 'w') as f:

View File

@ -37,12 +37,7 @@ def getFile(args):
if not args.filehandle:
logger.error("Need a file handle.")
sys.exit(1)
#else:
# if not os.path.isfile(BASE_DIR + args.filehandle):
# logger.error("File '" + args.filehandle + "' not found" )
# sys.exit(1)
#Get file
try:
file = requests.get(f'http://{state['REP_ADDRESS']}/file/get/' + args.filehandle + '/content')
file.raise_for_status()
@ -50,8 +45,6 @@ def getFile(args):
logger.error("Failed to obtain response from server.")
sys.exit(-1)
print(file)
if not args.file:
sys.stdout.write(file.text)
else:

View File

@ -112,19 +112,23 @@ def test_rep_get_file():
def test_decrypt_file():
# Test the rep_decrypt_file command
process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_decrypt_file file.txt '{json.dumps(metadata)}'", shell=True)
encryption_metadata = {
'alg': metadata['alg'],
'key': metadata['key']
}
process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_decrypt_file file.txt '{json.dumps(encryption_metadata)}'", shell=True)
process.wait()
assert process.returncode == 0
def test_rep_get_doc_file():
# Test the rep_get_doc_file
process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_get_doc_file session.json doc ", shell=True)
process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_get_doc_file session.json doc file.txt", shell=True)
process.wait()
assert process.returncode == 0
def test_rep_delete_doc():
# Test the rep_get_doc_file
process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_delete_doc session.json doc ", shell=True)
process = subprocess.Popen(f"{DELIVERY_PATH}/client/bin/rep_delete_doc session.json doc", shell=True)
process.wait()
assert process.returncode == 0

View File

@ -0,0 +1,4 @@
from .asymmetric_functs import *
from .symmetric_encryption import *
from .digest import *
from .key_pair import *

View File

@ -62,5 +62,8 @@ def decrypt_file(key, input_file, output_file=None) -> str:
# Finalize decryption
plaintext_content += decryptor.finalize()
return plaintext_content.decode('utf-8', errors='ignore')
try:
return plaintext_content.decode()
except UnicodeDecodeError:
return plaintext_content