Changeset 4670:8eeb660de23d in roaraudio


Ignore:
Timestamp:
12/19/10 23:43:57 (11 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

some statefull general hash api...

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • include/libroar/hash.h

    r4453 r4670  
    7676#define ROAR_HT_ISBN13      ROAR_HT_GTIN13 
    7777 
     78struct 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 
    7889const char * roar_ht2str (const int    ht); 
    7990int          roar_str2ht (const char * ht); 
     
    8596struct roar_hash_state; 
    8697 
     98struct roar_hash_state * roar_hash_new(int algo); 
     99int roar_hash_free(struct roar_hash_state * state); 
     100int roar_hash_digest(struct roar_hash_state * state, void * digest, size_t * len); 
     101int roar_hash_proc(struct roar_hash_state * state, const void * data, size_t len); 
     102 
    87103int roar_hash_buffer(void * digest, const void * data, size_t datalen, int algo); 
    88104int 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  
    3939#include <gcrypt.h> 
    4040#endif 
     41 
     42struct roar_hash_state { 
     43 struct roar_hash_cmds * cmds; 
     44 void * state; 
     45}; 
    4146 
    4247static const struct hashes { 
     
    8691}; 
    8792 
     93static 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 
     103static 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 
    88113static inline int roar_ht2gcrypt_tested (const int ht) { 
    89114#ifdef ROAR_HAVE_LIBGCRYPT 
     
    141166 roar_crypto_init(); 
    142167 
     168 if ( roar_ht2cmds(ht) != NULL ) 
     169  return 1; 
     170 
    143171#ifdef ROAR_HAVE_LIBGCRYPT 
    144172 if ( roar_ht2gcrypt_tested(ht) == -1 ) 
     
    149177 return 0; 
    150178#endif 
     179} 
     180 
     181struct 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 
     217int 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 
     232int 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 
     242int 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); 
    151250} 
    152251 
     
    189288 
    190289int 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 
    191294 if ( digest == NULL || data == NULL ) 
    192295  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 } 
    193317 
    194318#ifdef ROAR_HAVE_LIBGCRYPT 
Note: See TracChangeset for help on using the changeset viewer.