Skip to content

Instantly share code, notes, and snippets.

@Browne
Created October 13, 2011 02:03
Show Gist options
  • Select an option

  • Save Browne/1283158 to your computer and use it in GitHub Desktop.

Select an option

Save Browne/1283158 to your computer and use it in GitHub Desktop.
Password Validation Python - RegEx and '/usr/share/dict/words'
"""
Copyright (c) <2012> <Eliot Brown>.
All rights reserved.
Redistribution and use in source and binary forms are permitted
provided that the above copyright notice and this paragraph are
duplicated in all such forms and that any documentation,
advertising materials, and other materials related to such
distribution and use acknowledge that the software was developed
by Eliot Brown. The name of the
author may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"""
def valid_password(password):
''' Validates password using a regular expression and checking the
built in dictionary in Linux for words greater than 4 characters
'''
if re.match('^([a-z]*|[A-Z]*|[0-9]*|.{0,7})$', password):
return False
with open('/usr/share/dict/words', 'r') as f:
for word in [line[:-1] for line in f.readlines() if len(line) > 4]:
if word in password:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment