Creating Keys

This section explains how to create a key using the keystore.

Before you create a key, you must know which keystore the new key must be stored in. See Retrieving Keystores for more information.

The following steps explain the process of creating a key:


  1. Create a file system session using an RFs object.

  2. Create an object of type CUnifiedKeyStore using CUnifiedKeyStore::NewL() or CUnifiedKeyStore::NewLC().

  3. Initialise the member functions and keystore using the asynchronous function CUnifiedKeyStore::Initialize().

  4. Create a key using CUnifiedKeyStore::CreateKey().

A new key is created and stored in the selected keystore.

Example

The following code snippet shows how to set a file system session object, initialise keystore and member functions, and create a key.

//Create a file system session object
RFs iFs;
CleanupClosePushL(&iFs);


//Initialise the keystore and member functions
CUnifiedKeyStore* keyStore = CUnifiedKeyStore::NewL(iFs);
keyStore->Initialize(iStatus); //iStatus is a TRequestStatus object


//Initialise the arguments to be passed into the CreateKey() function
TKeyUsagePKCS15 usage = EPKCS15UsageSign | EPKCS15UsageSignRecover;
TUint keysize = 512;
_LIT(KLabel,”keylabel”);
CCTKeyInfo::EKeyAlgorithm algorithm = CCTKeyInfo::EDSA;
TInt accessType = CCTKeyInfo::EExtractable;    
TTime startDate, endDate;
startDate.HomeTime(); 
endDate.HomeTime();


// Create a pointer for data returned by CreateKey()
CCTKeyInfo* keyInfo;

//Create the key
keyStore->CreateKey(0, usage, keysize, KLabel, algorithm,
accessType, startDate, endDate, keyInfo, iStatus);


//Clean up
CleanupStack::PopAndDestroy(); // iFs
Related concepts
Unified Keystore