diff --git a/isso/__init__.py b/isso/__init__.py index fe3b579..b83975e 100644 --- a/isso/__init__.py +++ b/isso/__init__.py @@ -86,7 +86,9 @@ class Isso(object): self.db = db.SQLite3(conf.get('general', 'dbpath'), conf) self.signer = URLSafeTimedSerializer(self.db.preferences.get("session-key")) self.markup = html.Markup(conf.section('markup')) - self.hasher = hash.new(conf.section("hash")) + self.hasher = hash.new( + conf.get("hash", "algorithm"), + conf.get("hash", "salt")) super(Isso, self).__init__(conf) diff --git a/isso/tests/test_utils_hash.py b/isso/tests/test_utils_hash.py index 626d75e..3094609 100644 --- a/isso/tests/test_utils_hash.py +++ b/isso/tests/test_utils_hash.py @@ -7,8 +7,6 @@ try: except ImportError: import unittest -from isso import config - from isso.compat import PY2K, string_types from isso.utils.hash import Hash, PBKDF2, new @@ -49,25 +47,16 @@ class TestCreate(unittest.TestCase): def test_custom(self): - def _new(val): - conf = config.new({ - "hash": { - "algorithm": val, - "salt": "" - } - }) - return new(conf.section("hash")) - - sha1 = _new("sha1") + sha1 = new("sha1") self.assertIsInstance(sha1, Hash) self.assertEqual(sha1.func, "sha1") - self.assertRaises(ValueError, _new, "foo") + self.assertRaises(ValueError, new, "foo") - pbkdf2 = _new("pbkdf2:16") + pbkdf2 = new("pbkdf2:16") self.assertIsInstance(pbkdf2, PBKDF2) self.assertEqual(pbkdf2.iterations, 16) - pbkdf2 = _new("pbkdf2:16:2:md5") + pbkdf2 = new("pbkdf2:16:2:md5") self.assertIsInstance(pbkdf2, PBKDF2) self.assertEqual(pbkdf2.dklen, 2) self.assertEqual(pbkdf2.func, "md5") \ No newline at end of file diff --git a/isso/utils/hash.py b/isso/utils/hash.py index 0e93a1a..c35f1a2 100644 --- a/isso/utils/hash.py +++ b/isso/utils/hash.py @@ -85,13 +85,14 @@ class PBKDF2(Hash): return pbkdf2(val, self.salt, self.iterations, self.dklen, self.func) -def new(conf): +def new(algorithm, salt=None): """Factory to create hash functions from configuration section. If an algorithm takes custom parameters, you can separate them by a colon like this: pbkdf2:arg1:arg2:arg3.""" - algorithm = conf.get("algorithm") - salt = conf.get("salt").encode("utf-8") + if salt is None: + salt = '' + salt = salt.encode("utf-8") if algorithm == "none": return Hash(salt, None)