API C/C++
HSM Dinamo
Loading...
Looking for...
No entries found
create_hash.c

Example of generating a hash.

See Note on examples.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <dinamo.h> /* header do Dinamo */
#define HOST_ADDR "127.0.0.1"
#define USER_ID "master"
#define USER_PWD "12345678"
#define HASH_ID ALG_SHA1
int main()
{
int nRet;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HHASHCTX hHash = NULL;
BYTE pbData[256], pbHash[20];
DWORD cbData, dwAlgId, dwHashSize;
int i;
//Inicializa as bibliotecas do Dinamo
nRet = DInitialize(0);
if (nRet){
printf("Falha na funcao: DInitialize \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Bibliotecas inicializadas.\n");
//Inicializa a estrutura para conexão com o HSM
strncpy(authPwd.szAddr, HOST_ADDR, sizeof(authPwd.szAddr));
authPwd.nPort = DEFAULT_PORT;
strncpy(authPwd.szUserId, USER_ID, sizeof(authPwd.szUserId));
strncpy(authPwd.szPassword, USER_PWD, sizeof(authPwd.szPassword));
nRet = DOpenSession(&hSession, SS_USER_PWD, (BYTE *)&authPwd, sizeof(authPwd), ENCRYPTED_CONN);
if (nRet){
printf("Falha na funcao: DOpenSession \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Sessao com o Dinamo estabelecida.\n");
//Cria um contexto de hash
nRet = DCreateHash(hSession, HASH_ID, 0, 0, &hHash);
if (nRet){
printf("Falha na funcao: DCreateHash \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Contexto de hash criado.\n");
//Preenche o buffer com dados quaisquer
cbData = sizeof(pbData);
memset(pbData, 'A', cbData);
//Adiciona dados ao hash
nRet = DHashData(hHash, pbData, cbData, 0);
if (nRet){
printf("Falha na funcao: DHashData \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Dados adicionados ao hash.\n");
cbData = sizeof(dwAlgId);
nRet = DGetHashParam(hHash, DHP_ALGID, (BYTE *)&dwAlgId, &cbData, 0);
if (nRet){
printf("Falha na funcao: DGetHashParam \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Algoritmo de hash usado: "DWORD_PRINT"\n", dwAlgId);
cbData = sizeof(dwHashSize);
nRet = DGetHashParam(hHash, DHP_HASH_SIZE, (BYTE *)&dwHashSize, &cbData, 0);
if (nRet){
printf("Falha na funcao: DGetHashParam \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Tamanho do Hash: "DWORD_PRINT"\n", dwHashSize);
cbData = sizeof(pbHash);
nRet = DGetHashParam(hHash, DHP_HASH_VALUE, pbHash, &cbData, 0);
if (nRet){
printf("Falha na funcao: DGetHashParam \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Resultado do hash: ");
for (i = 0; i < (int)cbData; i++)
printf("%02X ", pbHash[i]);
printf("\n");
clean:
if (hHash){
DDestroyHash(&hHash);
printf("Contexto de hash liberado.\n");
}
if (hSession){
DCloseSession(&hSession, 0);
printf("Sessao encerrada.\n");
}
printf("Bibliotecas finalizada.\n");
return nRet;
}
Application Programming Interface (API) do HSM Dinamo.
void * HSESSIONCTX
Definição dinamo.h:68
#define DEFAULT_PORT
Definição dinamo.h:1949
#define DHP_ALGID
Definição dinamo.h:1495
#define DWORD_PRINT
Definição dinamo.h:55
#define DHP_HASH_SIZE
Definição dinamo.h:1497
#define DHP_HASH_VALUE
Definição dinamo.h:1496
unsigned char BYTE
Definição dinamo.h:45
unsigned int DWORD
Definição dinamo.h:46
void * HHASHCTX
Definição dinamo.h:69
#define ENCRYPTED_CONN
Definição dinamo.h:585
#define SS_USER_PWD
Definição dinamo.h:576
int AAP_API DCreateHash(HSESSIONCTX hSession, int nAlgId, HKEYCTX hKey, DWORD dwFlags, HHASHCTX *hHash)
int AAP_API DDestroyHash(HHASHCTX *phHash)
int AAP_API DHashData(HHASHCTX hHash, BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
int AAP_API DGetHashParam(HHASHCTX hHash, DWORD dwParam, BYTE *pbData, DWORD *pdwDataLen, DWORD dwFlags)
int AAP_API DOpenSession(HSESSIONCTX *phSession, DWORD dwParam, BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
int AAP_API DCloseSession(HSESSIONCTX *phSession, DWORD dwFlags)
int AAP_API DInitialize(DWORD dwReserved)
int AAP_API DFinalize()
Definição dinamo.h:3090
int nPort
Definição dinamo.h:3092
char szUserId[MAX_USR_LEN]
Definição dinamo.h:3093
char szAddr[MAX_ADDR_LEN]
Definição dinamo.h:3091
char szPassword[MAX_USR_PWD]
Definição dinamo.h:3094