SOL9 Sample: Signature

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * Signature.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2009/03/03 

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

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


void _tmain(int argc, TCHAR** argv)
{
  int rc = 0;

  try {
    // Specify CryptServiceProvider of keycontainer name.

    CryptServiceProvider csp(_T("MY"));
    {
      printf("\nSignature:\n");

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

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

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

      Bytes signedBytes;
      if (signature.sign(signedBytes) ==NO_ERROR) {
        printf("5. OK, signature.sign()\n");
        signedBytes.dump();
      } else {
        printf("6. Failed in signature.sign()\n");
        cryptError();
      }


      SignatureKey signatureKey(csp);
      // 
      printf("\n7. Try to export a publicKey from signatureKey\n");    

      Bytes pubKey;
      if (signatureKey.getPublicKey(pubKey) == NO_ERROR) {
        printf("8. OK, signatureKey.getPublicKey():");
        pubKey.dump();
      } else {
        printf("9. Failed in signatureKey.getPublicKey()\n");
        cryptError();
      }

      printf("\n10. Try to impport publicKey\n");    

      CryptKey impPublicKey;
      if (impPublicKey.importKey(csp.getProviderHandle(), pubKey) ==NO_ERROR) {
        printf("11. OK, imported publicKey\n");    
      } else {
        printf("12. Failed in impPublicKey.import()\n");
        cryptError();
      }

      // 
      printf("\n13. Try to verify a signature by importedPublicKey\n");        
      if (signature.verify(impPublicKey.getKeyHandle(), signedBytes) == NO_ERROR) {
        printf("11. OK signature.verify():");
      }  else {
        printf("14. Failed in signature.verify()\n");
        cryptError();
      }
    }

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

}

Last modified: 2 May 2016

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