Skip to main content

Hashing Data in C

Hashing Data in C

The Hash() function is used to hash a single data. The usage of Hash() is straightforward as shown in the following example. Notice that the use of this function does not require a connection to a key service.


Code Samples

See the C environment settings for details on setting the project environment.


< c_hash.c>
/****************************************************************************************
* c_hash.c
*
*
*****************************************************************************************/

#include <stdio.h>
#include <string.h>
#include "teagent_c.h"

void PrintError(const char * msg)
{
char szErr[1024];
uint32_t nLength = 1024;
uint32_t nCode = GetLastTEError(szErr, &nLength);
printf("%s Code = 0x%x, Message = %s\n", msg, nCode, szErr);
}

int main()
{
char szData[] = "This is a test string";
char szSalt[] = "this is our secret";
char szHashed[30];
unsigned long length;
TE_RETCODE ret;

/* Initialize the TE environment */
TEEnvInit();

printf("Source Data: %s\n", szData);
printf("Salt : %s\n", szSalt);

/* Hash data */
length = sizeof(szHashed); /* set buffer size for hashed value */
ret = Hash(szData, strlen(szData), szHashed, &length, szSalt, strlen(szSalt));

if(ret)
{
PrintError("Hash error:");
}
else
{
printf("Return value = %d\n", ret);
printf("Length of hash value = %d\n", length);
if(sizeof(szHashed) > length) {
szHashed[length] = '\0'; /* terminate hashed value at the right position */
printf("Hashed value : %s\n", szHashed);
}
}

/* Uninitialize the TE environment */
TEEnvClose();

return 0;
}
Output
Source Data: This is a test string
Salt : this is our secret
Return value = 0
Length of hash value = 28
Hashed value : h1S1Tcdvrj9UUGEpua/Kd6oZIKc=