Import checksumming from scapy
This commit is contained in:
parent
d5bc273111
commit
e7769abe31
1 changed files with 23 additions and 0 deletions
23
bin/checksum_from_scapy.py
Normal file
23
bin/checksum_from_scapy.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# Code copied from /usr/lib/python3/dist-packages/scapy/utils.py
|
||||
|
||||
|
||||
if struct.pack("H",1) == b"\x00\x01": # big endian
|
||||
def checksum(pkt):
|
||||
if len(pkt) % 2 == 1:
|
||||
pkt += b"\0"
|
||||
s = sum(array.array("H", pkt))
|
||||
s = (s >> 16) + (s & 0xffff)
|
||||
s += s >> 16
|
||||
s = ~s
|
||||
return s & 0xffff
|
||||
else:
|
||||
def checksum(pkt):
|
||||
if len(pkt) % 2 == 1:
|
||||
pkt += b"\0"
|
||||
s = sum(array.array("H", pkt))
|
||||
s = (s >> 16) + (s & 0xffff)
|
||||
s += s >> 16
|
||||
s = ~s
|
||||
return (((s>>8)&0xff)|s<<8) & 0xffff
|
Loading…
Reference in a new issue