Week 23 contribution of PDK documentation content. See release notes for details. Fixes bugs Bug 2714, Bug 462.
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License
"Eclipse Public License v1.0" which accompanies this distribution,
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
Nokia Corporation - initial contribution.
Contributors:
-->
<!DOCTYPE concept
PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-38C8F8B0-C259-5B03-A13E-10DBED4071F2" xml:lang="en"><title>Signing
and verification</title><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>The following example demonstrates signing and verifying with DSA keys.
It generates a DSA key pair from the factory, signs some randomly generated
data using the private key and then verifies the signature using the public
key. </p>
<codeblock id="GUID-BFCAC8B2-D97E-59BC-8855-E5A030B9E26B" xml:space="preserve">#include <legacyselector.h>
#include <cryptosignatureapi.h>
#include <cryptokeypairgeneratorapi.h>
#include <random.h>
#include <keypair.h>
using namespace CryptoSpi;
//Get a DSA key pair generator
CKeyPairGenerator* keyPairGeneratorImpl = NULL;
TRAPD(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL
(keyPairGeneratorImpl,
KDSAKeyPairGeneratorAlgorithmUid,
NULL));
if(keyPairGeneratorImpl && (err == KErrNone))
{
CleanupStack::PushL(keyPairGeneratorImpl);
CCryptoParams* keyParameters = CCryptoParams::NewLC();
//Create a DSA key pair
CKeyPair* keyPair = NULL;
//Hardcode the keybit to 512
TRAP(err,keyPairGeneratorImpl->GenerateKeyPairL
(512,
*keyParameters,
keyPair));
if(keyPair && (err == KErrNone))
{
CleanupStack::PushL(keyPair);
//Create a pointer for the signing implementation object
CSigner* signerImpl = NULL;
//Create a signer implementation object using the private key
const CKey& privateKey = keyPair->PrivateKey();
TRAP(err,CSignatureFactory::CreateSignerL(
signerImpl,
KDsaSignerUid,
privateKey,
KPaddingModeNoneUid,
NULL));
if(!signerImpl || (err != KErrNone))
{
User::Panic(_L("Signer/Verifier"),err);
}
else
{
CleanupStack::PushL(signerImpl);
//Create the object to hold the signature
CCryptoParams* signature = CCryptoParams::NewLC();
//Generate random data; 128 bytes max length and 128 bytes long
TBuf8<128> message(128);
TRandom::RandomL(message);
//Sign the random data
//On return, second parameter contains the signature
TRAP(err,signerImpl->SignL(message, *signature));
if(err != KErrNone)
{
User::Panic(_L("Signature using private key"),err);
}
//Now verify the signature using the public key
CVerifier* verifierImpl = NULL;
const CKey& publicKey = keyPair->PublicKey();
// Create a verification object from the factory
TRAP(err,CSignatureFactory::CreateVerifierL(verifierImpl,
KDsaVerifierUid,
publicKey,
KPaddingModeNoneUid,
NULL));
if (!verifierImpl || (err != KErrNone))
{
User::Panic(_L("Signer/Verifier"),err);
}
else
{
CleanupStack::PushL(verifierImpl);
TBool verifyResult = EFalse;
//Verify the signature using the public key
verifierImpl->VerifyL(message, *signature, verifyResult);
if (verifyResult == EFalse)
{
User::Panic(_L("Signer/Verifier"),err);
}
CleanupStack::PopAndDestroy(verifierImpl);
}
CleanupStack::PopAndDestroy(signature);
CleanupStack::PopAndDestroy(signerImpl);
}
CleanupStack::PopAndDestroy(keyPair);
}
CleanupStack::PopAndDestroy(keyParameters);
CleanupStack::PopAndDestroy(keyPairGeneratorImpl);
}</codeblock>
</conbody></concept>