Browse Source

Implement SHA1 digest of echo message

Signed-off-by: Selva Nair <selva.nair@gmail.com>
pull/391/head
Selva Nair 7 years ago
parent
commit
9c914305d9
  1. 8
      echo.c
  2. 50
      misc.c
  3. 12
      misc.h

8
echo.c

@ -69,8 +69,14 @@ echo_msg_init(void)
static void
echo_msg_add_fp(struct echo_msg *msg, time_t timestamp)
{
md_ctx ctx;
msg->fp.timestamp = timestamp;
/* digest not implemented */
if (md_init(&ctx, CALG_SHA1) != 0)
return;
md_update(&ctx, (BYTE*) msg->text, msg->txtlen*sizeof(msg->text[0]));
md_update(&ctx, (BYTE*) msg->title, wcslen(msg->title)*sizeof(msg->title[0]));
md_final(&ctx, msg->fp.digest);
return;
}

50
misc.c

@ -520,3 +520,53 @@ url_decode(const char *src)
return out;
}
DWORD
md_init(md_ctx *ctx, ALG_ID hash_type)
{
DWORD status = 0;
if (!CryptAcquireContext(&ctx->prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
goto err;
if (!CryptCreateHash(ctx->prov, hash_type, 0, 0, &ctx->hash))
{
CryptReleaseContext(ctx->prov, 0);
goto err;
}
return status;
err:
status = GetLastError();
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Error in md_ctx_init: status = %lu", status);
return status;
}
DWORD
md_update(md_ctx *ctx, const BYTE *data, size_t size)
{
DWORD status = 0;
if (!CryptHashData(ctx->hash, data, (DWORD)size, 0))
{
status = GetLastError();
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Error in md_update: status = %lu", status);
}
return status;
}
DWORD
md_final(md_ctx *ctx, BYTE *md)
{
DWORD status = 0;
DWORD digest_len = HASHLEN;
if (!CryptGetHashParam(ctx->hash, HP_HASHVAL, md, &digest_len, 0))
{
status = GetLastError();
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Error in md_final: status = %lu", status);
}
CryptDestroyHash(ctx->hash);
CryptReleaseContext(ctx->prov, 0);
return status;
}

12
misc.h

@ -22,6 +22,8 @@
#ifndef MISC_H
#define MISC_H
#include <wincrypt.h>
BOOL ManagementCommandFromInput(connection_t *, LPCSTR, HWND, int);
BOOL ManagementCommandFromInputBase64(connection_t *, LPCSTR, HWND, int, int);
@ -53,4 +55,14 @@ void CloseHandleEx(LPHANDLE h);
*/
char *url_decode(const char *src);
/* digest functions */
typedef struct md_ctx {
HCRYPTPROV prov;
HCRYPTHASH hash;
} md_ctx;
DWORD md_init(md_ctx *ctx, ALG_ID hash_type);
DWORD md_update(md_ctx *ctx, const BYTE *data, size_t size);
DWORD md_final(md_ctx *ctx, BYTE *md);
#endif

Loading…
Cancel
Save