Changeset 4670:8eeb660de23d in roaraudio
- Timestamp:
- 12/19/10 23:43:57 (13 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
include/libroar/hash.h
r4453 r4670 76 76 #define ROAR_HT_ISBN13 ROAR_HT_GTIN13 77 77 78 struct roar_hash_cmds { 79 int algo; 80 ssize_t statelen; 81 ssize_t blocksize; 82 int (*init)(void * state); 83 int (*uninit)(void * state); 84 int (*digest)(void * state, void * digest, size_t * len); 85 int (*proc_block)(void * state, const void * block); 86 int (*proc)(void * state, const void * data, size_t len); 87 }; 88 78 89 const char * roar_ht2str (const int ht); 79 90 int roar_str2ht (const char * ht); … … 85 96 struct roar_hash_state; 86 97 98 struct roar_hash_state * roar_hash_new(int algo); 99 int roar_hash_free(struct roar_hash_state * state); 100 int roar_hash_digest(struct roar_hash_state * state, void * digest, size_t * len); 101 int roar_hash_proc(struct roar_hash_state * state, const void * data, size_t len); 102 87 103 int roar_hash_buffer(void * digest, const void * data, size_t datalen, int algo); 88 104 int roar_hash_salted_buffer(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen); -
libroar/hash.c
r4549 r4670 39 39 #include <gcrypt.h> 40 40 #endif 41 42 struct roar_hash_state { 43 struct roar_hash_cmds * cmds; 44 void * state; 45 }; 41 46 42 47 static const struct hashes { … … 86 91 }; 87 92 93 static struct roar_hash_cmds _libroar_hash_cmds[] = { 94 {ROAR_HT_TIGER, sizeof(struct roar_hash_tiger), 512, 95 (int (*)(void *))roar_hash_tiger_init, (int (*)(void *))roar_hash_tiger_uninit, 96 (int (*)(void *, void *, size_t *))roar_hash_tiger_get_digest, 97 (int (*)(void *, const void *))roar_hash_tiger_proc_block, 98 (int (*)(void *, const void *, size_t))roar_hash_tiger_proc 99 }, 100 {-1, -1, -1, NULL, NULL, NULL, NULL, NULL} 101 }; 102 103 static struct roar_hash_cmds * roar_ht2cmds(const int ht) { 104 size_t i; 105 106 for(i = 0; _libroar_hash_cmds[i].algo != -1; i++) 107 if ( _libroar_hash_cmds[i].algo == ht ) 108 return &(_libroar_hash_cmds[i]); 109 110 return NULL; 111 } 112 88 113 static inline int roar_ht2gcrypt_tested (const int ht) { 89 114 #ifdef ROAR_HAVE_LIBGCRYPT … … 141 166 roar_crypto_init(); 142 167 168 if ( roar_ht2cmds(ht) != NULL ) 169 return 1; 170 143 171 #ifdef ROAR_HAVE_LIBGCRYPT 144 172 if ( roar_ht2gcrypt_tested(ht) == -1 ) … … 149 177 return 0; 150 178 #endif 179 } 180 181 struct roar_hash_state * roar_hash_new(int algo) { 182 struct roar_hash_cmds * cmds = roar_ht2cmds(algo); 183 struct roar_hash_state * self; 184 185 if ( cmds == NULL ) 186 return NULL; 187 188 self = roar_mm_malloc(sizeof(struct roar_hash_state)); 189 190 if ( self == NULL ) 191 return NULL; 192 193 memset(self, 0, sizeof(struct roar_hash_state)); 194 195 self->cmds = cmds; 196 197 self->state = roar_mm_malloc(cmds->statelen); 198 199 if ( self->state == NULL ) { 200 roar_mm_free(self); 201 return NULL; 202 } 203 204 memset(self->state, 0, cmds->statelen); 205 206 if ( cmds->init != NULL ) { 207 if ( cmds->init(self->state) == -1 ) { 208 roar_mm_free(self->state); 209 roar_mm_free(self); 210 return NULL; 211 } 212 } 213 214 return self; 215 } 216 217 int roar_hash_free(struct roar_hash_state * state) { 218 int ret = 0; 219 220 if ( state == NULL ) 221 return -1; 222 223 if ( state->cmds->uninit != NULL ) 224 ret = state->cmds->uninit(state->state); 225 226 roar_mm_free(state->state); 227 roar_mm_free(state); 228 229 return ret; 230 } 231 232 int roar_hash_digest(struct roar_hash_state * state, void * digest, size_t * len) { 233 if ( state == NULL ) 234 return -1; 235 236 if ( state->cmds->digest == NULL ) 237 return -1; 238 239 return state->cmds->digest(state->state, digest, len); 240 } 241 242 int roar_hash_proc(struct roar_hash_state * state, const void * data, size_t len) { 243 if ( state == NULL ) 244 return -1; 245 246 if ( state->cmds->proc == NULL ) 247 return -1; 248 249 return state->cmds->proc(state->state, data, len); 151 250 } 152 251 … … 189 288 190 289 int roar_hash_salted_buffer(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) { 290 struct roar_hash_state * state; 291 size_t len; 292 int ret; 293 191 294 if ( digest == NULL || data == NULL ) 192 295 return -1; 296 297 len = roar_ht_digestlen(algo); 298 if ( len == -1 ) 299 return -1; 300 301 if ( (state = roar_hash_new(algo)) != NULL ) { 302 if ( roar_hash_proc(state, data, datalen) == -1 ) 303 ret = -1; 304 305 if ( saltlen != 0 ) 306 if ( roar_hash_proc(state, salt, saltlen) == -1 ) 307 ret = -1; 308 309 if ( roar_hash_digest(state, digest, &len) == -1 ) 310 ret = -1; 311 312 if ( roar_hash_free(state) == -1 ) 313 ret = -1; 314 315 return ret; 316 } 193 317 194 318 #ifdef ROAR_HAVE_LIBGCRYPT
Note: See TracChangeset
for help on using the changeset viewer.