Sfoglia il codice sorgente

More cleanup of CryptoEngine

oz123 10 anni fa
parent
commit
36c685ee4c
2 ha cambiato i file con 16 aggiunte e 15 eliminazioni
  1. 3 1
      pwman/tests/crypto_tests.py
  2. 13 14
      pwman/util/crypto.py

+ 3 - 1
pwman/tests/crypto_tests.py

@@ -20,8 +20,10 @@ import unittest
 class CryptoTest(unittest.TestCase):
 
     def test_no_algorithm(self):
+        CryptoEngine._instance_new = None
         config.set_value('Encryption', 'algorithm', '')
-        self.assertRaises((CryptoException,), CryptoEngine)
+        self.assertRaises((CryptoException,), CryptoEngine.get)
+        config.set_value('Encryption', 'algorithm', 'AES')
 
     def test_getcipher(self):
         crypto = CryptoEngine()

+ 13 - 14
pwman/util/crypto.py

@@ -137,22 +137,29 @@ class CryptoEngine(object):
         Return an instance of CryptoEngine.
         If no instance is found, a CryptoException is raised.
         """
-        keycrypted = config.get_value("Encryption", "keycrypted")
 
         if CryptoEngine._instance:
             return CryptoEngine._instance
         if CryptoEngine._instance_new:
             return CryptoEngine._instance_new
 
+        keycrypted = config.get_value("Encryption", "keycrypted")
+        algo = config.get_value("Encryption", "algorithm")
+
+        if not algo:
+            raise CryptoException("Parameters missing, no algorithm given")
+
         if dbver < 0.5:
-            CryptoEngine._instance = CryptoEngineOld(keycrypted=keycrypted)
+            CryptoEngine._instance = CryptoEngineOld(keycrypted=keycrypted,
+                                                     algorithm=algo)
             return CryptoEngine._instance
 
         if dbver >= 0.5:
-            CryptoEngine._instance_new = CryptoEngine()
+            CryptoEngine._instance_new = CryptoEngine(keycrypted=keycrypted,
+                                                      algorithm=algo)
             return CryptoEngine._instance_new
 
-    def __init__(self, keycrypted=None):
+    def __init__(self, keycrypted=None, algorithm='AES'):
         """Initialise the Cryptographic Engine
 
         params is a dictionary. Valid keys are:
@@ -163,16 +170,8 @@ class CryptoEngine(object):
                              Default is -1 (disabled).
         """
         algo = config.get_value("Encryption", "algorithm")
-        if algo:
-            self._algo = algo
-        else:
-            raise CryptoException("Parameters missing, no algorithm given")
-
-        keycrypted = config.get_value("Encryption", "keycrypted")
-        if keycrypted:
-            self._keycrypted = keycrypted
-        else:
-            self._keycrypted = None
+        self._algo = algo if algo else None
+        self._keycrypted = keycrypted if keycrypted else None
 
         timeout = config.get_value("Encryption", "timeout")
         if timeout.isdigit():