Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
- Copyright (C) 1997 - 2000 Pekka Riikonen
+ Copyright (C) 1997 - 2001 Pekka Riikonen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
GNU General Public License for more details.
*/
-/*
- * $Id$
- * $Log$
- * Revision 1.1 2000/06/27 11:36:55 priikone
- * Initial revision
- *
- *
- */
+/* $Id$ */
#include "silcincludes.h"
/* 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));
- if (!new) {
- SILC_LOG_ERROR(("Could not allocate new hash list object"));
- return FALSE;
- }
-
new->hash = silc_calloc(1, sizeof(*new->hash));
- if (!new->hash) {
- SILC_LOG_ERROR(("Could not allocate new hash object"));
- return FALSE;
- }
/* 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;
/* Allocate the new object */
*new_hash = silc_calloc(1, sizeof(**new_hash));
- if (*new_hash == NULL) {
- SILC_LOG_ERROR(("Could not allocate new hash object"));
- return FALSE;
- }
if (silc_hash_list) {
h = silc_hash_list;
h = h->next;
}
- if (!h)
+ if (!h || !h->hash->context_len)
goto check_builtin;
/* Set the pointers */
if (silc_hash_builtin_list[i].name == NULL) {
silc_free(*new_hash);
+ *new_hash = NULL;
return FALSE;
}
}
}
+/* Returns the length of the hash digest. */
+
+unsigned int 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;
hash->hash->update(hash->context, (unsigned char *)data, len);
hash->hash->final(hash->context, return_hash);
}
+
+/* Creates fingerprint of the data. If `hash' is NULL SHA1 is used as
+ default hash function. The returned fingerprint must be free's by the
+ caller. */
+
+char *silc_hash_fingerprint(SilcHash hash, const unsigned char *data,
+ unsigned int data_len)
+{
+ char fingerprint[64], *cp;
+ unsigned char h[32];
+ int i;
+
+ if (!hash)
+ silc_hash_alloc("sha1", &hash);
+
+ silc_hash_make(hash, data, data_len, h);
+
+ memset(fingerprint, 0, sizeof(fingerprint));
+ cp = fingerprint;
+ for (i = 0; i < hash->hash->hash_len; i++) {
+ snprintf(cp, sizeof(fingerprint), "%02X", h[i]);
+ cp += 2;
+
+ if ((i + 1) % 2 == 0)
+ snprintf(cp++, sizeof(fingerprint), " ");
+
+ if ((i + 1) % 10 == 0)
+ snprintf(cp++, sizeof(fingerprint), " ");
+ }
+
+ return strdup(fingerprint);
+}