Code

sqlite doesn't need external locking
[roundup.git] / roundup / password.py
index 459e1f7877c86d94f9e3b0924dd9767f65c3a0b0..a267e4223a211dc54d7fca773943cf3d519d76f1 100644 (file)
 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 # 
-# $Id: password.py,v 1.7 2002-09-26 13:38:35 gmcm Exp $
+# $Id: password.py,v 1.11 2004-02-11 23:55:08 richard Exp $
 
-__doc__ = """
-Password handling (encoding, decoding).
+"""Password handling (encoding, decoding).
 """
+__docformat__ = 'restructuredtext'
 
-import sha, re, string
+import sha, re, string, random
 try:
     import crypt
 except:
     crypt = None
     pass
 
+class PasswordValueError(ValueError):
+    ''' The password value is not valid '''
+    pass
+
 def encodePassword(plaintext, scheme, other=None):
     '''Encrypt the plaintext password.
     '''
+    if plaintext is None:
+        plaintext = ""
     if scheme == 'SHA':
         s = sha.sha(plaintext).hexdigest()
     elif scheme == 'crypt' and crypt is not None:
@@ -43,9 +49,13 @@ def encodePassword(plaintext, scheme, other=None):
     elif scheme == 'plaintext':
         s = plaintext
     else:
-        raise ValueError, 'Unknown encryption scheme "%s"'%scheme
+        raise PasswordValueError, 'unknown encryption scheme %r'%scheme
     return s
 
+def generatePassword(length=8):
+    chars = string.letters+string.digits
+    return ''.join([random.choice(chars) for x in range(length)])
+
 class Password:
     '''The class encapsulates a Password property type value in the database.