調用TSPI中的hash函數散列測試
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <tss/tss_error.h>
#include <tss/platform.h>
#include <tss/tss_defines.h>
#include <tss/tss_typedef.h>
#include <tss/tss_structs.h>
#include <tss/tspi.h>
#include <trousers/trousers.h>
#define Debug(message, tResult) printf("%s : %s\n", message, (char *)Trspi_Error_String(result))
int main()
{
TSS_HCONTEXT hContext; //上下文
int i;
TSS_RESULT result;
printf("#########程序開始運行:###########\n");
/**********上下文及其初始化連接*********/
//選擇你正在與之通信的TPM,默認情況下是系統TPM(用NULL表示)
result = Tspi_Context_Create(&hContext);
Debug(" 1-Create Context",result);
result = Tspi_Context_Connect(hContext,NULL);
Debug(" 2-Context Connect", result);
/*==================================*/
/*********散列對象*****************/
TSS_HHASH hHash;
BYTE *digest, *data = "DATA TO HASH";
UINT32 digestLen;
//創建散列值對象
Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_HASH, TSS_HASH_SHA1, &hHash);
//將數據散列,因爲對象類型是 TSS_HASH_SHA1,所以TSS知道用SHA-1算法進行散列
Tspi_Hash_UpdateHashValue(hHash, strlen(data), data);
//取回散列值對象的摘要
Tspi_Hash_GetHashValue(hHash, &digestLen, &digest);
//輸出被散列的源數據對象
printf("----被散列的源數據: \n");
for (i = 0; i < strlen(data); i++)
printf("%02x", *(data + i));
//輸出被散列後的hash值
printf("\n----被散列後的hash值: \n");
for (i = 0; i < digestLen; i++)
printf("%02x", *(digest + i));
/*==================================*/
/*********釋放資源************/
//清理上下文對象
Tspi_Context_FreeMemory(hContext, NULL);
Tspi_Context_Close(hContext);
printf("\n######釋放資源成功,程序結束。#########\n");
return 0;
}