소스 검색

Add more testing to new crypto_engine

This commit makes the new crypto_engine a little
bit more compatible with the old crypto engine
oz123 10 년 전
부모
커밋
7c04fd47f6
2개의 변경된 파일32개의 추가작업 그리고 3개의 파일을 삭제
  1. 14 0
      pwman/tests/test_crypto_engine.py
  2. 18 3
      pwman/util/crypto_engine.py

+ 14 - 0
pwman/tests/test_crypto_engine.py

@@ -4,6 +4,7 @@ import os
 from pwman.util.crypto_engine import (write_password, save_a_secret_message,
                                       read_a_secret_message,
                                       CryptoEngine)
+import time
 
 # set cls_timout to negative number (e.g. -1) to disable
 default_config = {'Global': {'umask': '0100', 'colors': 'yes',
@@ -34,6 +35,7 @@ class CryptoEngineTest(unittest.TestCase):
 
     def test_d_get_crypto(self):
         ce = CryptoEngine.get()
+
         secret2 = ce.changepassword(reader=give_key)
         secret1 = ce.changepassword(reader=give_key)
         # althouth the same secret key is used,
@@ -44,4 +46,16 @@ class CryptoEngineTest(unittest.TestCase):
 
     def test_e_authenticate(self):
         ce = CryptoEngine.get()
+        self.assertFalse(ce.authenticate('verywrong'))
         self.assertTrue(ce.authenticate('verysecretkey'))
+        self.assertTrue(ce._is_authenticated())
+
+    def test_is_timedout(self):
+        ce = CryptoEngine.get()
+        ce._timeout = 1
+        time.sleep(1.1)
+        self.assertTrue(ce._is_timedout())
+        self.assertIsNone(ce._cipher)
+        self.assertFalse(ce._is_authenticated())
+        #:self.assertFalse(ce._is_timedout())
+

+ 18 - 3
pwman/util/crypto_engine.py

@@ -24,7 +24,7 @@ import base64
 import os
 import sys
 import binascii
-
+import time
 from pwman.util.callback import Callback
 import pwman.util.config as config
 
@@ -152,7 +152,6 @@ class CryptoEngine(object):  # pagma: no cover
         if CryptoEngine._instance_new:
             return CryptoEngine._instance_new
 
-        keycrypted = config.get_value("Encryption", "keycrypted")
         algo = config.get_value("Encryption", "algorithm")
 
         if not algo:
@@ -188,7 +187,23 @@ class CryptoEngine(object):  # pagma: no cover
         salt and digest are stored in a file or a database
         """
         dig = get_digest(password, self._salt)
-        return binascii.hexlify(dig) == self._digest
+        if binascii.hexlify(dig) == self._digest:
+            CryptoEngine._timeoutcount = time.time()
+            self._cipher = get_cipher(password, self._salt)
+            return True
+        return False
+
+    def _is_authenticated(self):
+        if not self._is_timedout() and self._cipher is not None:
+            return True
+        return False
+
+    def _is_timedout(self):
+        if self._timeout > 0:
+            if (time.time() - CryptoEngine._timeoutcount) > self._timeout:
+                self._cipher = None
+            return True
+        return False
 
     def changepassword(self, reader=raw_input):
         self._keycrypted = self._create_password(reader=reader)