Improve example for crypt module. No string exceptions..

This commit is contained in:
Éric Araujo 2011-05-29 03:24:45 +02:00
parent 7f9b37be45
commit bc57789daf
1 changed files with 7 additions and 5 deletions

View File

@ -131,18 +131,20 @@ Examples
A simple example illustrating typical use:: A simple example illustrating typical use::
import crypt, getpass, pwd import pwd
import crypt
import getpass
def login(): def login():
username = input('Python login:') username = input('Python login: ')
cryptedpasswd = pwd.getpwnam(username)[1] cryptedpasswd = pwd.getpwnam(username)[1]
if cryptedpasswd: if cryptedpasswd:
if cryptedpasswd == 'x' or cryptedpasswd == '*': if cryptedpasswd == 'x' or cryptedpasswd == '*':
raise "Sorry, currently no support for shadow passwords" raise ValueError('no support for shadow passwords')
cleartext = getpass.getpass() cleartext = getpass.getpass()
return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
else: else:
return 1 return True
To generate a hash of a password using the strongest available method and To generate a hash of a password using the strongest available method and
check it against the original:: check it against the original::
@ -151,4 +153,4 @@ check it against the original::
hashed = crypt.crypt(plaintext) hashed = crypt.crypt(plaintext)
if hashed != crypt.crypt(plaintext, hashed): if hashed != crypt.crypt(plaintext, hashed):
raise "Hashed version doesn't validate against original" raise ValueError("hashed version doesn't validate against original")