DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

BSD.py

Andrew Pennebaker user avatar by
Andrew Pennebaker
·
Feb. 19, 07 · · Code Snippet
Like (0)
Save
Tweet
1.18K Views

Join the DZone community and get the full member experience.

Join For Free
// 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)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Do Great Engineering Managers Need To Know About Compensation and Equity?
  • Testing Schema Registry: Spring Boot and Apache Kafka With JSON Schema
  • Message Queuing and the Database: Solving the Dual Write Problem
  • Ultra-Fast Microservices in Java: When Microstream Meets Open Liberty

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo