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) 2003-2005, Jeremiah Fincher
# Copyright (c) 2008, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -27,8 +28,6 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
import md5
import sha
import types import types
import supybot.utils as utils 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 http://www.rsasecurity.com/rsalabs/faq/3-6-6.html for more information
about md5. about md5.
""" """
irc.reply(md5.md5(text).hexdigest()) irc.reply(utils.crypt.md5(text).hexdigest())
md5 = wrap(md5, ['text']) md5 = wrap(md5, ['text'])
def sha(self, irc, msg, args, 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 http://www.secure-hash-algorithm-md5-sha-1.co.uk/ for more information
about SHA. about SHA.
""" """
irc.reply(sha.sha(text).hexdigest()) irc.reply(utils.crypt.sha(text).hexdigest())
sha = wrap(sha, ['text']) sha = wrap(sha, ['text'])
Class = String Class = String

View File

@ -1,5 +1,6 @@
### ###
# Copyright (c) 2002-2005, Jeremiah Fincher # Copyright (c) 2002-2005, Jeremiah Fincher
# Copyright (c) 2008, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -54,7 +55,7 @@ def force(x):
else: else:
return x return x
__builtins__['force'] = force __builtins__['force'] = force
if sys.version_info < (2, 4, 0): if sys.version_info < (2, 4, 0):
def reversed(L): def reversed(L):
"""Iterates through a sequence in reverse.""" """Iterates through a sequence in reverse."""
@ -97,11 +98,12 @@ if sys.version_info < (2, 4, 0):
# __builtins__ appropriately. # __builtins__ appropriately.
from gen import * from gen import *
import net import net
import web
import seq import seq
import str import str
import web
import file import file
import iter import iter
import crypt
import error import error
import python import python
import transaction 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) 2002-2005, Jeremiah Fincher
# Copyright (c) 2008, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -28,14 +29,15 @@
### ###
import os import os
import md5
import sha
import time import time
import random import random
import shutil import shutil
import os.path import os.path
from iter import ifilter from iter import ifilter
import crypt
def contents(filename): def contents(filename):
return file(filename).read() return file(filename).read()
@ -61,7 +63,7 @@ def copy(src, dst):
srcfd = file(src) srcfd = file(src)
dstfd = open(dst, 'wb') dstfd = open(dst, 'wb')
shutil.copyfileobj(srcfd, dstfd) shutil.copyfileobj(srcfd, dstfd)
def writeLine(fd, line): def writeLine(fd, line):
fd.write(line) fd.write(line)
if not line.endswith('\n'): if not line.endswith('\n'):
@ -77,11 +79,11 @@ def readLines(filename):
def touch(filename): def touch(filename):
fd = file(filename, 'w') fd = file(filename, 'w')
fd.close() fd.close()
def mktemp(suffix=''): def mktemp(suffix=''):
"""Gives a decent random string, suitable for a filename.""" """Gives a decent random string, suitable for a filename."""
r = random.Random() r = random.Random()
m = md5.md5(suffix) m = crypt.md5(suffix)
r.seed(time.time()) r.seed(time.time())
s = str(r.getstate()) s = str(r.getstate())
period = random.random() period = random.random()
@ -93,7 +95,7 @@ def mktemp(suffix=''):
m.update(s) m.update(s)
m.update(str(now)) m.update(str(now))
s = m.hexdigest() s = m.hexdigest()
return sha.sha(s + str(time.time())).hexdigest() + suffix return crypt.sha(s + str(time.time())).hexdigest() + suffix
def nonCommentLines(fd): def nonCommentLines(fd):
for line in fd: for line in fd:
@ -190,7 +192,7 @@ class AtomicFile(file):
fd = file(self.filename, 'a') fd = file(self.filename, 'a')
fd.close() fd.close()
shutil.move(self.tempFilename, self.filename) shutil.move(self.tempFilename, self.filename)
else: else:
raise ValueError, 'AtomicFile.close called after rollback.' raise ValueError, 'AtomicFile.close called after rollback.'

View File

@ -1,5 +1,6 @@
### ###
# Copyright (c) 2002-2005, Jeremiah Fincher # Copyright (c) 2002-2005, Jeremiah Fincher
# Copyright (c) 2008, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -29,9 +30,7 @@
import os import os
import sys import sys
import md5
import new import new
import sha
import time import time
import types import types
import compiler import compiler
@ -43,6 +42,8 @@ from str import format
from file import mktemp from file import mktemp
from iter import imap, all from iter import imap, all
import crypt
def abbrev(strings, d=None): def abbrev(strings, d=None):
"""Returns a dictionary mapping unambiguous abbreviations to full forms.""" """Returns a dictionary mapping unambiguous abbreviations to full forms."""
def eachSubstring(s): def eachSubstring(s):
@ -133,9 +134,9 @@ def saltHash(password, salt=None, hash='sha'):
if salt is None: if salt is None:
salt = mktemp()[:8] salt = mktemp()[:8]
if hash == 'sha': if hash == 'sha':
hasher = sha.sha hasher = crypt.sha
elif hash == 'md5': elif hash == 'md5':
hasher = md5.md5 hasher = crypt.md5
return '|'.join([salt, hasher(salt + password).hexdigest()]) return '|'.join([salt, hasher(salt + password).hexdigest()])
def safeEval(s, namespace={'True': True, 'False': False, 'None': None}): def safeEval(s, namespace={'True': True, 'False': False, 'None': None}):