Skip to main content

A Python wrapper for Troy Hunt's Pwned Passwords API.

Project description

.. raw:: html

<!-- <p align="center">
<img width="344" height="225" src="meta/repo-banner-small.png" />
</p> -->

|image0| |image1|

|CI Status| |Version| |Python Versions|

``pwnedpasswords`` is a small Python wrapper and command line utility
that lets you check if a passphrase has been pwned using the `Pwned
Passwords v2 API <>`_.
All provided password data is
`k-anonymized <>`_ before
sending to the API, so plaintext passwords never leave your computer.


Pwned Passwords are more than half a billion passwords which have
previously been exposed in data breaches. The service is detailed in
the `launch blog
post <>`_
then `further expanded on with the release of version
2 <>`_.
The entire data set is `both downloadable and searchable online via
the Pwned Passwords page <>`_.


pwnedpasswords is available for download through
`PyPi <>`_. You can install
it right away using pip.

.. code:: bash

pip install pwnedpasswords


.. code:: python

import pwnedpasswords

pwnedpasswords.check("testing 123")
# Returns 1

Security Note

No plaintext passwords ever leave your machine using pwnedpasswords.

How does that work? Well, the Pwned Passwords v2 API has a pretty cool
`k-anonymity <>`_


Formally, a data set can be said to hold the property of
k-anonymity, if for every record in a released table, there are k −
1 other records identical to it.

This allows us to only provide the first 5 characters of the SHA-1 hash
of the password in question. The API then responds with a list of SHA-1
hash suffixes with that prefix. On average, that list contains 478

People smarter than I am have used
`math <>`_
to prove that 5-character prefixes are sufficient to maintain
k-anonymity for this database.

In short: your plaintext passwords are protected if you use this
library. You won’t leak enough data to identity which passwords you’re
searching for.


pwnedpasswords automatically checks if your provided input looks like a
SHA-1 hash. If it does, it won’t do any further processing. If it looks
like plain text, it’ll automatically hash it before sending it to the
Pwned Passwords API.

If you’d like to provide an already-hashed password as input to
pwnedpasswords, you don’t need to do anything–pwnedpasswords will detect
that it looks like a SHA-1 hash and won’t hash it again before sending
it to the ``range`` endpoint.

.. code:: python


Likewise, if a password looks like a SHA-1 hash (i.e., matches the regex
``[0-9a-fA-F]{40}``) but is actually a user-provided password, set
``plain_text`` to ``True``, so that the library knows to hash it before
sending it to the API.

.. code:: python

pwnedpasswords.check("1231231231231231231231231231231231231231", plain_text=True)



This is the preferred method. By default, the ``check`` method uses the
```` endpoint, which is
`k-anonymous <>`_.

.. code:: python

# 34729

If you’d like to force pwnedpasswords to use the search endpoint instead
(, set the ``anonymous``
parameter to ``False``.

.. code:: python

pwnedpasswords.check("password", anonymous=False)
# 3303003

You might want to do this if you’d prefer faster response times, and
aren’t that worried about leaking passwords you’re searching for over
the network.

If you’d like direct access to the search and range endpoints, you can
also call them directly.


.. code:: python"testing 123")
# outputs 1


.. code:: python

# outputs a dictionary mapping SHA-1 hash suffixes to frequency counts

Command Line Utility

pwnedpasswords comes bundled with a handy command line utility.

.. code:: bash

$ pwnedpasswords 123456password

Output is simply the number of entries found in the Pwned Passwords

If you’d like to prevent input from appearing in your history, specify
the ``--stdin`` argument to provide input via stdin (h/t to
[@tveastman]( for requesting this).

.. code:: bash

$ pwnedpasswords --stdin

For help, just provide ``-h`` as a command-line argument.

.. code:: bash

$ pwnedpasswords -h
usage: pwnedpasswords [-h] [--verbose] [--plain-text] (--stdin | password)

Checks Pwned Passwords API to see if provided plaintext data was found in a
data breach.

positional arguments:
password The password or hashed password to search for.

optional arguments:
-h, --help show this help message and exit
--verbose Display verbose output.
--plain-text Specify that the provided input is plain text, even if it
looks like a SHA-1 hash.
--stdin Read provided input from stdin.


The CLI returns an exit code equal to the base-10 log of the result
count, plus 1. If there are no matches in the API, the exit status will
be ``0``. While returning the base-10 log might seem odd, note that most
systems require exit status codes to be in the range 0-127, and I wanted
the status code to provide *some* indication for severity. log(N) seemed
to be a good tradeoff. The exit status is log(N)+1 since there are
plenty of matches in the database with 1 match.

If you’d like to take a look under the hood to make sure things are
working as they should, set the ``--verbose`` flag.

.. code:: bash

$ pwnedpasswords 123456password --verbose
INFO:pwnedpasswords.pwnedpasswords:Entry found


Special thanks to `Troy Hunt <>`_ for
collecting this data and providing this service.


Dan Loewenherz / ([@dlo](

See also

`django-pwnedpasswords-validator <>`_,
a validator that checks user passwords against the Pwned Passwords API
using this library.


Apache License, Version 2.0. See `LICENSE <>`_ for details.

.. |image0| image:: meta/repo-banner.png
.. |image1| image:: meta/repo-banner-bottom.png
.. |CI Status| image::
.. |Version| image::
.. |Python Versions| image::

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
pwnedpasswords-1.2.1-py2.py3-none-any.whl (13.8 kB) Copy SHA256 hash SHA256 Wheel py2.py3

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page