Last active
December 22, 2020 05:45
-
-
Save giomasce/a7802bda1417521c5b30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
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.
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
what is
hashdirmixed
? i am unclear as to why the struct is necessary?