diff -r 51a74ef9ed63 -r ae94777fff8f Symbian3/SDK/Source/GUID-7D53E323-CF8D-5C4D-ABCD-4D95C7A4A5B5.dita --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/SDK/Source/GUID-7D53E323-CF8D-5C4D-ABCD-4D95C7A4A5B5.dita Fri Jun 11 12:39:03 2010 +0100 @@ -0,0 +1,111 @@ + + + + + +Secure store encryption example support code

Below is the supporting code for the secure store example code.

<codeblock id="GUID-36F57493-4F6E-559A-B51B-7261F57AF1B1" xml:space="preserve"> +#include "e32std.h" +#include "f32file.h" +#include "s32file.h" +#include "pbe.h" +#include "pbedata.h" +#include "s32crypt.h" + </codeblock> <codeblock id="GUID-EE52D935-4E72-5D4A-809C-BA0FEE19C90F" xml:space="preserve"> +/* + * Class to demonstrate the use of secure store APIs. + */ +class CSecureStoreExample : public CBase + { +public: + static CSecureStoreExample* NewLC(const TDesC& aFilename); + virtual ~CSecureStoreExample(); + + void CreateNewStoreL(const TDesC& aPassword); + TStreamId WriteEncryptedDataL(const TDesC8& aInput); + void OpenExistingStoreL(const TDesC& aPassword); + HBufC8* ReadEncryptedDataLC(TStreamId aStreamId); + void CloseStore(); + void ChangePasswordL(const TDesC& aNewPassword); +private: + CSecureStoreExample(const TDesC& aFilename); + void ConstructL(); +private: + RFs iFs; + const TDesC& iFilename; + CPermanentFileStore* iFileStore; + CPBEncryptSet* iEncryptSet; + CSecureStore* iSecureStore; + }; + </codeblock> <codeblock id="GUID-DB3DBE48-DCEC-5CDD-9066-246AD37A5C12" xml:space="preserve"> +CSecureStoreExample* CSecureStoreExample::NewLC(const TDesC& aFilename) + { + CSecureStoreExample* self = new (ELeave) CSecureStoreExample(aFilename); + CleanupStack::PushL(self); + self->ConstructL(); + return self; + } + +CSecureStoreExample::CSecureStoreExample(const TDesC& aFilename) + : iFilename(aFilename) + { + } + +void CSecureStoreExample::ConstructL() + { + User::LeaveIfError(iFs.Connect()); + } + +CSecureStoreExample::~CSecureStoreExample() + { + CloseStore(); + iFs.Close(); + } + </codeblock> <codeblock id="GUID-4485993E-0561-5688-85FA-3914AECDF12B" xml:space="preserve"> +LOCAL_D void RunPBEExampleL() + { + _LIT(KFilename, "c:\\pbe_example_data.dat"); + _LIT8(KInputData, "This is the data to be encrypted."); + _LIT(KPassword, "pa55w0rd"); + _LIT(KNewPassword, "chang3m3"); + + CSecureStoreExample* main = CSecureStoreExample::NewLC(KFilename); + + // Create a secure store and write some data to it + main->CreateNewStoreL(KPassword); + TStreamId streamId = main->WriteEncryptedDataL(KInputData); + main->CloseStore(); + + // Open it again, and read the data back out + main->OpenExistingStoreL(KPassword); + HBufC8* outputData = main->ReadEncryptedDataLC(streamId); + ASSERT(*outputData == KInputData); + CleanupStack::PopAndDestroy(outputData); + + // Change the password + main->ChangePasswordL(KNewPassword); + + // Check we can still read the data + outputData = main->ReadEncryptedDataLC(streamId); + ASSERT(*outputData == KInputData); + main->CloseStore(); + + CleanupStack::PopAndDestroy(2, main); + } + </codeblock> <codeblock id="GUID-7FF91AD3-106A-56EA-9199-0E261B8D044C" xml:space="preserve"> +GLDEF_C TInt E32Main() // main function called by E32 + { + __UHEAP_MARK; + CTrapCleanup* cleanup = CTrapCleanup::New(); // get clean-up stack + TRAPD(error, RunPBEExampleL()); + __ASSERT_ALWAYS(!error,User::Panic(_L("pbe_example_code"),error)); + delete cleanup; // destroy clean-up stack + __UHEAP_MARKEND; + return 0; + } + </codeblock> </section> </conbody></concept> \ No newline at end of file