all Python 3hashlibfunction
hashlib.sha1()
hashing
The SHA-1 hash: 20 bytes, 40 hex digits. It is broken for collisions, so it is unfit for signatures and passwords and survives for compatibility.
Syntax
hashlib.sha1(data=b'', *, usedforsecurity=True)
A practical SHA-1 collision was published in 2017, so two different inputs with the same hash can be crafted. It lives on where the format was fixed long ago: git object ids, older protocols, uuid5(). New code uses sha256(); when SHA-1 is needed for compatibility rather than protection, say so with usedforsecurity=False, or environments with a strict policy (FIPS mode) will refuse the call.
Examples
import hashlib
h = hashlib.sha1(b'abc')
print(h.hexdigest()[:16]) # → a9993e364706816a
print(h.digest_size, len(h.hexdigest())) # → 20 40
# Python 3.9+
import hashlib
# не для защиты, а для совместимости — так и говорим
h = hashlib.sha1(b'abc', usedforsecurity=False)
print(h.hexdigest()[:16]) # → a9993e364706816a
See also
Open in the full glossarydocs.python.orgReport an inaccuracy