Skip to content

Instantly share code, notes, and snippets.

@giomasce
Last active December 22, 2020 05:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save giomasce/a7802bda1417521c5b30 to your computer and use it in GitHub Desktop.
Save giomasce/a7802bda1417521c5b30 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])
@anarcat
Copy link

anarcat commented Mar 23, 2015

what is hashdirmixed? i am unclear as to why the struct is necessary?

@giomasce
Copy link
Author

It mimics the corresponding function in git-annex, see https://sources.debian.net/src/git-annex/5.20150731-1/Annex/DirHashes.hs/?hl=63#L63.

@DanielDent
Copy link

I realize these functions are relatively trivial, but I also appreciated not having to write them myself :).

I'm using them in https://github.com/DanielDent/git-annex-remote-rclone/blob/master/migrations/frankencase-to-mixed/frankencase-to-mixed-helper.py and would like to provide my users with a clear license for the code. Would you be able to clarify the terms under which this code is provided?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment