-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecksum.py
More file actions
343 lines (284 loc) · 8.92 KB
/
checksum.py
File metadata and controls
343 lines (284 loc) · 8.92 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
def checksumdecimal4bitsen(inp):
i = 0
sum = 0
while i < len(inp):
num = inp[i]
sum = sum + num
i += 1
print("Sum in decimal is : ", sum)
sum_to_bin = bin(sum).replace("0b", "").zfill(8)
print("Coversion of the sum to binary : ", sum_to_bin)
list_sum_to_bin = list(sum_to_bin)
l1 = sum_to_bin[0:4]
l2 = sum_to_bin[3:]
sl1 = "0b" + str(l1)
sl2 = "0b" + str(l2)
print(sl1," ",sl2)
add_bin = int(sl1, 2) + int(sl2, 2)
wrappedsum = bin(add_bin)
print("So the wrappedsum in binary is where 0b represents that its binary: ", wrappedsum)
print("So the wrappedsum in decimal is: ", int(wrappedsum, 2))
check = list((wrappedsum.replace('0b', '')).zfill(4))
checksum = []
for i in check:
if i == '0':
checksum.append("1")
else:
checksum.append("0")
checks = ''.join(checksum)
checksums = '0b' + checks
print("So the value of the checksum in binary is: ", checksums)
print("So the value of the checksum is ", int(checksums, 2))
def checksumdecimal4bitrec(inp):
i = 0
sum = 0
while i < len(inp):
num = inp[i]
sum = sum + num
i += 1
print("Sum in decimal is : ", sum)
sum_to_bin = bin(sum).replace("0b", "").zfill(8)
print("Coversion of the sum to binary : ", sum_to_bin)
list_sum_to_bin = list(sum_to_bin)
l1 = sum_to_bin[0:4]
l2 = sum_to_bin[3:]
sl1 = "0b" + str(l1)
sl2 = "0b" + str(l2)
print(sl1," ",sl2)
add_bin = int(sl1, 2) + int(sl2, 2)
wrappedsum = bin(add_bin)
print("So the wrappedsum in binary is where 0b represents that its binary: ", wrappedsum)
print("So the wrappedsum in decimal is: ", int(wrappedsum, 2))
check = list((wrappedsum.replace('0b', '')).zfill(4))
checksum = []
for i in check:
if i == '0':
checksum.append("1")
else:
checksum.append("0")
checks = ''.join(checksum)
checksums = '0b' + checks
print("So the value of the checksum in binary is: ", checksums)
print("So the value of the checksum is in decimal : ", int(checksums, 2))
if int(checksums, 2) == 0000:
print("Correct Data received")
print("Data received is: ", data)
else:
print("Incorrect data received")
def checksumdecimal8bitsen(inp):
i = 0
sum = 0
while i < len(inp):
num = inp[i]
sum = sum + num
i += 1
print("Sum in decimal is : ", sum)
sum_to_bin = bin(sum).replace("0b", "").zfill(16)
print("Coversion of the sum to binary : ", sum_to_bin)
list_sum_to_bin = list(sum_to_bin)
l1 = sum_to_bin[0:8]
l2 = sum_to_bin[7:]
sl1 = "0b" + str(l1)
sl2 = "0b" + str(l2)
print(sl1," ",sl2)
add_bin = int(sl1, 2) + int(sl2, 2)
wrappedsum = bin(add_bin)
print("So the wrappedsum in binary is where 0b represents that its binary: ", wrappedsum)
print("So the wrappedsum in decimal is: ", int(wrappedsum, 2))
check = list((wrappedsum.replace('0b', '')).zfill(8))
checksum = []
for i in check:
if i == '0':
checksum.append("1")
else:
checksum.append("0")
checks = ''.join(checksum)
checksums = '0b' + checks
print("So the value of the checksum in binary is: ", checksums)
print("So the value of the checksum is ", int(checksums, 2))
def checksumdecimal8bitrec(inp):
i = 0
sum = 0
while i < len(inp):
num = inp[i]
sum = sum + num
i += 1
print("Sum in decimal is : ", sum)
sum_to_bin = bin(sum).replace("0b", "").zfill(16)
print("Coversion of the sum to binary : ", sum_to_bin)
list_sum_to_bin = list(sum_to_bin)
l1 = sum_to_bin[0:8]
l2 = sum_to_bin[7:]
sl1 = "0b" + str(l1)
sl2 = "0b" + str(l2)
print(sl1," ",sl2)
add_bin = int(sl1, 2) + int(sl2, 2)
wrappedsum = bin(add_bin)
print("So the wrappedsum in binary is where 0b represents that its binary: ", wrappedsum)
print("So the wrappedsum in decimal is: ", int(wrappedsum, 2))
check = list((wrappedsum.replace('0b', '')).zfill(8))
checksum = []
for i in check:
if i == '0':
checksum.append("1")
else:
checksum.append("0")
checks = ''.join(checksum)
checksums = '0b' + checks
print("So the value of the checksum in binary is: ", checksums)
print("So the value of the checksum is in decimal : ", int(checksums, 2))
if int(checksums, 2) == 0000:
print("Correct Data received")
print("Data received is: ", data)
else:
print("Incorrect data received")
def checksum2sen(sender):
lst = []
total = 0
for c in sender:
lst.append(ord(c))
print(lst)
hexlst = []
for s in lst:
hexlst.append(hex(s))
print(hexlst)
hexl=[]
for a in hexlst:
hexl.append(a.split('x')[1])
print(hexl)
newl = []
i = 0
while i < len(hexl):
newl.append(hexl[i] + hexl[i + 1])
i = i + 2
print("compoments to be added of headecimal: ", newl)
i = 0
while i < len(newl):
total += int(newl[i], 16)
i = i + 1
print("Total sum :", hex(total))
s = hex(total).split('x')[1]
s1 = s[1:]
s2 = s[0]
wrapsum = int(s1, 16) + int(s2, 16)
w = hex(wrapsum)
print("Wrapsum is: ", hex(wrapsum))
wsum = w.split('x')[1]
print(wsum)
csum = bin(int(wsum, 16))
print(csum)
csum_l = list(csum)
j = 0
checkl = []
for j in csum:
if j == '1':
checkl.append('0')
else:
checkl.append('1')
checks = ''.join(checkl)
print("So the checksum is ", hex(int(checks[2:], 2)))
def checksum2rec(inp):
m = 0
sum=0
print("Hexadecimal list is ", inp)
while m < len(inp):
sum += int(inp[m], 16)
m = m + 1
print("The total sum is: ", hex(sum))
wr = hex(sum).split('x')[1]
wr1 = wr[1:]
wr2 = wr[0]
r_wrapsum = int(wr1, 16) + int(wr2, 16)
r_w = hex(r_wrapsum)
print("Wrapsum is: ", hex(r_wrapsum))
r_wsum = r_w.split('x')[1]
print(r_wsum)
r_csum = bin(int(r_wsum, 16))
print(r_csum)
r_csum_l = list(r_csum)
n = 0
r_checkl = []
for n in r_csum:
if n == '1':
r_checkl.append('0')
else:
r_checkl.append('1')
r_checks = ''.join(r_checkl)
r_hex = hex(int(r_checks[2:], 2))
r_hex1 = (r_hex.split('x')[1]).zfill(4)
print("So the checksum is ", r_hex1)
if (r_hex1 == '0000'):
print("Correct data received")
else:
print("Incorrect data is being received")
a= int(input("enter 1 for decimal and 2 for strings: "))
if a==1:
bit= int(input("enter 1 for 4bit and 2 for 8bit checksums: "))
if bit==1:
b= int(input("enter 1 for sender and 2 for reciever: "))
if b==1:
print(" Senders Side ")
print("---------------------------------------------\n")
inp1=[]
c=int(input("number of elements: "))
for i in range(c):
d= int(input("enter the number for "+ str(i)+ " position: "))
inp1.append(d)
checksumdecimal8bitsen(inp1)
if b==2:
print(" Recievers Side ")
print("---------------------------------------------\n")
inp1=[]
c=int(input("number of elements: "))
for i in range(c):
d= int(input("enter the number for "+ str(i)+ " position: "))
inp1.append(d)
checksumdecimal8bitrec(inp1)
else:
print("enter valid option")
if bit==2:
b= int(input("enter 1 for sender and 2 for reciever: "))
if b==1:
print(" Senders Side ")
print("---------------------------------------------\n")
inp1=[]
c=int(input("number of elements: "))
for i in range(c):
d= int(input("enter the number for "+ str(i)+ " position: "))
inp1.append(d)
checksumdecimal8bitsen(inp1)
if b==2:
print(" Recievers Side ")
print("---------------------------------------------\n")
inp1=[]
c=int(input("number of elements: "))
for i in range(c):
d= int(input("enter the number for "+ str(i)+ " position: "))
inp1.append(d)
checksumdecimal4bitrec(inp1)
else:
print("enter valid option")
if a==2:
b= int(input(" enter 1 for sender and 2 for reciever: "))
if b==1:
print("")
print(" Senders Side ")
print("---------------------------------------------\n")
sender = input("Enter the senders message: ")
if len(sender) % 2 == 1:
sender = '0' + sender
else:
sender = sender
checksum2sen(sender)
if b==2:
print(" Recievers Side ")
print("---------------------------------------------\n")
inpt=int(input('Enter the total times you want to input: '))
rlist = []
input_string = input("Enter a list elements separated by space in receiver side: ")
userList = input_string.split()
checksum2rec(userList)
else:
print("enter valid option")
else:
print("enter valid option")