-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (99 loc) · 4.85 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pydicom
import os
import argparse
from anonymization import anonymise
from hashing import hash_dicom
from encryption import encrypt_dicom
from send_to_server import send_dicom_to_server
from watermark import watermark_image
from server_address import SERVER_IP, SERVER_PORT
from write_np_to_file import write_nparr_to_file
def driver(filename, anonymization, server, port, sha2, timing):
print(f'server={server}, port={port}')
# st = time.time()
ds_diff_folder = os.path.join(os.getcwd(), 'ds_diffs')
ds = pydicom.dcmread(filename)
print("Read DICOM file...")
#####################################################################
write_nparr_to_file(ds.pixel_array, 'after_reading.txt') #
fp1 = open(os.path.join(ds_diff_folder,"initial.txt"), mode="w") #
print(ds, file=fp1) #
fp1.close() #
#####################################################################
if anonymization == True:
ds = anonymise(ds)
# print("Anonymized DICOM file...")
# print(ds)
#####################################################################
write_nparr_to_file(ds.pixel_array, 'after_anonymization.txt') #
fp1 = open(os.path.join(ds_diff_folder,"anonymized.txt"), mode="w") #
print(ds, file=fp1) #
fp1.close() #
#####################################################################
hash = hash_dicom(ds, sha2)
print("Hash in bytes: ", hash)
#####################################################################
fp2 = open(os.path.join(ds_diff_folder,"hashed.txt"), mode="w") #
print(ds, file=fp2) #
write_nparr_to_file(ds.pixel_array, 'after_hashing.txt') #
fp2.close() #
#####################################################################
image_array = ds.pixel_array
#function that takes pixel array, and hash as arg and returns watermarked pixel array, and dict containing old values
ds.PixelData, old_values = watermark_image(image_array, hash, timing)
#Watermarking complete
#####################################################################
write_nparr_to_file(ds.pixel_array, 'after_watermarking.txt') #
fp3 = open(os.path.join(ds_diff_folder,"watermarked.txt"), mode="w")#
print(ds, file=fp3) #
fp3.close() #
#####################################################################
peer_public_key, ciphertext, tag = encrypt_dicom(ds, old_values, server, port)
print("Encryption complete! Sending to server...")
#send this ciphertext and tag to server
response = send_dicom_to_server(ciphertext, tag, peer_public_key, server, port)
#print(ds)
if(response.status_code == 200):
print("Successfully sent file to server!")
else:
print('Failed! HTTP', response.status_code)
# Not timing main program because it depends on
# print('Main program execution completed in ', time.time()-st)
if __name__ == "__main__":
# Argparse parser for arguments
#what all do we want: multiple files, anonymization toggle, server IP address,port
parser = argparse.ArgumentParser(description='Transfer DICOM files securely')
parser.add_argument('--anonymize', '--anonymization', '-a',
action='store_true',
help='Enable Anonymization')
parser.add_argument('--ip', '-i', type=str,
help='Server IP Address, default=localhost')
parser.add_argument('--port', '-p', type=str,
help='Server Port, default=5000')
parser.add_argument('--sha2', '-s',
action='store_true',
help="Force use of SHA2_256 as hashing algorithm")
parser.add_argument('--timing',
action='store_true',
help="Disable display of plots for timing the modules")
parser.add_argument('filename',
nargs='+',
help='Enter the name of file/files separated by space')
args = parser.parse_args()
filenames = args.filename
if args.ip:
print(f'Server IP: {args.ip}')
server_ip = args.ip
else:
print('Server IP: localhost')
server_ip = SERVER_IP
if args.port:
print(f'Port: {args.port}')
server_port = args.port
else:
print('Port: 5000')
server_port = SERVER_PORT
if args.sha2:
print("Please make sure to change server config to use SHA2 instead")
for x in filenames:
driver(filename = x, anonymization = args.anonymize, server = server_ip, port = server_port, sha2 = args.sha2, timing = args.timing)