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 #ifndef HASHING_H
30 #define HASHING_H
31
32 #define WS_SHA1_DIGEST_LENGTH 20
33 #define WS_SHA256_DIGEST_LENGTH 32
34 #define WS_SHA512_DIGEST_LENGTH 32
35
36 #ifdef __APPLE__
37 #define WS_USE_CRYPTO_COMMON
38
39
40 #define WS_CRYPTO_COMMON_CRYPTO
41
42 #define WS_AES_CTX CCCryptorRef
43 #define WS_SHA1_CTX CC_SHA1_CTX
44 #define WS_SHA256_CTX CC_SHA256_CTX
45 #define WS_SHA512_CTX CC_SHA512_CTX
46
47 #include <CommonCrypto/CommonCrypto.h>
48 #include <CommonCrypto/CommonDigest.h>
49
50 #elif defined(
_WIN32)
51
52 #define WS_USE_CRYPTO_CNG
53
54 #include <windows.h>
55 #include <bcrypt.h>
56
57 typedef struct WinBCryptCTX {
58 BCRYPT_ALG_HANDLE hAlg;
59 BCRYPT_KEY_HANDLE hKey;
60 void *pbKeyObject;
61 unsigned char pbIV[
16];
62
63 unsigned char buf[
16];
64 ULONG buflen;
65 } WinBCryptCTX;
66
67 typedef struct WinBCryptSHACTX {
68 BCRYPT_ALG_HANDLE hAlg;
69 BCRYPT_HASH_HANDLE hHash;
70 void *pbHashObject;
71 } WinBCryptSHACTX;
72
73 #define WS_AES_CTX WinBCryptCTX
74 #define WS_SHA1_CTX WinBCryptSHACTX
75 #define WS_SHA256_CTX WinBCryptSHACTX
76 #define WS_SHA512_CTX WinBCryptSHACTX
77
78 #else
79
80
81 #include <openssl/evp.h>
82 #include <openssl/rand.h>
83
84 #define WS_USE_OPENSSL
85
86 #if OPENSSL_VERSION_NUMBER <
0x30000000L
87 #define WS_SHA1_CTX SHA_CTX
88 #define WS_SHA256_CTX SHA256_CTX
89 #define WS_SHA512_CTX SHA512_CTX
90 #else
91 #define WS_SHA1_CTX EVP_MD_CTX*
92 #define WS_SHA256_CTX EVP_MD_CTX*
93 #define WS_SHA512_CTX EVP_MD_CTX*
94 #endif
95
96
97 #if defined(__sun) && defined(__SunOS_5_10)
98 #include <sha2.h>
99 #define SHA_Init SHAInit
100 #define SHA_Update SHAUpdate
101 #define SHA_Final SHAFinal
102
103 #define SHA256_Init SHA256Init
104 #define SHA256_Update SHA256Update
105 #define SHA256_Final SHA256Final
106
107 #define SHA512_Init SHA512Init
108 #define SHA512_Update SHA512Update
109 #define SHA512_Final SHA512Final
110 #else
111 #include <openssl/sha.h>
112 #endif
113
114 #endif
115
116 int ws_sha1_init(
WS_SHA1_CTX *ctx);
117 WS_SHA1_CTX* ws_sha1_create(
void);
118 void ws_sha1_update(
WS_SHA1_CTX *ctx,
const char *data,
size_t len);
119 void ws_sha1_final(
WS_SHA1_CTX *ctx,
unsigned char *buf);
120
121 int ws_sha256_init(
WS_SHA256_CTX *ctx);
122 WS_SHA256_CTX* ws_sha256_create(
void);
123 void ws_sha256_update(
WS_SHA256_CTX *ctx,
const char *data,
size_t len);
124 void ws_sha256_final(
WS_SHA256_CTX *ctx,
unsigned char *buf);
125
126 int ws_sha512_init(
WS_SHA512_CTX *ctx);
127 WS_SHA512_CTX* ws_sha512_create(
void);
128 void ws_sha512_update(
WS_SHA512_CTX *ctx,
const char *data,
size_t len);
129 void ws_sha512_final(
WS_SHA512_CTX *ctx,
unsigned char *buf);
130
131
132 #ifdef __cplusplus
133 }
134 #endif
135
136 #endif
137
138