Fix file retrieve
Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
c5744de19a
commit
97c1236f44
|
@ -34,16 +34,8 @@ def decryptFile(args):
|
||||||
logger.error("File '" + args.encrypted + "' not found.")
|
logger.error("File '" + args.encrypted + "' not found.")
|
||||||
sys.exit(1)
|
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)
|
metadata = json.loads(args.metadata)
|
||||||
|
|
||||||
print(BASE_DIR + args.encrypted)
|
|
||||||
|
|
||||||
content = symmetric_encryption.decrypt_file(bytes.fromhex(metadata['key']), 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:
|
with open(BASE_DIR + 'decryped_file.txt', 'w') as f:
|
||||||
|
|
|
@ -69,13 +69,17 @@ def getDoc(args):
|
||||||
logger.error("Failed to obtain response from server.")
|
logger.error("Failed to obtain response from server.")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
file = file.json()
|
file = file.content
|
||||||
|
|
||||||
if not digest.get_hash(file) == metadata['file_handle']:
|
if not digest.get_hash(file) == metadata['file_handle']:
|
||||||
logger.error("Files integrity is lost.")
|
logger.error("Files integrity is lost.")
|
||||||
sys.exit(-1)
|
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:
|
if args.output:
|
||||||
with open(BASE_DIR + args.output, 'w') as f:
|
with open(BASE_DIR + args.output, 'w') as f:
|
||||||
|
|
|
@ -37,12 +37,7 @@ def getFile(args):
|
||||||
if not args.filehandle:
|
if not args.filehandle:
|
||||||
logger.error("Need a file handle.")
|
logger.error("Need a file handle.")
|
||||||
sys.exit(1)
|
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:
|
try:
|
||||||
file = requests.get(f'http://{state['REP_ADDRESS']}/file/get/' + args.filehandle + '/content')
|
file = requests.get(f'http://{state['REP_ADDRESS']}/file/get/' + args.filehandle + '/content')
|
||||||
file.raise_for_status()
|
file.raise_for_status()
|
||||||
|
@ -50,8 +45,6 @@ def getFile(args):
|
||||||
logger.error("Failed to obtain response from server.")
|
logger.error("Failed to obtain response from server.")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
print(file)
|
|
||||||
|
|
||||||
if not args.file:
|
if not args.file:
|
||||||
sys.stdout.write(file.text)
|
sys.stdout.write(file.text)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -112,13 +112,17 @@ def test_rep_get_file():
|
||||||
|
|
||||||
def test_decrypt_file():
|
def test_decrypt_file():
|
||||||
# Test the rep_decrypt_file command
|
# 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()
|
process.wait()
|
||||||
assert process.returncode == 0
|
assert process.returncode == 0
|
||||||
|
|
||||||
def test_rep_get_doc_file():
|
def test_rep_get_doc_file():
|
||||||
# Test the 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()
|
process.wait()
|
||||||
assert process.returncode == 0
|
assert process.returncode == 0
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
from .asymmetric_functs import *
|
||||||
|
from .symmetric_encryption import *
|
||||||
|
from .digest import *
|
||||||
|
from .key_pair import *
|
|
@ -62,5 +62,8 @@ def decrypt_file(key, input_file, output_file=None) -> str:
|
||||||
# Finalize decryption
|
# Finalize decryption
|
||||||
plaintext_content += decryptor.finalize()
|
plaintext_content += decryptor.finalize()
|
||||||
|
|
||||||
return plaintext_content.decode('utf-8', errors='ignore')
|
try:
|
||||||
|
return plaintext_content.decode()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
return plaintext_content
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue