what does sha256 actually do?

1.3k Views Asked by At

I am trying to understand hashing in python and in particular sha256.

I have a standard python function that uses the hashlib for creating a sha256 hash like this:

import hashlib

def hash_password(password):
    """Hashes a password using the SHA-256 algorithm."""
    hash_object = hashlib.sha256()
    hash_object.update(password.encode('utf-8'))
    return hash_object.hexdigest()

password = 'password123'
hashed_password = hash_password(password)
print(hashed_password)

I was expecting a function with a clear process.

So i navigate the the definition of .sha256() in the hashlib.pyi module to find this:

def sha256(string: ReadableBuffer = b"", *, usedforsecurity: bool = True) -> _Hash: ...

But i simply do not understand what this is doing ? it looks like a function that takes arguments and does nothing ....

So what does this function do please ?

1

There are 1 best solutions below

3
Ali Massoud On

The sha256() function in the hashlib module of Python is used to create a SHA256 hash object. It is a constructor method for the SHA256 hash algorithm. The sha256() function takes a byte-like input and returns a hashed value.

the file .pyi is a python interface.

Refer to the documentation if needed
https://docs.python.org/3/library/hashlib.html