Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sha: add support for Windows native API #851

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/sha/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,32 @@
#include <openssl/sha.h>
#elif defined (__APPLE__)
#include <CommonCrypto/CommonDigest.h>
#elif defined (WIN32)
#include <windows.h>
#include <wincrypt.h>
#endif
#include <re_sha.h>


#if !defined (USE_OPENSSL) && defined (WIN32)
static void compute_hash(ALG_ID alg_id, const void *data, size_t data_size,
uint8_t *md, DWORD hash_size)
{
HCRYPTPROV context;
HCRYPTHASH hash;

CryptAcquireContext(&context, 0, 0, PROV_RSA_FULL,CRYPT_VERIFYCONTEXT);

CryptCreateHash(context, alg_id, 0, 0, &hash);
CryptHashData(hash, (BYTE*)data, data_size, 0);
CryptGetHashParam(hash, HP_HASHVAL, md, &hash_size, 0);

CryptDestroyHash(hash);
CryptReleaseContext(context, 0);
}
#endif


/**
* Calculate the SHA1 hash from a buffer
*
Expand All @@ -28,6 +50,8 @@ void sha1(const uint8_t *d, size_t n, uint8_t *md)
(void)SHA1(d, n, md);
#elif defined (__APPLE__)
CC_SHA1(d, (uint32_t)n, md);
#elif defined (WIN32)
compute_hash(CALG_SHA1, d, n, md, SHA1_DIGEST_SIZE);
#else
(void)d;
(void)n;
Expand All @@ -50,6 +74,8 @@ void sha256(const uint8_t *d, size_t n, uint8_t *md)
(void)SHA256(d, n, md);
#elif defined (__APPLE__)
CC_SHA256(d, (uint32_t)n, md);
#elif defined (WIN32)
compute_hash(CALG_SHA_256, d, n, md, SHA256_DIGEST_SIZE);
#else
(void)d;
(void)n;
Expand Down