updates. New data types.
[silc.git] / lib / silccrypt / silchash.c
index 8bb7339336e98188d45ec00925a4c02b0527d3f5..e94a1b7a4ac6d51391748aab629ee960389f7673 100644 (file)
@@ -38,29 +38,28 @@ struct SilcHashListStruct *silc_hash_list = NULL;
 /* Statically declared list of hash functions. */
 SilcHashObject silc_hash_builtin_list[] = 
 {
-  { "md5", 16, 64, silc_md5_init, silc_md5_update, silc_md5_final,
-    silc_md5_transform, silc_md5_context_len },
   { "sha1", 20, 64, silc_sha1_init, silc_sha1_update, silc_sha1_final,
     silc_sha1_transform, silc_sha1_context_len },
+  { "md5", 16, 64, silc_md5_init, silc_md5_update, silc_md5_final,
+    silc_md5_transform, silc_md5_context_len },
 
   { NULL, 0, 0, NULL, NULL, NULL, NULL, NULL }
 };
 
-/* Registers a ned hash function into the SILC. This function is used at
+/* Registers a new hash function into the SILC. This function is used at
    the initialization of the SILC. */
 
 int silc_hash_register(SilcHashObject *hash)
 {
   struct SilcHashListStruct *new, *h;
 
-  SILC_LOG_DEBUG(("Registering new hash function"));
+  SILC_LOG_DEBUG(("Registering new hash function `%s'", hash->name));
 
   new = silc_calloc(1, sizeof(*new));
   new->hash = silc_calloc(1, sizeof(*new->hash));
 
   /* Set the pointers */
-  new->hash->name = silc_calloc(1, strlen(hash->name));
-  memcpy(new->hash->name, hash->name, strlen(hash->name));
+  new->hash->name = strdup(hash->name);
   new->hash->hash_len = hash->hash_len;
   new->hash->block_len = hash->block_len;
   new->hash->init = hash->init;
@@ -156,7 +155,7 @@ int silc_hash_alloc(const unsigned char *name, SilcHash *new_hash)
       h = h->next;
     }
 
-    if (!h)
+    if (!h || !h->hash->context_len)
       goto check_builtin;
 
     /* Set the pointers */
@@ -174,6 +173,7 @@ int silc_hash_alloc(const unsigned char *name, SilcHash *new_hash)
 
   if (silc_hash_builtin_list[i].name == NULL) {
     silc_free(*new_hash);
+    *new_hash = NULL;
     return FALSE;
   }
   
@@ -195,12 +195,22 @@ void silc_hash_free(SilcHash hash)
   }
 }
 
+/* Returns the length of the hash digest. */
+
+uint32 silc_hash_len(SilcHash hash)
+{
+  return hash->hash->hash_len;
+}
+
 /* Returns TRUE if hash algorithm `name' is supported. */
 
 int silc_hash_is_supported(const unsigned char *name)
 {
   struct SilcHashListStruct *h;
   int i;
+
+  if (!name)
+    return FALSE;
   
   if (silc_hash_list) {
     h = silc_hash_list;
@@ -263,7 +273,7 @@ char *silc_hash_get_supported()
 /* Creates the hash value and returns it to the return_hash argument. */
 
 void silc_hash_make(SilcHash hash, const unsigned char *data, 
-                   unsigned int len, unsigned char *return_hash)
+                   uint32 len, unsigned char *return_hash)
 {
   hash->hash->init(hash->context);
   hash->hash->update(hash->context, (unsigned char *)data, len);
@@ -275,7 +285,7 @@ void silc_hash_make(SilcHash hash, const unsigned char *data,
    caller. */
 
 char *silc_hash_fingerprint(SilcHash hash, const unsigned char *data,
-                           unsigned int data_len)
+                           uint32 data_len)
 {
   char fingerprint[64], *cp;
   unsigned char h[32];