How to calculate my encoding sha256 maximum int lenght?
Question:
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')
Answers:
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
72057594037927935
What about negative values? int.from_bytes() interprets its value as unsigned by default, so you won’t have negative values.
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')
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
72057594037927935
What about negative values? int.from_bytes() interprets its value as unsigned by default, so you won’t have negative values.