-
Notifications
You must be signed in to change notification settings - Fork 3
/
u_sha256.c
45 lines (35 loc) · 998 Bytes
/
u_sha256.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stddef.h>
#include "deconz/u_sha256.h"
#include "deconz/u_library_ex.h"
#define LONESHA256_STATIC
#include "lonesha256.h"
static unsigned char (*libSHA256)(const unsigned char *d, size_t n, unsigned char *md);
unsigned char libSha256LoneHash(const unsigned char *d, size_t n, unsigned char *hash)
{
lonesha256(hash, d, n);
return 1;
}
static int init_sha256_lib(void)
{
void *libcrypto;
if (!libSHA256)
{
#ifdef HAS_OPENSSL
libcrypto = U_library_open_ex("libcrypto");
if (libcrypto)
libSHA256 = (unsigned char (*)(const unsigned char*, size_t, unsigned char*))U_library_symbol(libcrypto, "SHA256");
#endif
if (!libSHA256)
libSHA256 = libSha256LoneHash;
}
return libSHA256 ? 1 : 0;
}
int U_Sha256(const void *data, unsigned size, unsigned char *hash)
{
if (!init_sha256_lib())
return 0;
if (!data || size == 0 || !hash)
return 0;
libSHA256(data, size, hash);
return 1;
}