From e00f871b2602b20485b56efe7f6095ddbe182f16 Mon Sep 17 00:00:00 2001 From: Stephan Erb <steve-e@h3c.de> Date: Thu, 8 Jan 2009 16:51:26 +0000 Subject: [PATCH] Use hashlib module in favor of sha and md5. --- src/common/connection_handlers.py | 6 +++--- src/common/helpers.py | 22 ++++++---------------- src/common/socks5.py | 7 ++++--- src/common/xmpp/auth_nb.py | 13 ++++++------- src/common/xmpp/bosh.py | 7 ++++--- 5 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py index b1fba52baf..488d3e2349 100644 --- a/src/common/connection_handlers.py +++ b/src/common/connection_handlers.py @@ -30,10 +30,10 @@ import os import base64 -import sha import socket import sys import operator +import hashlib from time import (altzone, daylight, gmtime, localtime, mktime, strftime, time as time_time, timezone, tzname) @@ -1049,7 +1049,7 @@ class ConnectionVcard: photo = vcard['PHOTO']['BINVAL'] photo_decoded = base64.decodestring(photo) gajim.interface.save_avatar_files(our_jid, photo_decoded) - avatar_sha = sha.sha(photo_decoded).hexdigest() + avatar_sha = hashlib.sha1(photo_decoded).hexdigest() iq2.getTag('PHOTO').setTagData('SHA', avatar_sha) else: gajim.interface.remove_avatar_files(our_jid) @@ -1205,7 +1205,7 @@ class ConnectionVcard: photo = vcard['PHOTO']['BINVAL'] try: photo_decoded = base64.decodestring(photo) - avatar_sha = sha.sha(photo_decoded).hexdigest() + avatar_sha = hashlib.sha1(photo_decoded).hexdigest() except Exception: avatar_sha = '' else: diff --git a/src/common/helpers.py b/src/common/helpers.py index c9f4d0ea08..66244597a8 100644 --- a/src/common/helpers.py +++ b/src/common/helpers.py @@ -38,6 +38,8 @@ import errno import select import base64 import sys +import hashlib + from encodings.punycode import punycode_encode import gajim @@ -45,18 +47,6 @@ from i18n import Q_ from i18n import ngettext import xmpp -try: - # Python 2.5 - import hashlib - hash_md5 = hashlib.md5 - hash_sha1 = hashlib.sha1 -except ImportError: - # Python 2.4 - import md5 - import sha - hash_md5 = md5.new - hash_sha1 = sha.new - try: from osx import nsapp except ImportError: @@ -731,7 +721,7 @@ def get_jid_from_iq(iq_obj): def get_auth_sha(sid, initiator, target): ''' return sha of sid + initiator + target used for proxy auth''' - return hash_sha1("%s%s%s" % (sid, initiator, target)).hexdigest() + return hashlib.sha1("%s%s%s" % (sid, initiator, target)).hexdigest() distro_info = { @@ -847,7 +837,7 @@ def sanitize_filename(filename): latin characters, and is not too long (in that case hash it)''' # 48 is the limit if len(filename) > 48: - hash = hash_md5(filename) + hash = hashlib.md5(filename) filename = base64.b64encode(hash.digest()) filename = punycode_encode(filename) # make it latin chars only @@ -1250,9 +1240,9 @@ def compute_caps_hash(identities, features, dataforms=[], hash_method='sha-1'): S += '%s<' % value if hash_method == 'sha-1': - hash_ = hash_sha1(S) + hash_ = hashlib.sha1(S) elif hash_method == 'md5': - hash_ = hash_md5(S) + hash_ = hashlib.md5(S) else: return '' return base64.b64encode(hash_.digest()) diff --git a/src/common/socks5.py b/src/common/socks5.py index 7c284a2e63..60c7559b35 100644 --- a/src/common/socks5.py +++ b/src/common/socks5.py @@ -25,7 +25,7 @@ import socket import struct -import sha +import hashlib import os from errno import EWOULDBLOCK @@ -645,9 +645,10 @@ class Socks5: ''' get sha of sid + Initiator jid + Target jid ''' if 'is_a_proxy' in self.file_props: del(self.file_props['is_a_proxy']) - return sha.new('%s%s%s' % (self.sid, self.file_props['proxy_sender'], + return hashlib.sha1('%s%s%s' % (self.sid, + self.file_props['proxy_sender'], self.file_props['proxy_receiver'])).hexdigest() - return sha.new('%s%s%s' % (self.sid, self.initiator, self.target)).\ + return hashlib.sha1('%s%s%s' % (self.sid, self.initiator, self.target)).\ hexdigest() class Socks5Sender(Socks5, IdleObject): diff --git a/src/common/xmpp/auth_nb.py b/src/common/xmpp/auth_nb.py index 0210a9adb8..02bafe01fe 100644 --- a/src/common/xmpp/auth_nb.py +++ b/src/common/xmpp/auth_nb.py @@ -22,18 +22,17 @@ See client_nb.py from protocol import NS_SASL, NS_SESSION, NS_STREAMS, NS_BIND, NS_AUTH from protocol import Node, NodeProcessed, isResultNode, Iq, Protocol, JID from client import PlugIn -import sha import base64 import random import itertools import dispatcher_nb -import md5 +import hashlib import logging log = logging.getLogger('gajim.c.x.auth_nb') -def HH(some): return md5.new(some).hexdigest() -def H(some): return md5.new(some).digest() +def HH(some): return hashlib.md5(some).hexdigest() +def H(some): return hashlib.md5(some).digest() def C(some): return ':'.join(some) try: @@ -372,8 +371,8 @@ class NonBlockingNonSASL(PlugIn): if query.getTag('digest'): log.info("Performing digest authentication") query.setTagData('digest', - sha.new(self.owner.Dispatcher.Stream._document_attrs['id'] + - self.password).hexdigest()) + hashlib.sha1(self.owner.Dispatcher.Stream._document_attrs['id'] + + self.password).hexdigest()) if query.getTag('password'): query.delChild('password') self._method = 'digest' @@ -383,7 +382,7 @@ class NonBlockingNonSASL(PlugIn): log.info("Performing zero-k authentication") def hasher(s): - return sha.new(s).hexdigest() + return hashlib.sha1(s).hexdigest() def hash_n_times(s, count): return count and hasher(hash_n_times(s, count-1)) or s diff --git a/src/common/xmpp/bosh.py b/src/common/xmpp/bosh.py index ab42c02ad0..227702cd4b 100644 --- a/src/common/xmpp/bosh.py +++ b/src/common/xmpp/bosh.py @@ -18,7 +18,8 @@ ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. -import locale, random, sha +import locale, random +from hashlib import sha1 from transports_nb import NonBlockingTransport, NonBlockingHTTPBOSH,\ CONNECTED, CONNECTING, DISCONNECTED, DISCONNECTING,\ urisplit, DISCONNECT_TIMEOUT_SECONDS @@ -527,10 +528,10 @@ class KeyStack(): def reset(self): seed = str(get_rand_number()) - self.keys = [sha.new(seed).hexdigest()] + self.keys = [sha1(seed).hexdigest()] for i in range(self.count-1): curr_seed = self.keys[i] - self.keys.append(sha.new(curr_seed).hexdigest()) + self.keys.append(sha1(curr_seed).hexdigest()) def get(self): if self.first_call: -- GitLab