2007-07-02 11:14:47 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2007 by Jan Dittberner.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
# USA.
|
|
|
|
#
|
|
|
|
# Version: $Id$
|
|
|
|
|
|
|
|
import crypt, crack, random
|
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
_pwchars = []
|
|
|
|
for pair in (('0', '9'), ('A', 'Z'), ('a', 'z')):
|
|
|
|
_pwchars.extend(range(ord(pair[0]), ord(pair[1])))
|
|
|
|
for char in "-+/*_@":
|
|
|
|
_pwchars.append(ord(char))
|
|
|
|
|
2007-07-02 11:14:47 +02:00
|
|
|
def generatepassword(minlength = 8, maxlength = 12):
|
|
|
|
"""Generates a random password with a length between the given
|
|
|
|
minlength and maxlength values."""
|
|
|
|
return "".join([chr(letter) for letter in \
|
2007-07-09 08:46:36 +02:00
|
|
|
random.sample(_pwchars,
|
2007-07-02 11:14:47 +02:00
|
|
|
random.randint(minlength, maxlength))])
|
|
|
|
|
|
|
|
def checkpassword(password):
|
|
|
|
"""Checks the password with cracklib. The password is returned if
|
|
|
|
it is good enough. Otherwise None is returned."""
|
|
|
|
try:
|
|
|
|
return crack.VeryFascistCheck(password)
|
|
|
|
except ValueError, ve:
|
|
|
|
print "Weak password:", ve
|
|
|
|
return None
|
|
|
|
|
|
|
|
def md5_crypt_password(password):
|
|
|
|
"""Hashes the given password with MD5 and a random salt value."""
|
|
|
|
salt = "".join([chr(letter) for letter in \
|
2007-07-09 08:46:36 +02:00
|
|
|
random.sample(_pwchars, 8)])
|
2007-07-02 11:14:47 +02:00
|
|
|
return crypt.crypt(password, '$1$' + salt)
|
|
|
|
|
|
|
|
def get_pw_tuple(password = None):
|
|
|
|
"""Gets a valid tuple consisting of a password and a md5 hash of the
|
|
|
|
password. If a password is given it is checked and if it is too weak
|
|
|
|
replaced by a generated one."""
|
|
|
|
while password == None or checkpassword(password) == None:
|
|
|
|
password = generatepassword()
|
|
|
|
return (password, md5_crypt_password(password))
|