Use hashlib module in favor of sha and md5.

This commit is contained in:
Stephan Erb 2009-01-08 16:51:26 +00:00
parent cca10e6d29
commit e00f871b26
5 changed files with 23 additions and 32 deletions

View file

@ -30,10 +30,10 @@
import os import os
import base64 import base64
import sha
import socket import socket
import sys import sys
import operator import operator
import hashlib
from time import (altzone, daylight, gmtime, localtime, mktime, strftime, from time import (altzone, daylight, gmtime, localtime, mktime, strftime,
time as time_time, timezone, tzname) time as time_time, timezone, tzname)
@ -1049,7 +1049,7 @@ class ConnectionVcard:
photo = vcard['PHOTO']['BINVAL'] photo = vcard['PHOTO']['BINVAL']
photo_decoded = base64.decodestring(photo) photo_decoded = base64.decodestring(photo)
gajim.interface.save_avatar_files(our_jid, photo_decoded) 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) iq2.getTag('PHOTO').setTagData('SHA', avatar_sha)
else: else:
gajim.interface.remove_avatar_files(our_jid) gajim.interface.remove_avatar_files(our_jid)
@ -1205,7 +1205,7 @@ class ConnectionVcard:
photo = vcard['PHOTO']['BINVAL'] photo = vcard['PHOTO']['BINVAL']
try: try:
photo_decoded = base64.decodestring(photo) photo_decoded = base64.decodestring(photo)
avatar_sha = sha.sha(photo_decoded).hexdigest() avatar_sha = hashlib.sha1(photo_decoded).hexdigest()
except Exception: except Exception:
avatar_sha = '' avatar_sha = ''
else: else:

View file

@ -38,6 +38,8 @@ import errno
import select import select
import base64 import base64
import sys import sys
import hashlib
from encodings.punycode import punycode_encode from encodings.punycode import punycode_encode
import gajim import gajim
@ -45,18 +47,6 @@ from i18n import Q_
from i18n import ngettext from i18n import ngettext
import xmpp 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: try:
from osx import nsapp from osx import nsapp
except ImportError: except ImportError:
@ -731,7 +721,7 @@ def get_jid_from_iq(iq_obj):
def get_auth_sha(sid, initiator, target): def get_auth_sha(sid, initiator, target):
''' return sha of sid + initiator + target used for proxy auth''' ''' 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 = { distro_info = {
@ -847,7 +837,7 @@ def sanitize_filename(filename):
latin characters, and is not too long (in that case hash it)''' latin characters, and is not too long (in that case hash it)'''
# 48 is the limit # 48 is the limit
if len(filename) > 48: if len(filename) > 48:
hash = hash_md5(filename) hash = hashlib.md5(filename)
filename = base64.b64encode(hash.digest()) filename = base64.b64encode(hash.digest())
filename = punycode_encode(filename) # make it latin chars only 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 S += '%s<' % value
if hash_method == 'sha-1': if hash_method == 'sha-1':
hash_ = hash_sha1(S) hash_ = hashlib.sha1(S)
elif hash_method == 'md5': elif hash_method == 'md5':
hash_ = hash_md5(S) hash_ = hashlib.md5(S)
else: else:
return '' return ''
return base64.b64encode(hash_.digest()) return base64.b64encode(hash_.digest())

View file

@ -25,7 +25,7 @@
import socket import socket
import struct import struct
import sha import hashlib
import os import os
from errno import EWOULDBLOCK from errno import EWOULDBLOCK
@ -645,9 +645,10 @@ class Socks5:
''' get sha of sid + Initiator jid + Target jid ''' ''' get sha of sid + Initiator jid + Target jid '''
if 'is_a_proxy' in self.file_props: if 'is_a_proxy' in self.file_props:
del(self.file_props['is_a_proxy']) 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() 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() hexdigest()
class Socks5Sender(Socks5, IdleObject): class Socks5Sender(Socks5, IdleObject):

View file

@ -22,18 +22,17 @@ See client_nb.py
from protocol import NS_SASL, NS_SESSION, NS_STREAMS, NS_BIND, NS_AUTH from protocol import NS_SASL, NS_SESSION, NS_STREAMS, NS_BIND, NS_AUTH
from protocol import Node, NodeProcessed, isResultNode, Iq, Protocol, JID from protocol import Node, NodeProcessed, isResultNode, Iq, Protocol, JID
from client import PlugIn from client import PlugIn
import sha
import base64 import base64
import random import random
import itertools import itertools
import dispatcher_nb import dispatcher_nb
import md5 import hashlib
import logging import logging
log = logging.getLogger('gajim.c.x.auth_nb') log = logging.getLogger('gajim.c.x.auth_nb')
def HH(some): return md5.new(some).hexdigest() def HH(some): return hashlib.md5(some).hexdigest()
def H(some): return md5.new(some).digest() def H(some): return hashlib.md5(some).digest()
def C(some): return ':'.join(some) def C(some): return ':'.join(some)
try: try:
@ -372,8 +371,8 @@ class NonBlockingNonSASL(PlugIn):
if query.getTag('digest'): if query.getTag('digest'):
log.info("Performing digest authentication") log.info("Performing digest authentication")
query.setTagData('digest', query.setTagData('digest',
sha.new(self.owner.Dispatcher.Stream._document_attrs['id'] + hashlib.sha1(self.owner.Dispatcher.Stream._document_attrs['id']
self.password).hexdigest()) + self.password).hexdigest())
if query.getTag('password'): if query.getTag('password'):
query.delChild('password') query.delChild('password')
self._method = 'digest' self._method = 'digest'
@ -383,7 +382,7 @@ class NonBlockingNonSASL(PlugIn):
log.info("Performing zero-k authentication") log.info("Performing zero-k authentication")
def hasher(s): def hasher(s):
return sha.new(s).hexdigest() return hashlib.sha1(s).hexdigest()
def hash_n_times(s, count): def hash_n_times(s, count):
return count and hasher(hash_n_times(s, count-1)) or s return count and hasher(hash_n_times(s, count-1)) or s

View file

@ -18,7 +18,8 @@
## along with Gajim. If not, see <http://www.gnu.org/licenses/>. ## 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,\ from transports_nb import NonBlockingTransport, NonBlockingHTTPBOSH,\
CONNECTED, CONNECTING, DISCONNECTED, DISCONNECTING,\ CONNECTED, CONNECTING, DISCONNECTED, DISCONNECTING,\
urisplit, DISCONNECT_TIMEOUT_SECONDS urisplit, DISCONNECT_TIMEOUT_SECONDS
@ -527,10 +528,10 @@ class KeyStack():
def reset(self): def reset(self):
seed = str(get_rand_number()) seed = str(get_rand_number())
self.keys = [sha.new(seed).hexdigest()] self.keys = [sha1(seed).hexdigest()]
for i in range(self.count-1): for i in range(self.count-1):
curr_seed = self.keys[i] curr_seed = self.keys[i]
self.keys.append(sha.new(curr_seed).hexdigest()) self.keys.append(sha1(curr_seed).hexdigest())
def get(self): def get(self):
if self.first_call: if self.first_call: