From: Pekka Riikonen Date: Mon, 10 Jul 2000 05:35:43 +0000 (+0000) Subject: Added fingerprint functions. X-Git-Tag: SILC.0.1~475 X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=commitdiff_plain;h=5e43b50e8fbfc59db36eda0db3e7bb74dcd8c642 Added fingerprint functions. --- diff --git a/lib/silccrypt/silchash.c b/lib/silccrypt/silchash.c index 547cb60d..18cf81bc 100644 --- a/lib/silccrypt/silchash.c +++ b/lib/silccrypt/silchash.c @@ -20,6 +20,9 @@ /* * $Id$ * $Log$ + * Revision 1.3 2000/07/10 05:35:43 priikone + * Added fingerprint functions. + * * Revision 1.2 2000/07/05 06:08:43 priikone * Global cosmetic change. * @@ -279,3 +282,35 @@ void silc_hash_make(SilcHash hash, const unsigned char *data, 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); +} diff --git a/lib/silccrypt/silchash.h b/lib/silccrypt/silchash.h index 98f85ef4..751c6e17 100644 --- a/lib/silccrypt/silchash.h +++ b/lib/silccrypt/silchash.h @@ -88,5 +88,7 @@ int silc_hash_is_supported(const unsigned char *name); char *silc_hash_get_supported(); void silc_hash_make(SilcHash hash, const unsigned char *data, unsigned int len, unsigned char *return_hash); +char *silc_hash_fingerprint(SilcHash hash, const unsigned char *data, + unsigned int data_len); #endif