src/server/util/hashing.c

changeset 589
70ad04769cbf
parent 587
c94800af0490
child 594
36a46311e0f6
equal deleted inserted replaced
588:97e7c113b1dd 589:70ad04769cbf
28 28
29 #include "hashing.h" 29 #include "hashing.h"
30 30
31 #include <stdlib.h> 31 #include <stdlib.h>
32 32
33 WS_SHA1_CTX* ws_sha256_create(void) { 33 WS_SHA1_CTX* ws_sha1_create(void) {
34 WS_SHA1_CTX *ctx = malloc(sizeof(WS_SHA1_CTX)); 34 WS_SHA1_CTX *ctx = malloc(sizeof(WS_SHA1_CTX));
35 if(!ctx) { 35 if(!ctx) {
36 return NULL; 36 return NULL;
37 } 37 }
38 if(ws_sha1_init(ctx)) { 38 if(ws_sha1_init(ctx)) {
40 return NULL; 40 return NULL;
41 } 41 }
42 return ctx; 42 return ctx;
43 } 43 }
44 44
45 WS_SHA256_CTX* ws_sha1_create(void) { 45 WS_SHA256_CTX* ws_sha256_create(void) {
46 WS_SHA256_CTX *ctx = malloc(sizeof(WS_SHA256_CTX)); 46 WS_SHA256_CTX *ctx = malloc(sizeof(WS_SHA256_CTX));
47 if(!ctx) { 47 if(!ctx) {
48 return NULL; 48 return NULL;
49 } 49 }
50 if(ws_sha256_init(ctx)) { 50 if(ws_sha256_init(ctx)) {
51 free(ctx);
52 return NULL;
53 }
54 return ctx;
55 }
56
57 WS_SHA1_CTX* ws_sha512_create(void) {
58 WS_SHA512_CTX *ctx = malloc(sizeof(WS_SHA512_CTX));
59 if(!ctx) {
60 return NULL;
61 }
62 if(ws_sha512_init(ctx)) {
51 free(ctx); 63 free(ctx);
52 return NULL; 64 return NULL;
53 } 65 }
54 return ctx; 66 return ctx;
55 } 67 }
67 79
68 void ws_sha1_final(char *md, WS_SHA1_CTX *ctx) { 80 void ws_sha1_final(char *md, WS_SHA1_CTX *ctx) {
69 SHA_Final(md, ctx); 81 SHA_Final(md, ctx);
70 } 82 }
71 83
72 void ws_sha256_init(WS_SHA1_CTX *ctx) { 84 void ws_sha256_init(WS_SHA256_CTX *ctx) {
73 SHA_Init(ctx); 85 SHA_Init(ctx);
74 return 0; 86 return 0;
75 } 87 }
76 88
77 void ws_sha256_update(WS_SHA1_CTX *ctx, const void *data, size_t length) { 89 void ws_sha256_update(WS_SHA256_CTX *ctx, const void *data, size_t length) {
78 SHA_Update(ctx, data, length); 90 SHA_Update(ctx, data, length);
79 } 91 }
80 92
81 void ws_sha256_final(char *md, WS_SHA1_CTX *ctx) { 93 void ws_sha256_final(char *md, WS_SHA256_CTX *ctx) {
82 SHA_Final(md, ctx); 94 SHA_Final(md, ctx);
95 }
96
97 void ws_sha512_init(WS_SHA512_CTX *ctx) {
98 SHA512_Init(ctx);
99 return 0;
100 }
101
102 void ws_sha256_update(WS_SHA512_CTX *ctx, const void *data, size_t length) {
103 SHA512_Update(ctx, data, length);
104 }
105
106 void ws_sha256_final(char *md, WS_SHA512_CTX *ctx) {
107 SHA512_Final(md, ctx);
83 } 108 }
84 109
85 #else 110 #else
86 111
87 int ws_sha1_init(WS_SHA256_CTX *ctx) { 112 int ws_sha1_init(WS_SHA256_CTX *ctx) {
118 143
119 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *md) { 144 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *md) {
120 EVP_DigestFinal(*ctx, md, NULL); 145 EVP_DigestFinal(*ctx, md, NULL);
121 } 146 }
122 147
148 int ws_sha512_init(WS_SHA512_CTX *ctx) {
149 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
150 if(!mdctx) {
151 return 1;
152 }
153 EVP_DigestInit_ex(mdctx, EVP_sha512(), NULL);
154 *ctx = mdctx;
155 return 0;
156 }
157
158 void ws_sha512_update(WS_SHA256_CTX *ctx, const char *data, size_t length) {
159 EVP_DigestUpdate(*ctx, data, length);
160 }
161
162 void ws_sha512_final(WS_SHA256_CTX *ctx, unsigned char *md) {
163 EVP_DigestFinal(*ctx, md, NULL);
164 }
165
123 #endif 166 #endif

mercurial