brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: check a password against Pwned Passwords with curl and five hash characters

Curl
~/tips/tip-security-pwned-passwords-range-api

curl -s "https://api.pwnedpasswords.com/range/${H:0:5}" | grep -i "${H:5:35}"

Checks whether a password is in known breaches by sending only 5 characters of its SHA-1 hash.

Checking a password against a breach list sounds like something you should never do online. The range API makes it safe enough, because the password never leaves your machine.

The Pwned Passwords API uses k-anonymity. You send the first five characters of the password’s SHA-1 hash to https://api.pwnedpasswords.com/range/, and it returns every hash suffix with that prefix, each with a count. You compare the rest locally. No API key is needed.

read -rs PW; echo
H=$(printf %s "$PW" | sha1sum | cut -c1-40)
curl -s "https://api.pwnedpasswords.com/range/${H:0:5}" | grep -i "${H:5:35}"
unset PW H

A line like 0018A45C4D1DEF81644B54AB7F969B88D65:1 means the password turned up in breaches that many times. No output means it is not in the list. The read -rs keeps the password off the screen and out of your shell history, and grep -i covers the upper case letters in the response.

The docs also describe an Add-Padding: true request header. It pads the response to between 800 and 1,000 entries, so the size no longer hints at how many real matches there were. Padded entries always have a count of 0 and can be discarded.

curl -s -H "Add-Padding: true" "https://api.pwnedpasswords.com/range/${H:0:5}"

A hit means the password is public and has to go. A miss only means it was not in that list. I would use this on a password I already have, and leave new ones to a password manager. Type real passwords only on a machine you trust.

Further reading: Have I Been Pwned API v3

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link