diff --git a/echo.c b/echo.c index 3261c4f..9dea46a 100644 --- a/echo.c +++ b/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; } diff --git a/misc.c b/misc.c index 8597f1e..0d31d4e 100644 --- a/misc.c +++ b/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; +} diff --git a/misc.h b/misc.h index cbf0a24..6e9f7b6 100644 --- a/misc.h +++ b/misc.h @@ -22,6 +22,8 @@ #ifndef MISC_H #define MISC_H +#include + 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