File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -121,11 +121,14 @@ The :mod:`crypt` module defines the following functions:
121121Examples
122122--------
123123
124- A simple example illustrating typical use::
124+ A simple example illustrating typical use (a constant-time comparison
125+ operation is needed to limit exposure to timing attacks.
126+ :func: `hmac.compare_digest ` is suitable for this purpose)::
125127
126128 import pwd
127129 import crypt
128130 import getpass
131+ from hmac import compare_digest as compare_hash
129132
130133 def login():
131134 username = input('Python login: ')
@@ -134,15 +137,16 @@ A simple example illustrating typical use::
134137 if cryptedpasswd == 'x' or cryptedpasswd == '*':
135138 raise ValueError('no support for shadow passwords')
136139 cleartext = getpass.getpass()
137- return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
140+ return compare_hash( crypt.crypt(cleartext, cryptedpasswd), cryptedpasswd)
138141 else:
139142 return True
140143
141144To generate a hash of a password using the strongest available method and
142145check it against the original::
143146
144147 import crypt
148+ from hmac import compare_digest as compare_hash
145149
146150 hashed = crypt.crypt(plaintext)
147- if hashed != crypt.crypt(plaintext, hashed):
151+ if not compare_hash( hashed, crypt.crypt(plaintext, hashed) ):
148152 raise ValueError("hashed version doesn't validate against original")
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ Cryptographic Services
88
99The modules described in this chapter implement various algorithms of a
1010cryptographic nature. They are available at the discretion of the installation.
11+ On Unix systems, the :mod: `crypt ` module may also be available.
1112Here's an overview:
1213
1314
You can’t perform that action at this time.
0 commit comments