crypto/weakcrypto/docs/secure_stream_example_code.dox
changeset 0 2c201484c85f
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /**
       
     2 	@page secure_stream_example_code Secure stream example code
       
     3 	
       
     4 	This example covers: 
       
     5 	-# @ref stream_example_1 "Encrypting some data with a password and writing it to a file"
       
     6 	-# @ref stream_example_2 "Reading the data back from the file and decrypting it with the same password".
       
     7 
       
     8 	Also, see:
       
     9 	- @ref pbe_example_code
       
    10 	- @ref secure_store_example_code
       
    11 
       
    12 <hr>
       
    13 
       
    14 	@ref secure_stream_example_code_support "Supporting code for this example"
       
    15 
       
    16 @anchor stream_example_1
       
    17 @code
       
    18 /*
       
    19  * Encrypt data with password and write it to a file.
       
    20  */
       
    21 void CSecureStreamExample::WriteEncryptedDataL(const TDesC8& aInput, const TDesC& aFilename, const TDesC& aPassword)
       
    22 		 {
       
    23 		 // Open a stream to the output file
       
    24 		 RFileWriteStream writeStream;
       
    25 		 User::LeaveIfError(writeStream.Replace(iFs, aFilename, EFileShareExclusive | EFileWrite));
       
    26 		 CleanupClosePushL(writeStream);
       
    27 
       
    28 		 // Create a CPBEncryptElement object, passing details of the encryption we
       
    29 		 // are using and the user's password
       
    30 		 CPBEncryptElement* encryption = CPBEncryptElement::NewLC(aPassword, ECipherDES_CBC);
       
    31 
       
    32 		 // Store encryption data as the first thing in the stream
       
    33 		 writeStream << encryption->EncryptionData();
       
    34 
       
    35 		 // Create an REncryptStream - this wraps the write stream and encrypts
       
    36 		 // everything written to it
       
    37 		 REncryptStream encStream;
       
    38 		 encStream.OpenL(writeStream, *encryption);
       
    39 		 CleanupClosePushL(encStream);
       
    40 
       
    41 		 // Now we can simply write the plaintext to the encrypt stream - it gets
       
    42 		 // encrypted automatically
       
    43 		 encStream << aInput;
       
    44 
       
    45 		 // Commit the stream
       
    46 		 encStream.CommitL();
       
    47 
       
    48 		 // Free memory (writeStream, encryption, encStream)
       
    49 		 CleanupStack::PopAndDestroy(3, &writeStream);
       
    50 		 }
       
    51 @endcode
       
    52 
       
    53 @anchor stream_example_2
       
    54 @code
       
    55 /*
       
    56  * Read data from file and decrypt it.
       
    57  */
       
    58 HBufC8* CSecureStreamExample::ReadEncryptedDataLC(const TDesC& aFilename, const TDesC& aPassword)
       
    59 		 {
       
    60 		 // Open a stream to the input file
       
    61 		 RFileReadStream readStream;
       
    62 		 User::LeaveIfError(readStream.Open(iFs, aFilename, EFileRead));
       
    63 		 CleanupClosePushL(readStream);
       
    64 
       
    65 		 // Read the encryption data from the stream
       
    66 		 CPBEncryptionData* encryptionData = CPBEncryptionData::NewLC(readStream);
       
    67 		 
       
    68 		 // Recreate the CPBEncryptElement object, using the encryption data from the
       
    69 		 // file and the user's password
       
    70 		 CPBEncryptElement* encryption = CPBEncryptElement::NewLC(*encryptionData, aPassword);
       
    71 
       
    72 		 // Create an RDecryptStream based on the read stream - this decrypts
       
    73 		 // everything we read from it
       
    74 		 RDecryptStream decStream;
       
    75 		 decStream.OpenL(readStream, *encryption);
       
    76 		 CleanupClosePushL(decStream);
       
    77 
       
    78 		 // Now we can read the plaintext straight from the stream
       
    79 		 HBufC8* plaintext = HBufC8::NewL(decStream, KMaxTInt);
       
    80 		 
       
    81 		 // Free memory (readStream, encryptionData, encryption, decStream)
       
    82 		 CleanupStack::PopAndDestroy(4, &readStream);
       
    83 		 CleanupStack::PushL(plaintext);
       
    84 
       
    85 		 // Return plaintext to the caller
       
    86 		 return plaintext;
       
    87 		 }
       
    88 @endcode
       
    89 
       
    90 */