eDonkey2000 Hash in Python

February 21st, 2010

As part of a filesystem watcher program I maintain as part of a larger site, I need to obtain the ed2k hash of some files. Believe me when I tell you I have searched high and low for an Edonkey2000 hash algorithm in python. I've seen massive libraries that scared the bejesus out of me, but nothing clean, concise, and without external dependencies. Enjoy the below. It's worth noting it's just as fast as the C version.

import hashlib

def hash_file(self, file_path):
    """ Returns the ed2k hash of a given file. """

    md4 = hashlib.new('md4').copy

    def gen(f):
        while True:
            x = f.read(9728000)
            if x: yield x
            else: return

    def md4_hash(data):
        m = md4()
        m.update(data)
        return m

    with open(file_path, 'rb') as f:
        a = gen(f)
        hashes = [md4_hash(data).digest() for data in a]
        if len(hashes) == 1:
            return hashes[0].encode("hex")
        else: return md4_hash(reduce(lambda a,d: a + d, hashes, "")).hexdigest()
 

Reverse Bitwise Flag Calculator

September 21st, 2009

Sometimes when you need to deal with bitwise flags, it can get a little difficult to figure out what that gigantic number you're using really consists of. The below should help:

#!/usr/bin/python
#bc.py - ./bc.py 108 = flags that make up 108.
import sys

def decompose(i):
  limit = i+1
  tmpi = 1
  while tmpi < limit:
    if i & tmpi == tmpi:
      print tmpi
    tmpi *= 2

if __name__ == "__main__":
  try:
    b = int(sys.argv[1])
    decompose(b)
  except:
    print "You need to specify an integer"
    exit(0)
 
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan