use hashlib module if possible

svn path=/plone.formwidget.captcha/trunk/; revision=48795
This commit is contained in:
Thomas Desvenain 2011-04-13 21:31:51 +00:00
parent e2d173455f
commit a7eccc3d35
2 changed files with 20 additions and 5 deletions

View File

@ -4,7 +4,8 @@ Changelog
1.0b3 - unreleased
------------------
* Plone 4+ imports (removes warnings under Plone 4.1)
* Zope 2.13 imports (removes warnings under Plone 4.1)
[thomasdesvenain]
1.0b2 - 2010-09-02
------------------

View File

@ -2,7 +2,13 @@
import os.path
import random
import re
import sha
try:
import hashlib
USE_HASHLIB = True
except ImportError:
import sha
USE_HASHLIB = False
import string
import sys
import time
@ -54,7 +60,11 @@ class Captcha(BrowserView):
def _generate_session(self):
"""Create a new session id"""
if self._session_id is None:
id = sha.new(str(random.randrange(sys.maxint))).hexdigest()
if USE_HASHLIB:
id = hashlib.sha1(str(random.randrange(sys.maxint))).hexdigest()
else:
id = sha.new(str(random.randrange(sys.maxint))).hexdigest()
self._session_id = id
self._setcookie(id)
@ -83,8 +93,12 @@ class Captcha(BrowserView):
# we're in. Indeed, every second, int(time.time()) increments by 1, so
# int(time.time() / 300) will increment by 1 every 5 minutes.
secret = getUtility(IKeyManager).secret()
seeds = [sha.new(secret + session + str(nowish)).digest(),
sha.new(secret + session + str(nowish - 1)).digest()]
if USE_HASHLIB:
seeds = [hashlib.sha1(secret + session + str(nowish)).digest(),
hashlib.sha1(secret + session + str(nowish - 1)).digest()]
else:
seeds = [sha.new(secret + session + str(nowish)).digest(),
sha.new(secret + session + str(nowish - 1)).digest()]
# The line above generates a seed based on the "nowish" of 5 minutes ago.
words = []