[Telepathy-commits] [telepathy-gabble/master] Implement sha1_hex, sha1_bin in terms of GChecksum

Simon McVittie simon.mcvittie at collabora.co.uk
Mon Oct 13 03:43:30 PDT 2008


Now that we use GLib >= 2.16, we can do this. I've kept the utility
wrappers because sha1_hex guarantees to return lower-case (unlike
GChecksum), and sha1_bin is more convenient than what's in GLib.

Also remove compatibility #define for G_PARAM_STATIC_STRINGS, because
we now depend on a GLib version that has it.
---
 src/util.c |   33 ++++++++++++++++-----------------
 src/util.h |   12 +++++-------
 2 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/src/util.c b/src/util.c
index c524d56..4119fca 100644
--- a/src/util.c
+++ b/src/util.c
@@ -36,26 +36,23 @@
 #include "connection.h"
 #include "debug.h"
 #include "namespaces.h"
-#include "sha1/sha1.h"
 
 gchar *
-sha1_hex (const gchar *bytes, guint len)
+sha1_hex (const gchar *bytes,
+          guint len)
 {
-  SHA1Context sc;
-  uint8_t hash[SHA1_HASH_SIZE];
-  gchar *hex_hash = g_malloc (SHA1_HASH_SIZE*2 + 1);
-  int i;
+  gchar *hex = g_compute_checksum_for_string (G_CHECKSUM_SHA1, bytes, len);
+  guint i;
 
-  SHA1Init (&sc);
-  SHA1Update (&sc, bytes, len);
-  SHA1Final (&sc, hash);
-
-  for (i = 0; i < SHA1_HASH_SIZE; i++)
+  for (i = 0; i < SHA1_HASH_SIZE * 2; i++)
     {
-      sprintf (hex_hash + 2 * i, "%02x", (unsigned int) hash[i]);
+      g_assert (hex[i] != '\0');
+      hex[i] = g_ascii_tolower (hex[i]);
     }
 
-  return hex_hash;
+  g_assert (hex[SHA1_HASH_SIZE * 2] == '\0');
+
+  return hex;
 }
 
 void
@@ -63,11 +60,13 @@ sha1_bin (const gchar *bytes,
           guint len,
           guchar out[SHA1_HASH_SIZE])
 {
-  SHA1Context sc;
+  GChecksum *checksum = g_checksum_new (G_CHECKSUM_SHA1);
+  gsize out_len = SHA1_HASH_SIZE;
 
-  SHA1Init (&sc);
-  SHA1Update (&sc, bytes, len);
-  SHA1Final (&sc, (uint8_t *) out);
+  g_assert (g_checksum_type_get_length (G_CHECKSUM_SHA1) == SHA1_HASH_SIZE);
+  g_checksum_update (checksum, (const guchar *) bytes, len);
+  g_checksum_get_digest (checksum, out, &len);
+  g_assert (out_len == SHA1_HASH_SIZE);
 }
 
 static void
diff --git a/src/util.h b/src/util.h
index a4331e4..fceebbf 100644
--- a/src/util.h
+++ b/src/util.h
@@ -30,16 +30,14 @@
 #include <loudmouth/loudmouth.h>
 
 #include "types.h"
-#include "sha1/sha1.h"
-
-/* In case we're building with GLib < 2.13. */
-#ifndef G_PARAM_STATIC_STRINGS
-#define G_PARAM_STATIC_STRINGS \
-    (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)
-#endif
 
+/* Guarantees that the resulting hash is in lower-case */
 gchar *sha1_hex (const gchar *bytes, guint len);
+
+/* A SHA1 digest is 20 bytes long */
+#define SHA1_HASH_SIZE 20
 void sha1_bin (const gchar *bytes, guint len, guchar out[SHA1_HASH_SIZE]);
+
 void lm_message_node_add_own_nick (LmMessageNode *node,
     GabbleConnection *conn);
 void lm_message_node_unlink (LmMessageNode *orphan);
-- 
1.5.6.5




More information about the Telepathy-commits mailing list