Symbian3/SDK/Source/GUID-38C8F8B0-C259-5B03-A13E-10DBED4071F2.dita
changeset 8 ae94777fff8f
equal deleted inserted replaced
7:51a74ef9ed63 8:ae94777fff8f
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-38C8F8B0-C259-5B03-A13E-10DBED4071F2" xml:lang="en"><title>Signing
       
    13 and verification</title><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>The following example demonstrates signing and verifying with DSA keys.
       
    15 It generates a DSA key pair from the factory, signs some randomly generated
       
    16 data using the private key and then verifies the signature using the public
       
    17 key. </p>
       
    18 <codeblock id="GUID-BFCAC8B2-D97E-59BC-8855-E5A030B9E26B" xml:space="preserve">#include &lt;legacyselector.h&gt;
       
    19 #include &lt;cryptosignatureapi.h&gt;
       
    20 #include &lt;cryptokeypairgeneratorapi.h&gt;
       
    21 #include &lt;random.h&gt;
       
    22 #include &lt;keypair.h&gt;
       
    23 
       
    24 using namespace CryptoSpi;
       
    25 
       
    26 //Get a DSA key pair generator
       
    27 CKeyPairGenerator* keyPairGeneratorImpl = NULL;
       
    28 
       
    29 TRAPD(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL
       
    30         (keyPairGeneratorImpl,
       
    31         KDSAKeyPairGeneratorAlgorithmUid,
       
    32         NULL));
       
    33 
       
    34 if(keyPairGeneratorImpl &amp;&amp; (err == KErrNone))
       
    35     {
       
    36     CleanupStack::PushL(keyPairGeneratorImpl);
       
    37     CCryptoParams* keyParameters = CCryptoParams::NewLC();
       
    38 
       
    39     //Create a DSA key pair
       
    40     CKeyPair* keyPair = NULL;
       
    41                 
       
    42     //Hardcode the keybit to 512
       
    43     TRAP(err,keyPairGeneratorImpl-&gt;GenerateKeyPairL
       
    44         (512,
       
    45         *keyParameters,
       
    46         keyPair));
       
    47             
       
    48     if(keyPair &amp;&amp; (err == KErrNone))
       
    49         {
       
    50         CleanupStack::PushL(keyPair);
       
    51                 
       
    52         //Create a pointer for the signing implementation object
       
    53         CSigner* signerImpl = NULL;
       
    54                 
       
    55         //Create a signer implementation object using the private key
       
    56         const CKey&amp; privateKey = keyPair-&gt;PrivateKey();
       
    57         TRAP(err,CSignatureFactory::CreateSignerL(
       
    58              signerImpl,
       
    59              KDsaSignerUid,
       
    60              privateKey,
       
    61              KPaddingModeNoneUid,
       
    62              NULL));
       
    63                 
       
    64         if(!signerImpl || (err != KErrNone))
       
    65             {
       
    66             User::Panic(_L("Signer/Verifier"),err);
       
    67             }
       
    68         else
       
    69             {
       
    70             CleanupStack::PushL(signerImpl);
       
    71                 
       
    72             //Create the object to hold the signature
       
    73             CCryptoParams* signature = CCryptoParams::NewLC();
       
    74 
       
    75             //Generate random data; 128 bytes max length and 128 bytes long
       
    76             TBuf8&lt;128&gt; message(128); 
       
    77             TRandom::RandomL(message);
       
    78 
       
    79             //Sign the random data
       
    80             //On return, second parameter contains the signature
       
    81             TRAP(err,signerImpl-&gt;SignL(message, *signature));
       
    82             
       
    83             if(err != KErrNone)
       
    84                 {
       
    85                 User::Panic(_L("Signature using private key"),err);
       
    86                 }
       
    87 
       
    88             //Now verify the signature using the public key
       
    89             CVerifier* verifierImpl = NULL;
       
    90                     
       
    91             const CKey&amp; publicKey = keyPair-&gt;PublicKey();
       
    92 
       
    93             // Create a verification object from the factory
       
    94             TRAP(err,CSignatureFactory::CreateVerifierL(verifierImpl,
       
    95                  KDsaVerifierUid,
       
    96                  publicKey,
       
    97                  KPaddingModeNoneUid,
       
    98                  NULL));
       
    99                         
       
   100             if (!verifierImpl || (err != KErrNone))
       
   101                 {
       
   102                 User::Panic(_L("Signer/Verifier"),err);
       
   103                 }
       
   104             else
       
   105                 {
       
   106                 CleanupStack::PushL(verifierImpl);
       
   107 
       
   108                 TBool verifyResult = EFalse;    
       
   109                         
       
   110                 //Verify the signature using the public key
       
   111                 verifierImpl-&gt;VerifyL(message, *signature, verifyResult);
       
   112 
       
   113                 if (verifyResult == EFalse)
       
   114                     {
       
   115                     User::Panic(_L("Signer/Verifier"),err);
       
   116                     }
       
   117                             
       
   118                 CleanupStack::PopAndDestroy(verifierImpl);
       
   119                 }
       
   120                         
       
   121             CleanupStack::PopAndDestroy(signature);
       
   122             CleanupStack::PopAndDestroy(signerImpl);
       
   123             }
       
   124                     
       
   125         CleanupStack::PopAndDestroy(keyPair);                    
       
   126         }
       
   127             
       
   128     CleanupStack::PopAndDestroy(keyParameters);    
       
   129     CleanupStack::PopAndDestroy(keyPairGeneratorImpl);    
       
   130     }</codeblock>
       
   131 </conbody></concept>