Selaa lähdekoodia

Add loadkey and savekey

oz123 10 vuotta sitten
vanhempi
commit
922034b578
3 muutettua tiedostoa jossa 24 lisäystä ja 2 poistoa
  1. 17 0
      pwman/data/drivers/sqlite.py
  2. 5 0
      pwman/tests/test_sqlite.py
  3. 2 2
      pwman/util/crypto_engine.py

+ 17 - 0
pwman/data/drivers/sqlite.py

@@ -503,6 +503,23 @@ class SQLite(SQLiteDatabaseNewForm):
         self._cur.execute(clean)
         self._con.commit()
 
+    def savekey(self, key):
+        salt, digest = key.split('$6$')
+        sql = "INSERT INTO CRYPTO(SEED, DIGEST) VALUES(?,?)"
+        self._cur.execute("DELETE FROM CRYPTO")
+        self._cur.execute(sql, (salt, digest))
+        self._digest = digest.encode('utf-8')
+        self._salt = salt.encode('utf-8')
+
+    def loadkey(self):
+        # TODO: rename this method!
+        """
+        return _keycrypted
+        """
+        sql = "SELECT * FROM CRYPTO"
+        seed, digest = self._cur.execute(sql).fetchone()
+        return seed + u'$6$' + digest
+
     def close(self):
         self._clean_orphands()
         self._cur.close()

+ 5 - 0
pwman/tests/test_sqlite.py

@@ -144,6 +144,11 @@ class TestSQLite(unittest.TestCase):
         rv = self.db._cur.execute("select * from node").fetchall()
         self.assertListEqual(rv, [])
 
+    def test_a12_test_savekey(self):
+        ce = CryptoEngine.get()
+        self.db.savekey(ce.get_cryptedkey())
+        self.assertEqual(ce.get_cryptedkey(), self.db.loadkey())
+
     def tearDown(self):
         self.db.close()
 

+ 2 - 2
pwman/util/crypto_engine.py

@@ -176,7 +176,7 @@ class CryptoEngine(object):  # pagma: no cover
     def changepassword(self, reader=raw_input):
         if self._callback is None:
             raise CryptoException("No callback class has been specified")
-        #if not self._is_authenticated():
+        # if not self._is_authenticated():
         #    p, s = self._auth()
         # if you change the password of the database you have to Change
         # all the cipher texts in the databse!!!
@@ -224,4 +224,4 @@ class CryptoEngine(object):  # pagma: no cover
         """
         return _keycrypted
         """
-        return self._salt + '$6$' + self._digest
+        return self._salt.decode() + u'$6$' + self._digest.decode()