Symbian3/SDK/Source/GUID-7D53E323-CF8D-5C4D-ABCD-4D95C7A4A5B5.dita
changeset 8 ae94777fff8f
equal deleted inserted replaced
7:51a74ef9ed63 8:ae94777fff8f
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept xml:lang="en" id="GUID-7D53E323-CF8D-5C4D-ABCD-4D95C7A4A5B5"><title>Secure store encryption example support code</title><prolog><metadata><keywords/></metadata></prolog><conbody><p>Below is the supporting code for the <xref href="GUID-36BCDD1F-3713-5DF0-8D8A-CF093694B636.dita">secure store example code</xref>. </p> <section id="GUID-5DE9A95D-C755-429A-A3E3-7A8FF06008B9"><title/><codeblock id="GUID-36F57493-4F6E-559A-B51B-7261F57AF1B1" xml:space="preserve">
       
    13 #include "e32std.h"
       
    14 #include "f32file.h"
       
    15 #include "s32file.h"
       
    16 #include "pbe.h"
       
    17 #include "pbedata.h"
       
    18 #include "s32crypt.h"
       
    19             </codeblock> <codeblock id="GUID-EE52D935-4E72-5D4A-809C-BA0FEE19C90F" xml:space="preserve">
       
    20 /*
       
    21  * Class to demonstrate the use of secure store APIs.
       
    22  */
       
    23 class CSecureStoreExample : public CBase
       
    24          {
       
    25 public:
       
    26          static CSecureStoreExample* NewLC(const TDesC&amp; aFilename);
       
    27          virtual ~CSecureStoreExample();
       
    28 
       
    29          void CreateNewStoreL(const TDesC&amp; aPassword);
       
    30          TStreamId WriteEncryptedDataL(const TDesC8&amp; aInput);
       
    31          void OpenExistingStoreL(const TDesC&amp; aPassword);
       
    32          HBufC8* ReadEncryptedDataLC(TStreamId aStreamId);
       
    33          void CloseStore();
       
    34          void ChangePasswordL(const TDesC&amp; aNewPassword);
       
    35 private:
       
    36          CSecureStoreExample(const TDesC&amp; aFilename);
       
    37          void ConstructL();
       
    38 private:
       
    39          RFs iFs;
       
    40          const TDesC&amp; iFilename;
       
    41          CPermanentFileStore* iFileStore;
       
    42          CPBEncryptSet* iEncryptSet;
       
    43          CSecureStore* iSecureStore;
       
    44          };
       
    45             </codeblock> <codeblock id="GUID-DB3DBE48-DCEC-5CDD-9066-246AD37A5C12" xml:space="preserve">
       
    46 CSecureStoreExample* CSecureStoreExample::NewLC(const TDesC&amp; aFilename)
       
    47          {
       
    48          CSecureStoreExample* self = new (ELeave) CSecureStoreExample(aFilename);
       
    49          CleanupStack::PushL(self);
       
    50          self-&gt;ConstructL();
       
    51          return self;
       
    52          }
       
    53 
       
    54 CSecureStoreExample::CSecureStoreExample(const TDesC&amp; aFilename)
       
    55          : iFilename(aFilename)
       
    56          {
       
    57          }
       
    58 
       
    59 void CSecureStoreExample::ConstructL()
       
    60          {
       
    61          User::LeaveIfError(iFs.Connect());
       
    62          }
       
    63 
       
    64 CSecureStoreExample::~CSecureStoreExample()
       
    65          {
       
    66          CloseStore();
       
    67          iFs.Close();
       
    68          }
       
    69             </codeblock> <codeblock id="GUID-4485993E-0561-5688-85FA-3914AECDF12B" xml:space="preserve">
       
    70 LOCAL_D void RunPBEExampleL()
       
    71          {
       
    72          _LIT(KFilename, "c:\\pbe_example_data.dat");
       
    73          _LIT8(KInputData, "This is the data to be encrypted.");
       
    74          _LIT(KPassword, "pa55w0rd");
       
    75          _LIT(KNewPassword, "chang3m3");
       
    76 
       
    77          CSecureStoreExample* main = CSecureStoreExample::NewLC(KFilename);
       
    78 
       
    79          // Create a secure store and write some data to it
       
    80          main-&gt;CreateNewStoreL(KPassword);
       
    81          TStreamId streamId = main-&gt;WriteEncryptedDataL(KInputData);
       
    82          main-&gt;CloseStore();
       
    83 
       
    84          // Open it again, and read the data back out
       
    85          main-&gt;OpenExistingStoreL(KPassword);
       
    86          HBufC8* outputData = main-&gt;ReadEncryptedDataLC(streamId);
       
    87          ASSERT(*outputData == KInputData);
       
    88          CleanupStack::PopAndDestroy(outputData);
       
    89 
       
    90          // Change the password
       
    91          main-&gt;ChangePasswordL(KNewPassword);
       
    92 
       
    93          // Check we can still read the data
       
    94          outputData = main-&gt;ReadEncryptedDataLC(streamId);
       
    95          ASSERT(*outputData == KInputData);         
       
    96          main-&gt;CloseStore();
       
    97 
       
    98          CleanupStack::PopAndDestroy(2, main);
       
    99          }
       
   100             </codeblock> <codeblock id="GUID-7FF91AD3-106A-56EA-9199-0E261B8D044C" xml:space="preserve">
       
   101 GLDEF_C TInt E32Main() // main function called by E32
       
   102     {
       
   103          __UHEAP_MARK;
       
   104          CTrapCleanup* cleanup = CTrapCleanup::New(); // get clean-up stack
       
   105          TRAPD(error, RunPBEExampleL());
       
   106          __ASSERT_ALWAYS(!error,User::Panic(_L("pbe_example_code"),error));
       
   107          delete cleanup; // destroy clean-up stack
       
   108          __UHEAP_MARKEND;
       
   109          return 0;
       
   110     }
       
   111             </codeblock> </section> </conbody></concept>