How to hash int/long using hashlib in Python?

2024/10/15 2:24:16

I'm developing a set of cryptographic algorithms / protocols for educational purposes. Specifically, I am currently working on OAEP encoding.

OAEP involves use of cryptographic hash functions; therefore I wanted to use the hashlib library, provided in the standard of Python3.

Let's say, I have a 128-bit integer, for which I want to get the SHA256 digest of. How can I do this in Python? All I could found was how to hash strings (or b-strings) with hashlib.sha256().

Answer

Hashes work on bytes, a sequence of integer values in the range 0-255; this is independent of the implementation language. You'd have to convert your 128-bit integer into a series of bytes representing that value. That's why the hashlib module only accepts bytes objects ("b values").

How you do this is entirely dependent on the use case; you'd need to see how the specific OAEP standard specifies how such an integer is represented.

For example, you could take the string representation of the decimal integer value; a sequence of ASCII digits; this is not a very efficient method as that can take up to 39 bytes:

>>> import hashlib
>>> 2 ** 128 - 1  # largest 128-bit value
340282366920938463463374607431768211455
>>> len(str(2 ** 128 - 1))
39
>>> str(2 ** 128 - 1).encode('ASCII')  # ascii bytes
b'340282366920938463463374607431768211455'
>>> hashlib.sha256(str(2 ** 128 - 1).encode('ASCII')).hexdigest()
'f315ff319bf588e202110ab686fb8c3dbca12b4df9fbd844615b566a2fff3e75'

A much more efficient method would be to take those 128 bits, divide them into 16 bytes and hash those. You then need to decide on byte order (little or big endian). If you need to hash multiple integer values, then the Python struct module can help with producing the bytes, but only for integer values up to 8 bytes (you'd have to split up your larger numbers first). For single values, just use the int.to_bytes() method:

>>> (2 ** 128 - 1).to_bytes(16, 'little', signed=False)
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
>>> hashlib.sha256((2 ** 128 - 1).to_bytes(16, 'little', signed=False)).hexdigest()
'5ac6a5945f16500911219129984ba8b387a06f24fe383ce4e81a73294065461b'
https://en.xdnf.cn/q/69339.html

Related Q&A

SQLAlchemy: Override relationship-defined order_by in a query

So, I have a model that is something like:class Foo(model):__tablename__ = "foo"id = Column(Integer, primary_key=True)data = relationship("FooData",cascade="all, delete-orphan&…

Toplevel in Tkinter: Prevent Two Windows from Opening

Say I have some simple code, like this:from Tkinter import * root = Tk() app = Toplevel(root) app.mainloop()This opens two windows: the Toplevel(root) window and the Tk() window. Is it possible to avoi…

Specify File path in tkinter File dialog

I have a file dialog to open a file, however, the file that I want to open is in a different directory than the program I wrote. The file dialog opens to the directory where I am. Is there a way to s…

Why does scipy linear interpolation run faster than nearest neighbor interpolation?

Ive written a routine that interpolates point data onto a regular grid. However, I find that scipys implementation of nearest neighbor interpolation performs almost twice as slow as the radial basis f…

How do I create a 404 page?

My application catches all url requests with an @app.route, but occasionally I bump into a bad url for which I have no matching jinja file (bu it does match an existing @app.route). So I want to redire…

Injecting pre-trained word2vec vectors into TensorFlow seq2seq

I was trying to inject pretrained word2vec vectors into existing tensorflow seq2seq model.Following this answer, I produced the following code. But it doesnt seem to improve performance as it should, a…

MySQL Stored Procedures, Pandas, and Use multi=True when executing multiple statements

Note - as MaxU suggested below, the problem is specific to mysql.connector and does not occur if you use pymysql. Hope this saves someone else some headachesUsing Python, Pandas, and mySQL and cannot…

How can I change the font size in GTK?

Is there an easy way to change the font size of text elements in GTK? Right now the best I can do is do set_markup on a label, with something silly like:lbl.set_markup("<span font_desc=Tahoma …

How to read BigQuery table using python pipeline code in GCP Dataflow

Could someone please share syntax to read/write bigquery table in a pipeline written in python for GCP Dataflow

How can I wrap a python function in a way that works with with inspect.signature?

Some uncontroversial background experimentation up front: import inspectdef func(foo, bar):passprint(inspect.signature(func)) # Prints "(foo, bar)" like youd expectdef decorator(fn):def _wra…