From 828d41e37d3b8e452992017d422edf88918b26e2 Mon Sep 17 00:00:00 2001 From: James Vega Date: Mon, 8 Dec 2008 20:38:38 +0000 Subject: [PATCH] Add utils/crypt.py as a Python version-agnostic crypt module --- plugins/String/plugin.py | 7 +++---- src/utils/__init__.py | 6 ++++-- src/utils/crypt.py | 39 +++++++++++++++++++++++++++++++++++++++ src/utils/file.py | 16 +++++++++------- src/utils/gen.py | 9 +++++---- 5 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/utils/crypt.py diff --git a/plugins/String/plugin.py b/plugins/String/plugin.py index 937b27cde..bfebd73f0 100644 --- a/plugins/String/plugin.py +++ b/plugins/String/plugin.py @@ -1,5 +1,6 @@ ### # Copyright (c) 2003-2005, Jeremiah Fincher +# Copyright (c) 2008, James Vega # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -27,8 +28,6 @@ # POSSIBILITY OF SUCH DAMAGE. ### -import md5 -import sha import types import supybot.utils as utils @@ -156,7 +155,7 @@ class String(callbacks.Plugin): http://www.rsasecurity.com/rsalabs/faq/3-6-6.html for more information about md5. """ - irc.reply(md5.md5(text).hexdigest()) + irc.reply(utils.crypt.md5(text).hexdigest()) md5 = wrap(md5, ['text']) def sha(self, irc, msg, args, text): @@ -166,7 +165,7 @@ class String(callbacks.Plugin): http://www.secure-hash-algorithm-md5-sha-1.co.uk/ for more information about SHA. """ - irc.reply(sha.sha(text).hexdigest()) + irc.reply(utils.crypt.sha(text).hexdigest()) sha = wrap(sha, ['text']) Class = String diff --git a/src/utils/__init__.py b/src/utils/__init__.py index ddc0d3de6..7de7a425d 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,5 +1,6 @@ ### # Copyright (c) 2002-2005, Jeremiah Fincher +# Copyright (c) 2008, James Vega # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -54,7 +55,7 @@ def force(x): else: return x __builtins__['force'] = force - + if sys.version_info < (2, 4, 0): def reversed(L): """Iterates through a sequence in reverse.""" @@ -97,11 +98,12 @@ if sys.version_info < (2, 4, 0): # __builtins__ appropriately. from gen import * import net -import web import seq import str +import web import file import iter +import crypt import error import python import transaction diff --git a/src/utils/crypt.py b/src/utils/crypt.py new file mode 100644 index 000000000..bfbb9ad4c --- /dev/null +++ b/src/utils/crypt.py @@ -0,0 +1,39 @@ +### +# Copyright (c) 2008, James Vega +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions, and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions, and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the author of this software nor the name of +# contributors to this software may be used to endorse or promote products +# derived from this software without specific prior written consent. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +### + +import sys + +if sys.version_info < (2, 5, 0): + from md5 import md5 + from sha import sha +else: + from hashlib import md5 + from hashlib import sha1 as sha + +# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/src/utils/file.py b/src/utils/file.py index bbd8b64ed..3043079e5 100644 --- a/src/utils/file.py +++ b/src/utils/file.py @@ -1,5 +1,6 @@ ### # Copyright (c) 2002-2005, Jeremiah Fincher +# Copyright (c) 2008, James Vega # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -28,14 +29,15 @@ ### import os -import md5 -import sha import time import random import shutil import os.path + from iter import ifilter +import crypt + def contents(filename): return file(filename).read() @@ -61,7 +63,7 @@ def copy(src, dst): srcfd = file(src) dstfd = open(dst, 'wb') shutil.copyfileobj(srcfd, dstfd) - + def writeLine(fd, line): fd.write(line) if not line.endswith('\n'): @@ -77,11 +79,11 @@ def readLines(filename): def touch(filename): fd = file(filename, 'w') fd.close() - + def mktemp(suffix=''): """Gives a decent random string, suitable for a filename.""" r = random.Random() - m = md5.md5(suffix) + m = crypt.md5(suffix) r.seed(time.time()) s = str(r.getstate()) period = random.random() @@ -93,7 +95,7 @@ def mktemp(suffix=''): m.update(s) m.update(str(now)) s = m.hexdigest() - return sha.sha(s + str(time.time())).hexdigest() + suffix + return crypt.sha(s + str(time.time())).hexdigest() + suffix def nonCommentLines(fd): for line in fd: @@ -190,7 +192,7 @@ class AtomicFile(file): fd = file(self.filename, 'a') fd.close() shutil.move(self.tempFilename, self.filename) - + else: raise ValueError, 'AtomicFile.close called after rollback.' diff --git a/src/utils/gen.py b/src/utils/gen.py index 020af90d0..bcdd3997e 100644 --- a/src/utils/gen.py +++ b/src/utils/gen.py @@ -1,5 +1,6 @@ ### # Copyright (c) 2002-2005, Jeremiah Fincher +# Copyright (c) 2008, James Vega # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -29,9 +30,7 @@ import os import sys -import md5 import new -import sha import time import types import compiler @@ -43,6 +42,8 @@ from str import format from file import mktemp from iter import imap, all +import crypt + def abbrev(strings, d=None): """Returns a dictionary mapping unambiguous abbreviations to full forms.""" def eachSubstring(s): @@ -133,9 +134,9 @@ def saltHash(password, salt=None, hash='sha'): if salt is None: salt = mktemp()[:8] if hash == 'sha': - hasher = sha.sha + hasher = crypt.sha elif hash == 'md5': - hasher = md5.md5 + hasher = crypt.md5 return '|'.join([salt, hasher(salt + password).hexdigest()]) def safeEval(s, namespace={'True': True, 'False': False, 'None': None}):