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 

Adler32.py

   1  
   2  __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)"
   3  __date__="21 Dec 2005 - 17 Jul 2006"
   4  __copyright__="Copyright 2006 Andrew Pennebaker"
   5  __license__="GPL"
   6  __version__="0.3"
   7  __URL__="http://snippets.dzone.com/posts/show/2889"
   8  
   9  import HashFunction
  10  
  11  class Adler32(HashFunction.HashFunction):
  12  	BLOCK_SIZE=1
  13  	DIGEST_SIZE=2
  14  
  15  	INIT=0x0001
  16  	SUM_REQ="Sum >= 0"
  17  
  18  	BASE=65521
  19  
  20  	TEST_DATA="abc"
  21  	TEST_HASH=0x24d0127
  22  
  23  	def __init__(self, sum=0x0001):
  24  		self.sum=sum
  25  
  26  	def sumValid(self, sum):
  27  		return sum>=0
  28  
  29  	def _update(self, b):
  30  		s1=self.sum&0xffff
  31  		s2=(self.sum>>16)&0xffff
  32  
  33  		s1=(s1+(b&0xff))%self.BASE
  34  		s2=(s1+s2)%self.BASE
  35  
  36  		self.sum=(s2<<16)|s1
  37  
  38  	def digest(self):
  39  		return self.sum
  40  
  41  	def formatDigest(self):
  42  		return "%02x" % (self.digest())
  43  
  44  	def unformatDigest(self, hash):
  45  		return int(hash, 16)
  46  
  47  if __name__=="__main__":
  48  	HashFunction.main(Adler32, "Adler32.py")
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS