Added fingerprint functions.
authorPekka Riikonen <priikone@silcnet.org>
Mon, 10 Jul 2000 05:35:43 +0000 (05:35 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Mon, 10 Jul 2000 05:35:43 +0000 (05:35 +0000)
lib/silccrypt/silchash.c
lib/silccrypt/silchash.h

index 547cb60d0a42421f7c4d6c4270ebe96f77bc188b..18cf81bc4720f815b21464e1c0a5225a1eb22e29 100644 (file)
@@ -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);
+}
index 98f85ef47d0da1846c6e61f2131b8886858e2aad..751c6e174b94ccca818fde6c0b7b0d20dc9cd09b 100644 (file)
@@ -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