Performing hash oprations

PKIF can be used to perform hash operations using the SHA1, SHA256, SHA384, SHA512, or MD5 algorithms. SHA256, SHA384, SHA512 algorithms are only supported when using NSS as crypto provider. The following sample demonstrates hash generation using SHA1.

Supported Languages

C++

void PerformingHashOperations()
{
      //Create a mediator using MakeDefaultMediator
      IPKIFMediatorPtr m = MakeDefaultMediator(); 

      //Get a pointer to the IPKIFCryptoRawOperations interface
      IPKIFCryptoMisc* cRaw = m->GetMediator<IPKIFCryptoMisc>(); 

      //Create a buffer for the hash result (alg enums can be used to size buffers)
      unsigned char hashResult[SHA1];
      unsigned char expectedHash[] = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
      int hashResultLen = SHA1; 

      //create a hash context for the desired algorithm
      IPKIFHashContext* hi = cRaw->HashInit(SHA1); 

      //pass some data via HashUpdate (this call can be invoked repeatedly to
      //hash large amounts of data)
      cRaw->HashUpdate(hi, (unsigned char*)"abc", 3); 

      //finalize the hash and retrieve the result
      cRaw->HashFinal(hi, hashResult, &hashResultLen); 

      //clean up the hash context
      delete hi; 

      cout << endl;
      if(0 == memcmp(hashResult, expectedHash, SHA1))
            cout << "Successfully generated correct hash value" << endl;
      else
            cout << "Failed to generate correct hash value" << endl;
}

 

C#

public void PerformingHashOperations()
{
    //Create a mediator using MakeDefaultMediator
    IPKIFColleaguePtr m = pkif_module.MakeDefaultMediator(); 

    //Get a pointer to the IPKIFCryptoRawOperations interface
    IPKIFCryptoMisc cRaw = pkif_module.Get_IPKIFCryptoMisc(m); 

    //Create a buffer for the hash result (alg enums can be used to size buffers)
    byte[] hashResult = new byte[20];
    byte[] expectedHash = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
    SWIGTYPE_p_int hashResultLen = pkif_module.Make_int(20);
 

    //create a hash context for the desired algorithm
    IPKIFHashContext hi = cRaw.HashInit(HASH_ALG.SHA1);
 

    //pass some data via HashUpdate (this call can be invoked repeatedly to
    //hash large amounts of data)
    cRaw.HashUpdate(hi, pkif_module.StringToBuffer("abc").GetBuffer(), 3);

    //finalize the hash and retrieve the result
 
  cRaw.HashFinal(hi, hashResult, hashResultLen);
 

    //clean up the hash context
    pkif_module.Delete_IPKIFHashContext(hi); 

    Console.WriteLine();
    if (HighLevelSamples.CompareArrays(hashResult, expectedHash))
        Console.WriteLine("Successfully generated correct hash value");
    else
        Console.WriteLine("Failed to generate correct hash value");
}

JAVA

public void PerformingHashOperations()
{
   
//Create a mediator using MakeDefaultMediator
    IPKIFColleaguePtr m = pkif_module.MakeDefaultMediator(); 

    //Get a pointer to the IPKIFCryptoRawOperations interface
    IPKIFCryptoMisc cRaw = pkif_module.Get_IPKIFCryptoMisc(m); 

    //Create a buffer for the hash result (alg enums can be used to size buffers)
    byte[] hashResult = new byte[20];
   
int[] expectedHash = {0xA9,0x99,0x3E,0x36,0x47,0x06,0x81,0x6A,0xBA,0x3E,0x25,0x71,0x78,0x50,0xC2,0x6C,0x9C,0xD0,0xD8,0x9D};
    SWIGTYPE_p_int hashResultLen = pkif_module.Make_int(20);
 

    //create a hash context for the desired algorithm
    IPKIFHashContext hi = cRaw.HashInit(HASH_ALG.SHA1); 

    //pass some data via HashUpdate (this call can be invoked repeatedly to
    //hash large amounts of data)
    cRaw.HashUpdate(hi, pkif_module.StringToBuffer("abc").GetBuffer(), 3); 

    //finalize the hash and retrieve the result
    cRaw.HashFinal(hi, hashResult, hashResultLen); 

    //clean up the hash context
    pkif_module.Delete_IPKIFHashContext(hi); 

    System.out.println();
   
if (HighLevelSamples.CompareArrays(hashResult, IntArrayToByteArray(expectedHash)))
        System.
out.println("Successfully generated correct hash value");
   
else
        System.out.println("Failed to generate correct hash value");
}