Search code examples
pythonhashprefix

split a 256 bit hash into 32 bit prefix in python


In python, how does one split a SHA256 hash into a 32bit prefixes? I'm working with Google's safebrowsing api, which requires that I compare 32bit prefixes between my own collection, and the collection the API sends to me. I understand how to pull the list from Google, and I understand how to form a collection of hashes from parsed URLs, however, I don't understand how I am to derive the first 32bits of each hash.

And after obtaining the prefix, would the best course of action between to place them in a dictionary with corresponding key/value pairs being the prefix/full hash, so that I can reference them later?


Solution

  • 32 bits is the first 4 bytes. So you can slice the byte array.

    hash_obj.digest()[:4]
    

    You can take that and use it as a dictionary key.

    EDIT

    I'm not sure if you need the hex representation, that would be.

    hash_obj.hexdigest()[:8]