Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Created January 13, 2016 16:39
Show Gist options
  • Save cheeyeo/bcb2bc7040eedfd66481 to your computer and use it in GitHub Desktop.
Save cheeyeo/bcb2bc7040eedfd66481 to your computer and use it in GitHub Desktop.
Password entropy example
import re
from math import log,pow
class PasswordStrength:
def __init__(self):
self.numeric=re.compile('\d')
self.loweralpha=re.compile('[a-z]')
self.upperalpha=re.compile('[A-Z]')
self.symbols=re.compile('[-_.:,;<>?"#$%&/()!@~]')
self.num_of_symbols=20 # adjust accordingly...
def calculate_entropy(self,password=''):
charset = 0
if self.numeric.search(password):
charset += 10
if self.loweralpha.search(password):
charset += 26
if self.upperalpha.search(password):
charset += 26
if self.symbols.search(password):
charset += self.num_of_symbols
entropy = log(pow(charset,len(password)),2)
return entropy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment