Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading