Python - Criptografia

Introducció

Crea un projecte amb el nom crypto:

Terminal window
uv init crypto

Afegeix una dependència amb cryptography:

Terminal window
uv add cryptography

Hash

BLAKE2

BLAKE2 is a cryptographic hash function specified in RFC 7693.

BLAKE2’s design makes it immune to length-extension attacks, an advantage over the SHA-family of hashes.

from cryptography.hazmat.primitives import hashes
digest = hashes.Hash(hashes.BLAKE2b(64))
digest.update(b"Tortuga")
digest.update(b"Gallina")
hash = digest.finalize()
print(hash.hex())

El resultat és el hash de TortugaGallina en hexadecimal:

288ed0626f24a06c42b831b41cb6a9e8c8f3b9790c0beaa3bdc1ffb36e739b98d595ec22960f2bc0620604d89d6e64e0127149f9c9600174ea55e10a8524ad2c

El parametre 64 indica que el hash és de 64 bytes.

Si passes algun altre valor es produeix un erro:

ValueError: Digest size must be 64
Terminal window
echo -n "TortugaGalllina" | openssl dgst -blake2b512
BLAKE2B-512(stdin)= 8446c21b9e85c09d071b4f8f33453b8ff0772d3ab679cbdcc1848193b89f16c4e106ac81f51706b09ac6defcac179b79d412b859e5099ecd55b9885ca694f7ef