Symbian3/SDK/Source/GUID-38C8F8B0-C259-5B03-A13E-10DBED4071F2.dita
changeset 8 ae94777fff8f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Symbian3/SDK/Source/GUID-38C8F8B0-C259-5B03-A13E-10DBED4071F2.dita	Fri Jun 11 12:39:03 2010 +0100
@@ -0,0 +1,131 @@
+<?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 &lt;legacyselector.h&gt;
+#include &lt;cryptosignatureapi.h&gt;
+#include &lt;cryptokeypairgeneratorapi.h&gt;
+#include &lt;random.h&gt;
+#include &lt;keypair.h&gt;
+
+using namespace CryptoSpi;
+
+//Get a DSA key pair generator
+CKeyPairGenerator* keyPairGeneratorImpl = NULL;
+
+TRAPD(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL
+        (keyPairGeneratorImpl,
+        KDSAKeyPairGeneratorAlgorithmUid,
+        NULL));
+
+if(keyPairGeneratorImpl &amp;&amp; (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-&gt;GenerateKeyPairL
+        (512,
+        *keyParameters,
+        keyPair));
+            
+    if(keyPair &amp;&amp; (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&amp; privateKey = keyPair-&gt;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&lt;128&gt; message(128); 
+            TRandom::RandomL(message);
+
+            //Sign the random data
+            //On return, second parameter contains the signature
+            TRAP(err,signerImpl-&gt;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&amp; publicKey = keyPair-&gt;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-&gt;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>
\ No newline at end of file