Secure store encryption example support code

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


#include "e32std.h"
#include "f32file.h"
#include "s32file.h"
#include "pbe.h"
#include "pbedata.h"
#include "s32crypt.h"
            

/*
 * 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;
         };
            

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();
         }
            

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);
         }
            

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;
    }