import datetime, time
import string
import random
-import hashlib
from email.mime.text import MIMEText
import email.utils
def __init__(self, algorithm = 'sha1', *args, **kwargs):
self.algorithm = algorithm
+ try:
+ import hashlib
+ self.hashlib = True
+ except ImportError:
+ self.hashlib = False
+ if algorithm == 'sha1':
+ import sha
+ self.hash_constructor = sha.new
+ elif algorithm == 'md5':
+ import md5
+ self.hash_constructor = md5.new
+ else:
+ raise NameError("Unknown algorithm '%s'" % algorithm)
+
super(HashField, self).__init__(*args, **kwargs)
def db_type(self):
- n_bytes = len(hashlib.new(self.algorithm).digest())
+ if self.hashlib:
+ n_bytes = len(hashlib.new(self.algorithm).digest())
+ else:
+ n_bytes = len(self.hash_constructor().digest())
if settings.DATABASE_ENGINE == 'postgresql':
return 'bytea'
elif settings.DATABASE_ENGINE == 'mysql':
import re
-import hashlib
+try:
+ import hashlib
+ sha1_hash = hashlib.sha1
+except ImportError:
+ import sha
+ sha1_hash = sha.sha
_hunk_re = re.compile('^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
_filename_re = re.compile('^(---|\+\+\+) (\S+)')
lines = str.split('\n')
prefixes = ['-', '+', ' ']
- hash = hashlib.sha1()
+ hash = sha1_hash()
for line in str.split('\n'):