summaryrefslogtreecommitdiffstats
path: root/Doc/library/crypt.rst
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-09-28 13:20:38 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2012-09-28 13:20:38 (GMT)
commit74cca70ce28859906cfbd4e95520c6add6461d9e (patch)
tree3d99d3e60f7846d6a20e58406a3fd293e9fd7994 /Doc/library/crypt.rst
parent2246aa8ac8138dd60e2f6ec5bf5534929d7979db (diff)
downloadcpython-74cca70ce28859906cfbd4e95520c6add6461d9e.zip
cpython-74cca70ce28859906cfbd4e95520c6add6461d9e.tar.gz
cpython-74cca70ce28859906cfbd4e95520c6add6461d9e.tar.bz2
Now that it's possible, avoid timing attacks in the crypt module examples)
Diffstat (limited to 'Doc/library/crypt.rst')
-rw-r--r--Doc/library/crypt.rst10
1 files changed, 7 insertions, 3 deletions
diff --git a/Doc/library/crypt.rst b/Doc/library/crypt.rst
index 1ba2ed3..b4c90cd 100644
--- a/Doc/library/crypt.rst
+++ b/Doc/library/crypt.rst
@@ -121,11 +121,14 @@ The :mod:`crypt` module defines the following functions:
Examples
--------
-A simple example illustrating typical use::
+A simple example illustrating typical use (a constant-time comparison
+operation is needed to limit exposure to timing attacks.
+:func:`hmac.compare_digest` is suitable for this purpose)::
import pwd
import crypt
import getpass
+ from hmac import compare_digest as compare_hash
def login():
username = input('Python login: ')
@@ -134,7 +137,7 @@ A simple example illustrating typical use::
if cryptedpasswd == 'x' or cryptedpasswd == '*':
raise ValueError('no support for shadow passwords')
cleartext = getpass.getpass()
- return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
+ return compare_hash(crypt.crypt(cleartext, cryptedpasswd), cryptedpasswd)
else:
return True
@@ -142,7 +145,8 @@ To generate a hash of a password using the strongest available method and
check it against the original::
import crypt
+ from hmac import compare_digest as compare_hash
hashed = crypt.crypt(plaintext)
- if hashed != crypt.crypt(plaintext, hashed):
+ if not compare_hash(hashed, crypt.crypt(plaintext, hashed)):
raise ValueError("hashed version doesn't validate against original")