IUserHash

 

     IUserHash represens a SHA-1 Hashing function object.

 

When to Implement

 

     This interface is implemented by UserCrypto.dll

    

Methods in Vtable Order

 

IUnknown methods

 

QueryInterface      Returns pointers to supported interfaces.

AddRef      Increments the reference count.

Release      Decrements the reference count.     

 

    

IUserHash Methods:

 

Clear      Reset the instance

DoHashing      Apply the hashing function to an array of data

GetResult      Get the result of the last DoHashing method

 

Method Description:

 

1:STDMETHODIMP Clear()

 

     Reset the instance

 

Return:

 

     S_OK if successful

     S_FALSE if not successful     

 

 

2:STDMETHODIMP DoHashing(unsigned char * pData, int len)

 

     Apply the hashing function to an array of data.

     You cannot seqment a long sequence of data into smaller pieces and hash each small piece one by one with this method. See the note for the technique to hash a file.

 

Return:

 

     S_OK if successful

     S_FALSE if not successful     

 

Parameters:

 

     pData: an array of data to be hashed with SHA-1.

 

     len: number of bytes in pData

 

Note:

     If you want to hash a file on the disk, follow the code segment as below. This is a very fast method.

 

void HashFile(CString m_strFile,DWORD dwResult[5])

{

   

 

   HANDLE hFile = CreateFile(

         m_strFile.GetBuffer(0), // pointer to name of the file

          GENERIC_READ, // access (read-write) mode

          FILE_SHARE_READ , // share mode

          NULL, // pointer to security attributes

            OPEN_EXISTING, // how to create

            FILE_ATTRIBUTE_NORMAL, // file attributes

            NULL // handle to file with attributes to copy

       );

    

   if(hFile == NULL) return;

 

   HANDLE hMap = CreateFileMapping(

        hFile,// handle to file to map

        NULL,// optional security attributes

        PAGE_READONLY,// protection for mapping object

        0,// high-order 32 bits of object size

        0,// low-order 32 bits of object size

        NULL // name of file-mapping object

     );

 

   if (hMap != NULL && GetLastError() == ERROR_ALREADY_EXISTS)

   {

          CloseHandle(hMap);

          hMap = NULL;

   } else

   {

 

        BYTE *pData = (BYTE *)MapViewOfFile(

             hMap, // file-mapping object to map into address space

             FILE_MAP_READ, // access mode

               0, // high-order 32 bits of file offset

                 0, // low-order 32 bits of file offset

               0// number of bytes to map

                 );

 

          if(pData != NULL)

          {

              

               m_pUserHash->DoHashing(pData,GetFileSize(hFile,0));

              

               m_pUserHash->GetResult(dwResult);

          }

 

        CloseHandle(hMap);

        hMap = NULL;

   }

    

   CloseHandle(hFile);

 }

 

 

3:STDMETHODIMP GetResult(DWORD dwResult[5])

 

     Get the result of the last DoHashing method

 

Return:

 

     S_OK if successful

     S_FALSE if not successful     

 

Parameters:

 

     dwResult a 5 element DWORD array to hold the hashing result

 

Example C++ code segment

 

     #include "UserCrypto_i.c"

     #include "UserCrypto.h"

 

     IUserHash *pUserHash;

    

     CoCreateInstance(CLSID_UserHash,

     NULL,

     CLSCTX_INPROC_SERVER,

     IID_IUserHash,

     (void **)&pUserHash);

 

     pUserHash->DoHashing("1234567890123456789012345678901234567890",40);

 

     DWORD dwResult[5];

    

     pUserHash->GetResult(dwResult); // dwResult now contains the hashing result

 

     pUserHash->Release();