Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- def bit_not(n, numbits=16):
- return (1 << numbits) - 1 - n
- class ChecksumCalculator:
- def __init__(self, tgt):
- self.tgt = tgt
- def _conv_16bits(self):
- res_list = []
- temp = "".join(self.tgt.split(" "))
- i = 0
- j = 0
- for x in range(len(temp) // 4):
- unit = ""
- while i < 4:
- unit += temp[j]
- i += 1
- j += 1
- res_list.append(unit)
- i = 0
- return res_list[:-1]
- def _calc_sum(self, bits=None):
- s = 0
- if not bits:
- bits=self._conv_16bits()
- for item in bits:
- s += int(item, 16)
- res = hex(s).split('x')[-1]
- res = "000" +res[0] + " " + res[1:]
- return res.upper()
- def combine_inverse(self):
- s = self._calc_sum().split(" ")
- t = "".join(self._calc_sum(s)[3:].split(" "))
- combined = hex(bit_not(int(bin(int(t, 16)), 2))).split('x')[-1].upper()
- return " ".join(self._conv_16bits()) + " " + combined
- if __name__ == "__main__":
- pkt_header = input("Please enter a packet header string to process: ").upper()
- job = ChecksumCalculator(pkt_header)
- print("Done! Result is: %s." % job.combine_inverse())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement