Skip to content

Instantly share code, notes, and snippets.

@oyvholm
Forked from giomasce/git-annex hashing functions
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oyvholm/2897bd9537bbe4b1c8fc to your computer and use it in GitHub Desktop.
Save oyvholm/2897bd9537bbe4b1c8fc to your computer and use it in GitHub Desktop.
# See docs in http://git-annex.branchable.com/internals/hashing/ and implementation in http://sources.debian.net/src/git-annex/5.20140227/Locations.hs/?hl=408#L408
import hashlib
import struct
def hashdirlower(key):
hasher = hashlib.md5()
hasher.update(key)
digest = hasher.hexdigest()
return "%s/%s/" % (digest[:3], digest[3:6])
def hashdirmixed(key):
hasher = hashlib.md5()
hasher.update(key)
digest = hasher.digest()
first_word = struct.unpack('<I', digest[:4])[0]
nums = [first_word >> (6 * x) & 31 for x in xrange(4)]
letters = ["0123456789zqjxkmvwgpfZQJXKMVWGPF"[i] for i in nums]
return "%s%s/%s%s/" % (letters[1], letters[0], letters[3], letters[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment