DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
BSD.py
// BSD Unix TCP/IP Checksum
#!/usr/bin/env python __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)" __date__="21 Dec 2005 - 3 May 2006" __copyright__="Copyright 2006 Andrew Pennebaker" __version__="0.3" __URL__="http://snippets.dzone.com/posts/show/3542" import HashFunction class BSD(HashFunction.HashFunction): BLOCK_SIZE=1 DIGEST_SIZE=2 INIT=0x00 SUM_REQ="Sum >= 0" TEST_DATA="abc" TEST_HASH=0x40ac def __init__(self, sum=0x00): self.sum=sum def sumValid(self, sum): return sum>=0 def rotate(self, b): if (b&1)!=0: return (b>>1)+0x8000 return b>>1 def _update(self, b): self.sum=(self.rotate(self.sum)+b)&0xffff def digest(self): return self.sum def format(self, data): return "%05d" % (data) def unformat(self, hash): return int(hash) if __name__=="__main__": HashFunction.main(BSD)





