Add utils/crypt.py as a Python version-agnostic crypt module

This commit is contained in:
James Vega 2008-12-08 20:38:38 +00:00
parent d72b696739
commit 828d41e37d
5 changed files with 60 additions and 17 deletions

View File

@ -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

View File

@ -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
@ -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

39
src/utils/crypt.py Normal file
View File

@ -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:

View File

@ -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()
@ -81,7 +83,7 @@ def touch(filename):
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:

View File

@ -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}):