Преглед на файлове

Add test for authenticate in new_crypto_engine

oz123 преди 10 години
родител
ревизия
6f229b8ecd
променени са 2 файла, в които са добавени 29 реда и са изтрити 15 реда
  1. 3 0
      pwman/tests/test_crypto_engine.py
  2. 26 15
      pwman/util/crypto_engine.py

+ 3 - 0
pwman/tests/test_crypto_engine.py

@@ -42,3 +42,6 @@ class CryptoEngineTest(unittest.TestCase):
         # CryptoEngine._get_digest
         self.assertNotEqual(secret1, secret2)
 
+    def test_e_authenticate(self):
+        ce = CryptoEngine.get()
+        self.assertTrue(ce.authenticate('verysecretkey'))

+ 26 - 15
pwman/util/crypto_engine.py

@@ -163,13 +163,37 @@ class CryptoEngine(object):  # pagma: no cover
         except ValueError:
             timeout = -1
 
-        kwargs = {'keycrypted': keycrypted, 'algorithm': algo,
-                  'timeout': timeout}
+        salt, digest = get_digest_from_file('passwords.txt')
+
+        kwargs = {'algorithm': algo,
+                  'timeout': timeout, 'salt': salt, 'digest': digest}
 
         if dbver >= 0.5:
             CryptoEngine._instance_new = CryptoEngine(**kwargs)
             return CryptoEngine._instance_new
 
+    def __init__(self, salt=None, digest=None, algorithm='AES',
+                 timeout=-1):
+        """
+        Initialise the Cryptographic Engine
+        """
+        self._algo = algorithm
+        self._digest = digest if digest else None
+        self._salt = salt if salt else None
+        self._timeout = timeout
+        self._cipher = None
+
+    def authenticate(self, password):
+        """
+        salt and digest are stored in a file or a database
+        """
+        dig = get_digest(password, self._salt)
+        return binascii.hexlify(dig) == self._digest
+
+    def changepassword(self, reader=raw_input):
+        self._keycrypted = self._create_password(reader=reader)
+        return self._keycrypted
+
     @property
     def callback(self):
         """
@@ -197,10 +221,6 @@ class CryptoEngine(object):  # pagma: no cover
         hpk = salt+'$6$'.encode('utf8')+binascii.hexlify(key)
         return hpk.decode('utf-8')
 
-    def changepassword(self, reader=raw_input):
-        self._keycrypted = self._create_password(reader=reader)
-        return self._keycrypted
-
     def _get_digest(self, password, salt):
         """
         Get a digest based on clear text password
@@ -208,15 +228,6 @@ class CryptoEngine(object):  # pagma: no cover
         iterations = 5000
         return PBKDF2(password, salt, dkLen=32, count=iterations)
 
-    def __init__(self, keycrypted=None, algorithm='AES', timeout=-1):
-        """
-        Initialise the Cryptographic Engine
-        """
-        self._algo = algorithm
-        self._keycrypted = keycrypted if keycrypted else None
-        self._timeout = timeout
-        self._cipher = None
-
 
 if __name__ == '__main__':  # pragma: no cover
     if '-i' in sys.argv: