diff options
author | Yaakov Selkowitz <yselkowitz@users.sourceforge.net> | 2012-04-26 01:49:06 -0500 |
---|---|---|
committer | Yaakov Selkowitz <yselkowitz@users.sourceforge.net> | 2012-05-04 13:13:50 -0500 |
commit | e6461db6dcb437cfa7d4b23008c2c12f8169ff79 (patch) | |
tree | fcf4d8f7922d3ad99d8f49a24d50eb91a8e0b231 /os | |
parent | 97041364a6acb2b66b5cfd06757c90a006ad50e9 (diff) |
os: Add CryptoAPI as a choice of SHA1 implementation
Both Cygwin and MinGW can use Windows' native CryptoAPI for SHA1,
saving a dependency on libgcrypt or OpenSSL. The necessary functions
are in ADVAPI32.DLL, which is among the default lib flags and is
already used in hw/xwin for accessing the registry.
Signed-off-by: Yaakov Selkowitz <yselkowitz@users.sourceforge.net>
Reviewed-by: Mikhail Gusarov <dottedmag@dottedmag.net>
Reviewed-by: Jon TURNEY <jon.turney@dronecode.org.uk>
Reviewed-by: Colin Harrison <colin.harrison@virgin.net>
Tested-by: Colin Harrison <colin.harrison@virgin.net>
Diffstat (limited to 'os')
-rw-r--r-- | os/xsha1.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/os/xsha1.c b/os/xsha1.c index dccce74b6..fa66c7a06 100644 --- a/os/xsha1.c +++ b/os/xsha1.c @@ -74,6 +74,48 @@ x_sha1_final(void *ctx, unsigned char result[20]) return 1; } +#elif defined(HAVE_SHA1_IN_CRYPTOAPI) /* Use CryptoAPI for SHA1 */ + +#define WIN32_LEAN_AND_MEAN +#include <X11/Xwindows.h> +#include <wincrypt.h> + +static HCRYPTPROV hProv; + +void * +x_sha1_init(void) +{ + HCRYPTHASH *ctx = malloc(sizeof(*ctx)); + + if (!ctx) + return NULL; + CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); + CryptCreateHash(hProv, CALG_SHA1, 0, 0, ctx); + return ctx; +} + +int +x_sha1_update(void *ctx, void *data, int size) +{ + HCRYPTHASH *hHash = ctx; + + CryptHashData(*hHash, data, size, 0); + return 1; +} + +int +x_sha1_final(void *ctx, unsigned char result[20]) +{ + HCRYPTHASH *hHash = ctx; + DWORD len = 20; + + CryptGetHashParam(*hHash, HP_HASHVAL, result, &len, 0); + CryptDestroyHash(*hHash); + CryptReleaseContext(hProv, 0); + free(ctx); + return 1; +} + #elif defined(HAVE_SHA1_IN_LIBGCRYPT) /* Use libgcrypt for SHA1 */ #include <gcrypt.h> |