How to calculate my encoding sha256 maximum int lenght?


I use this little code in a function to generate immutable hash of strings and store it.
My problem is i don’t know how to find the max possible value with sha256 :7 ‘little’ ???

int.from_bytes(hashlib.sha256(value.encode('utf-8')).digest()[:7], 'little')
Asked By: Jonito



Well, if you have seven bytes, and you turn that into an integer, the maximum value is the same as the maximum value of a (7*8) bit integer, because there are 8 bits in a byte. The largest value of a 56-bit unsigned integer is 2**56 – 1, and the smallest value is 0.

>>> 2**56 - 1

What about negative values? int.from_bytes() interprets its value as unsigned by default, so you won’t have negative values.

Answered By: Nick ODell
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.