Skip to content

Instantly share code, notes, and snippets.

@elliotwutingfeng
Last active April 10, 2025 01:55
Show Gist options
  • Save elliotwutingfeng/c61b8a1c6f80938cd659cd7a831db0e1 to your computer and use it in GitHub Desktop.
Save elliotwutingfeng/c61b8a1c6f80938cd659cd7a831db0e1 to your computer and use it in GitHub Desktop.
Estimate password entropy.
"""Estimate password entropy.
Entropy (bits) | Estimated Strength
---------------|-------------------
0 to 35 | Very weak
36 to 59 | Weak
60 to 119 | Strong
120+ | Very strong
WARNING: High entropy is of little use if the password has been leaked before in data breaches.
A password can be checked against known data breaches at https://haveibeenpwned.com/Passwords
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
"""
import getpass
import math
import sys
password = getpass.getpass(prompt="Password: ", stream=None)
if not password:
sys.exit("Invalid input: Empty password.")
unique_characters = set(password)
entropy_bits = len(password) * math.log2(len(unique_characters))
sys.stdout.write("Length: %d\n" % len(password))
sys.stdout.write("Number of unique characters: %d\n" % len(unique_characters))
sys.stdout.write("Entropy (bits): %d\n" % entropy_bits)
sys.stdout.write("Estimated Strength: ")
if entropy_bits <= 35:
sys.stdout.write("Very weak")
elif entropy_bits <= 59:
sys.stdout.write("Weak")
elif entropy_bits <= 119:
sys.stdout.write("Strong")
else:
sys.stdout.write("Very strong")
sys.stdout.write("\n")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment