From 0219b08b465365a02b7948c4405a3759486cfc95 Mon Sep 17 00:00:00 2001 From: Michael Barton Date: Wed, 27 Feb 2013 15:26:27 -0800 Subject: [PATCH] simplify the chexor function Replace all that map(operator) nonsense. It changes the error raised on invalid hashes, but we don't handle that anywhere, and it shouldn't ever happen in real life. Change-Id: Ib8cb549fac05e0b2725b4ea295326ac0c5e1f035 --- swift/common/db.py | 8 ++------ test/unit/common/test_db.py | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/swift/common/db.py b/swift/common/db.py index 1f70c89285..0c8e7ea3fc 100644 --- a/swift/common/db.py +++ b/swift/common/db.py @@ -19,7 +19,6 @@ from __future__ import with_statement from contextlib import contextmanager import hashlib import logging -import operator import os from uuid import uuid4 import sys @@ -112,11 +111,8 @@ def chexor(old, name, timestamp): """ if name is None: raise Exception('name is None!') - old = old.decode('hex') - new = hashlib.md5(('%s-%s' % (name, timestamp)).encode('utf_8')).digest() - response = ''.join( - map(chr, map(operator.xor, map(ord, old), map(ord, new)))) - return response.encode('hex') + new = hashlib.md5(('%s-%s' % (name, timestamp)).encode('utf8')).hexdigest() + return '%032x' % (int(old, 16) ^ int(new, 16)) def get_db_connection(path, timeout=30, okay_to_create=False): diff --git a/test/unit/common/test_db.py b/test/unit/common/test_db.py index ce9b1f4df7..e4e3843c9a 100644 --- a/test/unit/common/test_db.py +++ b/test/unit/common/test_db.py @@ -71,7 +71,7 @@ class TestChexor(unittest.TestCase): '4f2ea31ac14d4273fe32ba08062b21de') def test_invalid_old_hash(self): - self.assertRaises(TypeError, chexor, 'oldhash', 'name', + self.assertRaises(ValueError, chexor, 'oldhash', 'name', normalize_timestamp(1)) def test_no_name(self):