SOL9 2.0 Sample: SignatureKey

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SignatureKey.cpp 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2009/03/05

#include <sol/crypt/CryptServiceProvider.h>
#include <sol/crypt/SignatureKey.h>
#include <sol/crypt/Signature.h>

void cryptError() 
{ 
    _tprintf(_T("%s\n", CryptError::getMessage(GetLastError()) ));
    //exit(1);
}


void  _tmain(int argc, TCHAR** argv)
{
    try {
        //Specify CryptServiceProvider having a keycontainer name.
        CryptServiceProvider csp(_T("SOL"));

        {
            printf("\nSignatureKey:\n");

            SignatureKey signatureKey(csp);

            printf("\n1. Try to get(export) publicKey from signatureKey\n"); 
            Bytes pubKey;
            if (signatureKey.getPublicKey(pubKey) == NO_ERROR) {
                printf("2. OK, signatureKey.getPublicKey()\n");
                pubKey.dump();
            } else {
                printf("3. Failed in signatureKey.getPublicKey()\n");
                cryptError();
            }

            printf("\n4. Try to export privateKey from signatureKey\n"); 
            Bytes priKey;
            if (signatureKey.getPrivateKey(priKey) == NO_ERROR) {
                printf("5. OK, signatureKey.getPrivateKey()\n");
                priKey.dump();
            } else {
                printf("6. Failed in signatureKey.getPrivateKey()\n");
                cryptError();
            }

            printf("\n7. Try to import the publicKey from exported publicKey data\n"); 
            CryptKey impPublicKey;
            if (impPublicKey.importKey(csp.getProviderHandle(), 
                pubKey.getData(), pubKey.getSize()) == NO_ERROR) {
                printf("8. OK, impPublicKey.importKey()\n");
            } else {
                printf("9. Failed in impPublicKey.importKey()\n");
                cryptError();
            }

            printf("\n10. Try to import the privateKey from exported privateKey data\n"); 
            CryptKey impPrivateKey;
            if (impPrivateKey.importKey(csp.getProviderHandle(), 
                priKey.getData(), priKey.getSize()) == NO_ERROR) {
                printf("11. OK, impPrivateKey.importKey()\n");
            } else {
                printf("12. Failed in impPrivateKey.importKey()\n");
                cryptError();
            }

            Signature signature(csp, CALG_MD5);
            
            const char* message = "Hello world! This is a SOL9 SignatureKey class sample";
            printf("\n13. Tr to hash string=[%s]\n", message);

            if (signature.hashString(message) == NO_ERROR) {
                printf("14. OK, signature.hashString()\n");
            } else {
                printf("15. Failed in signature.hashString()\n");
                cryptError();
            }

            printf("\n16. Tr to sign by signature\n");

            Bytes signedBytes;
            if (signature.sign(signedBytes) ==NO_ERROR) {
                printf("17. OK, signature.sign()\n");
                signedBytes.dump();
            } else {
                printf("18. Failed in signature.sign()\n");
                cryptError();
            }
            
            // 
            printf("\n19. Try to verify a signature by importedPublicKey\n");                
            if (signature.verify(impPublicKey.getKeyHandle(), signedBytes) == NO_ERROR) {
                printf("20. OK signature.verify()\n");
            }  else {
                printf("21. Failed in signature.verify()\n");
                cryptError();
            }
        }

    } catch (Exception& ex) {
        printf("Exception = %s\n", ex.getErrorMessage());
    }
}

Last modified: 11 Nov 2009

Copyright (c) 2009 Antillia.com ALL RIGHTS RESERVED.