This tutorial explains how to generate random bytes using
the CRandom
API.
Create a CRandom
object.
Use the GenerateRandomBytesL() function to generate cryptographically secure random data. A call to this function fills the provided buffer up to its current length, discarding any data that it may contain.
This example shows how the CRandom::GenerateRandomBytesL()
method is used to populate a descriptor with a series of random
bytes.
... using namespace CryptoSpi; //Create a pointer to store the factory-generated random implementation object CRandom* randomImpl = NULL; //Calling the CreateRandomL() method of the random factory class //creates a new CRandom object CRandomFactory::CreateRandomL(randomImpl, KRandomUid, NULL); CleanupStack::PushL(randomImpl); //Create an 8 bit descriptor 50 bytes long with a max length of 50 bytes TBuf8<50> randomStr(50); //Passing the 8 bit descriptor to the CRandom::GenerateRandomBytesL() //method fills it with random bytes. If there is no memory available //or any problems occur, the method may leave. randomImpl->GenerateRandomBytesL(randomStr); //Destroy the random implementation object CleanupStack::PopAndDestroy(randomImpl);