liveBook · Manning

Hashlib

As explained in Python Concurrency with asyncio

hashlib is Python’s standard-library module providing common cryptographic hash algorithms and related constructors.

hashlib provides Python’s standard-library hashing algorithms for turning input into values that are difficult to read or recover. Hashes support password checks and tamper detection without storing the original password. section 7.5.1

The practical rules are:

  • Prefer scrypt for password hashing because it uses a salt and resists straightforward recovery and brute-force attacks better than plain SHA choices. section 7.5.1
  • Consider multithreading for large hashing workloads: many hashlib operations release the GIL above 2048 bytes, while scrypt always releases it. section 7.5 section 7.5.1

Hashing passwords with scrypt

Generate a random salt with os.urandom(16), then call hashlib.scrypt(password, salt=salt, n=2048, p=1, r=8). A synchronous loop hashes 10,000 generated passwords; a threaded asyncio version submits the same work to a ThreadPoolExecutor and awaits it with asyncio.gather.

How Python Concurrency with asyncio covers it

How deep it goes
Explains hash purpose, salts, algorithm choices, and threaded execution, but stops short of detailed cryptographic internals.
Examples
Demonstrates salted scrypt hashing for 10,000 generated passwords synchronously and through threaded asyncio execution.
What you can do after
Apply scrypt to salted password hashing and distribute suitable hashing workloads across threads with asyncio.
How it connects
Connects cryptographic hash functions with scrypt, salts, the GIL, multithreading, asyncio, and ThreadPoolExecutor.

Also explained in

Compare how 2 books explain hashlib →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.