1  Benchmark of the SHA256 hash function, with Python, Cython and Numba
1.1  What is a hash function?
1.2  Common API for the different classes
1.4  Pure Python code for the SHA-2 hashing function
1.4.1  Useful functions the SHA-2 algorithm
1.4.2  The `SHA2` class
1.4.3  Check on SHA-2
1.4.4  Trying 1000 random examples
1.5  Numba-powered code for the SHA-2 hashing function
1.5.1  Requirements
1.5.2  Useful functions the SHA-2 algorithm
1.5.3  The `SHA2_Numba` class
1.5.4  Check on SHA-2
1.6  Cython-power code for the `SHA-2` hashing function
1.6.1  Requirements
1.6.2  Useful functions the SHA-2 algorithm
1.6.3  The `SHA2_Cython` class
1.6.4  Check on SHA-2
1.7  Conclusion
1.7.1  Bonus
# # Benchmark of the SHA256 hash function, with Python, Cython and Numba # # This small [Jupyter notebook](https://www.Jupyter.org/) is a short experiment, to compare the time complexity of three different implementations of the [SHA-256 hash function](https://en.wikipedia.org/wiki/SHA-2), in pure [Python](https://www.Python.org/), with [Cython](http://Cython.org/), and with [Numba](http://Numba.PyData.org/). # # - *Reference*: Wikipedia pages on [Hash functions](https://en.wikipedia.org/wiki/Hash_function) and [SHA-2](https://en.wikipedia.org/wiki/SHA-2). # - *Date*: 21 June 2017. # - *Author*: [Lilian Besson](https://GitHub.com/Naereen/notebooks). # - *License*: [MIT Licensed](https://LBesson.MIT-License.org/). # ---- # ## What is a hash function? # > TL;DR : [Hash functions](https://en.wikipedia.org/wiki/Hash_function) and [cryptographic hashing functions](https://en.wikipedia.org/wiki/Cryptographic_hash_function) on Wikipedia. # ---- # ## Common API for the different classes # # I will copy the API proposed by [the `hashlib` module in Python standard library](https://docs.python.org/3/library/hashlib.html), so it will be very easy to compare my implementations with the one provided with your default [Python](https://www.Python.org/) installation. # In[2]: class Hash(object): """ Common class for all hash methods. It copies the one of the hashlib module (https://docs.python.org/3.5/library/hashlib.html). """ def __init__(self, *args, **kwargs): """ Create the Hash object.""" self.name = self.__class__.__name__ # https://docs.python.org/3.5/library/hashlib.html#hashlib.hash.name self.byteorder = 'little' self.digest_size = 0 # https://docs.python.org/3.5/library/hashlib.html#hashlib.hash.digest_size self.block_size = 0 # https://docs.python.org/3.5/library/hashlib.html#hashlib.hash.block_size def __str__(self): return self.name def update(self, arg): """ Update the hash object with the object arg, which must be interpretable as a buffer of bytes.""" pass def digest(self): """ Return the digest of the data passed to the update() method so far. This is a bytes object of size digest_size which may contain bytes in the whole range from 0 to 255.""" return b"" def hexdigest(self): """ Like digest() except the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.""" digest = self.digest() raw = digest.to_bytes(self.digest_size, byteorder=self.byteorder) format_str = '{:0' + str(2 * self.digest_size) + 'x}' return format_str.format(int.from_bytes(raw, byteorder='big')) # ---- # ## Checking the [the `hashlib` module in Python standard library](https://docs.python.org/3/library/hashlib.html) # In[3]: import hashlib # We can check [the available algorithms](https://docs.python.org/3.5/library/hashlib.html#hashlib.algorithms_available), some of them being [guaranteed to be on any platform](https://docs.python.org/3.5/library/hashlib.html#hashlib.algorithms_guaranteed), some are not. # In[4]: list(hashlib.algorithms_available) # I will need at least this one: # In[5]: assert 'SHA256' in hashlib.algorithms_available # Lets check that they have the block size and digest size announced: # In[6]: name = 'SHA256' s = hashlib.sha256() print("For {:<8} : the block size is {:<3} and the digest size is {:<2}.".format(name, s.block_size, s.digest_size)) # ---- # ## Pure Python code for the SHA-2 hashing function # Let now study and implement a last hashing function, again slightly harder to write but more secure: SHA-2, "Secure Hash Algorithm, version 2". # See [the SHA-2 hashing function](https://en.wikipedia.org/wiki/SHA-2) on Wikipedia, if needed. # #
Remark: it is not (yet) considered broken, and it is the military standard for security and cryptographic hashing. SHA-3 is preferred for security purposes.