remove configuration dependency from hash
This commit is contained in:
parent
d386590e57
commit
65caa7ad99
@ -86,7 +86,9 @@ class Isso(object):
|
|||||||
self.db = db.SQLite3(conf.get('general', 'dbpath'), conf)
|
self.db = db.SQLite3(conf.get('general', 'dbpath'), conf)
|
||||||
self.signer = URLSafeTimedSerializer(self.db.preferences.get("session-key"))
|
self.signer = URLSafeTimedSerializer(self.db.preferences.get("session-key"))
|
||||||
self.markup = html.Markup(conf.section('markup'))
|
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)
|
super(Isso, self).__init__(conf)
|
||||||
|
|
||||||
|
@ -7,8 +7,6 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from isso import config
|
|
||||||
|
|
||||||
from isso.compat import PY2K, string_types
|
from isso.compat import PY2K, string_types
|
||||||
from isso.utils.hash import Hash, PBKDF2, new
|
from isso.utils.hash import Hash, PBKDF2, new
|
||||||
|
|
||||||
@ -49,25 +47,16 @@ class TestCreate(unittest.TestCase):
|
|||||||
|
|
||||||
def test_custom(self):
|
def test_custom(self):
|
||||||
|
|
||||||
def _new(val):
|
sha1 = new("sha1")
|
||||||
conf = config.new({
|
|
||||||
"hash": {
|
|
||||||
"algorithm": val,
|
|
||||||
"salt": ""
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return new(conf.section("hash"))
|
|
||||||
|
|
||||||
sha1 = _new("sha1")
|
|
||||||
self.assertIsInstance(sha1, Hash)
|
self.assertIsInstance(sha1, Hash)
|
||||||
self.assertEqual(sha1.func, "sha1")
|
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.assertIsInstance(pbkdf2, PBKDF2)
|
||||||
self.assertEqual(pbkdf2.iterations, 16)
|
self.assertEqual(pbkdf2.iterations, 16)
|
||||||
|
|
||||||
pbkdf2 = _new("pbkdf2:16:2:md5")
|
pbkdf2 = new("pbkdf2:16:2:md5")
|
||||||
self.assertIsInstance(pbkdf2, PBKDF2)
|
self.assertIsInstance(pbkdf2, PBKDF2)
|
||||||
self.assertEqual(pbkdf2.dklen, 2)
|
self.assertEqual(pbkdf2.dklen, 2)
|
||||||
self.assertEqual(pbkdf2.func, "md5")
|
self.assertEqual(pbkdf2.func, "md5")
|
@ -85,13 +85,14 @@ class PBKDF2(Hash):
|
|||||||
return pbkdf2(val, self.salt, self.iterations, self.dklen, self.func)
|
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
|
"""Factory to create hash functions from configuration section. If an
|
||||||
algorithm takes custom parameters, you can separate them by a colon like
|
algorithm takes custom parameters, you can separate them by a colon like
|
||||||
this: pbkdf2:arg1:arg2:arg3."""
|
this: pbkdf2:arg1:arg2:arg3."""
|
||||||
|
|
||||||
algorithm = conf.get("algorithm")
|
if salt is None:
|
||||||
salt = conf.get("salt").encode("utf-8")
|
salt = ''
|
||||||
|
salt = salt.encode("utf-8")
|
||||||
|
|
||||||
if algorithm == "none":
|
if algorithm == "none":
|
||||||
return Hash(salt, None)
|
return Hash(salt, None)
|
||||||
|
Loading…
Reference in New Issue
Block a user