[Checkins]
SVN: z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/
Use the key in the persistent object to help with the encryption.
Stephan Richter
srichter at cosmos.phy.tufts.edu
Fri Jun 27 05:17:52 EDT 2008
Log message for revision 87824:
Use the key in the persistent object to help with the encryption.
Changed:
U z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/README.txt
U z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/encryptedpersistent.py
U z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/interfaces.py
U z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/testing.py
U z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/tests.py
-=-
Modified: z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/README.txt
===================================================================
--- z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/README.txt 2008-06-27 08:44:38 UTC (rev 87823)
+++ z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/README.txt 2008-06-27 09:17:51 UTC (rev 87824)
@@ -13,24 +13,27 @@
>>> myObj = MyObject()
>>> myObj.name = u'Stephan Richter'
-Setup
------
-We need a utility that provides IEncryption for use with the
-EncryptedPersistent object. We have defined a very simple demonstration
+The Encryption Utility
+----------------------
+
+We need a utility that provides ``IEncryption`` for use with the
+``EncryptedPersistent`` object. We have defined a very simple demonstration
class that simply adds an "encryption string" to the data in order to indicate
that it has encrypted it, and removes that string to decrypt the data:
- >>> from zope.app.testing import ztapi
- >>> from z3c.encryptedpersistent import testing, interfaces
- >>> ztapi.provideUtility(interfaces.IEncryption, testing.DemoEncrypter())
-
+ >>> import zope.component
+ >>> from z3c.encryptedpersistent import testing
+ >>> zope.component.provideUtility(testing.DemoEncrypter())
+
+En- and decrypting the Obejct State
+-----------------------------------
+
When an object is stored to a database, its ``__getstate__`` method is called:
-
>>> myObj.__getstate__()
- "ENCRYPTED_(dp1\nS'name'\np2\nVStephan Richter\np3\ns."
+ (None, "ENCRYPTED_None(dp1\nS'name'\np2\nVStephan Richter\np3\ns.")
When an object is loaded from the database, the state is passed into the
``__setstate__`` method:
@@ -42,6 +45,12 @@
>>> myObj2.name
u'Stephan Richter'
+And that's all there is to it.
+
+
+Storing in the ZODB
+-------------------
+
Let's now test this with a full database. Since we want to test, whether the
data is stored encrypted, we have to create a file:
@@ -68,5 +77,5 @@
and the data is truly encrypted in the file:
- >>> state in open(dbFile).read()
+ >>> state[1] in open(dbFile).read()
True
Modified: z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/encryptedpersistent.py
===================================================================
--- z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/encryptedpersistent.py 2008-06-27 08:44:38 UTC (rev 87823)
+++ z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/encryptedpersistent.py 2008-06-27 09:17:51 UTC (rev 87824)
@@ -36,12 +36,14 @@
# 3. Convert the state to a string.
stateStr = cPickle.dumps(state)
# 4. Encrypt the state string and return it as the state.
- return encryption.encrypt(stateStr)
+ return self.__key__, encryption.encrypt(self.__key__, stateStr)
def __setstate__(self, encryptedState):
+ # 1. Extract the key from the state first
+ key, encryptedState = encryptedState
# 2. Decrypt the state string.
encryption = zope.component.getUtility(interfaces.IEncryption)
- stateStr = encryption.decrypt(encryptedState)
+ stateStr = encryption.decrypt(key, encryptedState)
# 3. Convert the state string to the state
state = cPickle.loads(stateStr)
# 4. Set the state of the object using the Persistent implementation.
Modified: z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/interfaces.py
===================================================================
--- z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/interfaces.py 2008-06-27 08:44:38 UTC (rev 87823)
+++ z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/interfaces.py 2008-06-27 09:17:51 UTC (rev 87824)
@@ -15,19 +15,26 @@
$Id$
"""
__docformat__ = "reStructuredText"
-
import zope.interface
import zope.schema
-
class IEncryption(zope.interface.Interface):
"""Utility providing encryption mechanism"""
- def encrypt(data):
+ def encrypt(key, data):
"""Returns the encrypted data"""
- def decrypt(data):
+ def decrypt(key, data):
"""Returns the decrypted data"""
class IEncryptedPersistent(zope.interface.Interface):
- """ """
\ No newline at end of file
+ """A persistent object that encrypts its state for storage."""
+
+ __key__ = zope.schema.Field(
+ title=u'Encryption Key',
+ description=(u'Encryption key/state/hint that can be used to aid '
+ u'the encruption and decryption process. This attribute '
+ u'can be any data structure that is necessary to '
+ u'complete the task.'),
+ default=None,
+ required=False)
Modified: z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/testing.py
===================================================================
--- z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/testing.py 2008-06-27 08:44:38 UTC (rev 87823)
+++ z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/testing.py 2008-06-27 09:17:51 UTC (rev 87824)
@@ -7,17 +7,17 @@
$Id$
"""
import zope.interface
+from z3c.encryptedpersistent import interfaces
-import interfaces
-
-
class DemoEncrypter(object):
zope.interface.implements(interfaces.IEncryption)
_EncryptionString = "ENCRYPTED_"
- def encrypt(self, data):
- return self._EncryptionString + data
+ def encrypt(self, key, data):
+ """See interfaces.IEncryption"""
+ return self._EncryptionString + str(key) + data
- def decrypt(self, data):
- return data.lstrip(self._EncryptionString)
+ def decrypt(self, key, data):
+ """See interfaces.IEncryption"""
+ return data.lstrip(self._EncryptionString + str(key))
Modified: z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/tests.py
===================================================================
--- z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/tests.py 2008-06-27 08:44:38 UTC (rev 87823)
+++ z3c.encryptedpersistent/trunk/src/z3c/encryptedpersistent/tests.py 2008-06-27 09:17:51 UTC (rev 87824)
@@ -17,7 +17,7 @@
setup.setUpTestAsModule(test, 'README')
def tearDown(test):
- tearDownTestAsModule(test)
+ setup.tearDownTestAsModule(test)
placelesssetup.tearDown(test)
def test_suite():
More information about the Checkins
mailing list