Never been to DZone Snippets before?

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

About this user

Andrew Pennebaker http://mcandre.devjavu.com/wiki

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

BSD.py

// BSD Unix TCP/IP Checksum

   1  
   2  #!/usr/bin/env python
   3  
   4  __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
   5  __date__="21 Dec 2005 - 3 May 2006"
   6  __copyright__="Copyright 2006 Andrew Pennebaker"
   7  __license__="GPL"
   8  __version__="0.3"
   9  __URL__="http://snippets.dzone.com/posts/show/3542"
  10  
  11  import HashFunction
  12  
  13  class BSD(HashFunction.HashFunction):
  14  	BLOCK_SIZE=1
  15  	DIGEST_SIZE=2
  16  
  17  	INIT=0x00
  18  	SUM_REQ="Sum >= 0"
  19  
  20  	TEST_DATA="abc"
  21  	TEST_HASH=0x40ac
  22  
  23  	def __init__(self, sum=0x00):
  24  		self.sum=sum
  25  
  26  	def sumValid(self, sum):
  27  		return sum>=0
  28  
  29  	def rotate(self, b):
  30  		if (b&1)!=0:
  31  			return (b>>1)+0x8000
  32  
  33  		return b>>1
  34  
  35  	def _update(self, b):
  36  		self.sum=(self.rotate(self.sum)+b)&0xffff
  37  
  38  	def digest(self):
  39  		return self.sum
  40  
  41  	def format(self, data):
  42  		return "%05d" % (data)
  43  
  44  	def unformat(self, hash):
  45  		return int(hash)
  46  
  47  if __name__=="__main__":
  48  	HashFunction.main(BSD)
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS