remove configuration dependency from hash

This commit is contained in:
Martin Zimmermann 2014-06-25 17:39:36 +02:00
parent d386590e57
commit 65caa7ad99
3 changed files with 11 additions and 19 deletions

View File

@ -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)

View File

@ -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")

View File

@ -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)