/soc/2013/ankitkv/gobjectification: 087db73b115d: Refactored pro...

Ankit Vani a at nevitus.org
Sat Jun 15 04:58:51 EDT 2013


Changeset: 087db73b115d4c61a56522135ca237077deb4103
Author:	 Ankit Vani <a at nevitus.org>
Date:	 2013-06-15 14:28 +0530
Branch:	 soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/087db73b115d

Description:

Refactored protocols bonjour, gg, jabber to use the GObject-based PurpleCipher

diffstat:

 libpurple/protocols/bonjour/bonjour_ft.c     |  11 ++++-
 libpurple/protocols/gg/oauth/oauth.c         |  22 ++++++----
 libpurple/protocols/jabber/auth.c            |  19 +++++----
 libpurple/protocols/jabber/auth_digest_md5.c |  30 +++++++--------
 libpurple/protocols/jabber/auth_scram.c      |  53 ++++++++++++++-------------
 libpurple/protocols/jabber/auth_scram.h      |   4 +-
 libpurple/protocols/jabber/caps.c            |  49 ++++++++++++++++---------
 libpurple/protocols/jabber/caps.h            |   3 +-
 libpurple/protocols/jabber/jutil.c           |  23 ++++++++---
 9 files changed, 126 insertions(+), 88 deletions(-)

diffs (truncated from 562 to 300 lines):

diff --git a/libpurple/protocols/bonjour/bonjour_ft.c b/libpurple/protocols/bonjour/bonjour_ft.c
--- a/libpurple/protocols/bonjour/bonjour_ft.c
+++ b/libpurple/protocols/bonjour/bonjour_ft.c
@@ -28,7 +28,7 @@
 #include "buddy.h"
 #include "bonjour.h"
 #include "bonjour_ft.h"
-#include "cipher.h"
+#include "ciphers/sha1.h"
 
 static void
 bonjour_bytestreams_init(PurpleXfer *xfer);
@@ -1018,6 +1018,7 @@ bonjour_bytestreams_connect(PurpleXfer *
 {
 	PurpleBuddy *pb;
 	PurpleAccount *account = NULL;
+	PurpleCipher *hash;
 	XepXfer *xf;
 	char dstaddr[41];
 	const gchar *name = NULL;
@@ -1039,8 +1040,12 @@ bonjour_bytestreams_connect(PurpleXfer *
 	account = purple_buddy_get_account(pb);
 
 	p = g_strdup_printf("%s%s%s", xf->sid, name, bonjour_get_jid(account));
-	purple_cipher_digest_region("sha1", (guchar *)p, strlen(p), hashval,
-		sizeof(hashval));
+
+	hash = purple_sha1_cipher_new();
+	purple_cipher_append(hash, (guchar *)p, strlen(p));
+	purple_cipher_digest(hash, hashval, sizeof(hashval));
+	g_object_unref(G_OBJECT(hash));
+
 	g_free(p);
 
 	memset(dstaddr, 0, 41);
diff --git a/libpurple/protocols/gg/oauth/oauth.c b/libpurple/protocols/gg/oauth/oauth.c
--- a/libpurple/protocols/gg/oauth/oauth.c
+++ b/libpurple/protocols/gg/oauth/oauth.c
@@ -26,7 +26,8 @@
 #include "oauth.h"
 
 #include "oauth-parameter.h"
-#include <cipher.h>
+#include "ciphers/hmac.h"
+#include "ciphers/sha1.h"
 
 char *gg_oauth_static_nonce;		/* dla unit testów */
 char *gg_oauth_static_timestamp;	/* dla unit testów */
@@ -48,15 +49,18 @@ static void gg_oauth_generate_nonce(char
 
 static gchar *gg_hmac_sha1(const char *key, const char *message)
 {
-	PurpleCipherContext *context;
+	PurpleCipher *cipher, *hash;
 	guchar digest[20];
-	
-	context = purple_cipher_context_new_by_name("hmac", NULL);
-	purple_cipher_context_set_option(context, "hash", "sha1");
-	purple_cipher_context_set_key(context, (guchar *)key, strlen(key));
-	purple_cipher_context_append(context, (guchar *)message, strlen(message));
-	purple_cipher_context_digest(context, digest, sizeof(digest));
-	purple_cipher_context_destroy(context);
+
+	hash = purple_sha1_cipher_new();
+	cipher = purple_hmac_cipher_new(hash);
+
+	purple_cipher_set_key(cipher, (guchar *)key, strlen(key));
+	purple_cipher_append(cipher, (guchar *)message, strlen(message));
+	purple_cipher_digest(cipher, digest, sizeof(digest));
+
+	g_object_unref(cipher);
+	g_object_unref(hash);
 	
 	return purple_base64_encode(digest, sizeof(digest));
 }
diff --git a/libpurple/protocols/jabber/auth.c b/libpurple/protocols/jabber/auth.c
--- a/libpurple/protocols/jabber/auth.c
+++ b/libpurple/protocols/jabber/auth.c
@@ -24,7 +24,6 @@
 
 #include "account.h"
 #include "debug.h"
-#include "cipher.h"
 #include "core.h"
 #include "conversation.h"
 #include "request.h"
@@ -39,6 +38,9 @@
 #include "iq.h"
 #include "notify.h"
 
+#include "ciphers/hmac.h"
+#include "ciphers/md5.h"
+
 static GSList *auth_mechs = NULL;
 
 static void auth_old_result_cb(JabberStream *js, const char *from,
@@ -276,16 +278,17 @@ static void auth_old_cb(JabberStream *js
 			 */
 			const char *challenge;
 			gchar digest[33];
-			PurpleCipherContext *hmac;
+			PurpleCipher *hmac, *md5;
 
 			/* Calculate the MHAC-MD5 digest */
+			md5 = purple_md5_cipher_new();
+			hmac = purple_hmac_cipher_new(md5);
 			challenge = xmlnode_get_attrib(x, "challenge");
-			hmac = purple_cipher_context_new_by_name("hmac", NULL);
-			purple_cipher_context_set_option(hmac, "hash", "md5");
-			purple_cipher_context_set_key(hmac, (guchar *)pw, strlen(pw));
-			purple_cipher_context_append(hmac, (guchar *)challenge, strlen(challenge));
-			purple_cipher_context_digest_to_str(hmac, digest, 33);
-			purple_cipher_context_destroy(hmac);
+			purple_cipher_set_key(hmac, (guchar *)pw, strlen(pw));
+			purple_cipher_append(hmac, (guchar *)challenge, strlen(challenge));
+			purple_cipher_digest_to_str(hmac, digest, 33);
+			g_object_unref(hmac);
+			g_object_unref(md5);
 
 			/* Create the response query */
 			iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:auth");
diff --git a/libpurple/protocols/jabber/auth_digest_md5.c b/libpurple/protocols/jabber/auth_digest_md5.c
--- a/libpurple/protocols/jabber/auth_digest_md5.c
+++ b/libpurple/protocols/jabber/auth_digest_md5.c
@@ -23,7 +23,7 @@
 #include "internal.h"
 
 #include "debug.h"
-#include "cipher.h"
+#include "ciphers/md5.h"
 #include "util.h"
 #include "xmlnode.h"
 
@@ -107,7 +107,6 @@ generate_response_value(JabberID *jid, c
 		const char *cnonce, const char *a2, const char *realm)
 {
 	PurpleCipher *cipher;
-	PurpleCipherContext *context;
 	guchar result[16];
 	size_t a1len;
 
@@ -122,35 +121,34 @@ generate_response_value(JabberID *jid, c
 		convpasswd = g_strdup(passwd);
 	}
 
-	cipher = purple_ciphers_find_cipher("md5");
-	context = purple_cipher_context_new(cipher, NULL);
+	cipher = purple_md5_cipher_new();
 
 	x = g_strdup_printf("%s:%s:%s", convnode, realm, convpasswd ? convpasswd : "");
-	purple_cipher_context_append(context, (const guchar *)x, strlen(x));
-	purple_cipher_context_digest(context, result, sizeof(result));
+	purple_cipher_append(cipher, (const guchar *)x, strlen(x));
+	purple_cipher_digest(cipher, result, sizeof(result));
 
 	a1 = g_strdup_printf("xxxxxxxxxxxxxxxx:%s:%s", nonce, cnonce);
 	a1len = strlen(a1);
 	g_memmove(a1, result, 16);
 
-	purple_cipher_context_reset(context, NULL);
-	purple_cipher_context_append(context, (const guchar *)a1, a1len);
-	purple_cipher_context_digest(context, result, sizeof(result));
+	purple_cipher_reset(cipher);
+	purple_cipher_append(cipher, (const guchar *)a1, a1len);
+	purple_cipher_digest(cipher, result, sizeof(result));
 
 	ha1 = purple_base16_encode(result, 16);
 
-	purple_cipher_context_reset(context, NULL);
-	purple_cipher_context_append(context, (const guchar *)a2, strlen(a2));
-	purple_cipher_context_digest(context, result, sizeof(result));
+	purple_cipher_reset(cipher);
+	purple_cipher_append(cipher, (const guchar *)a2, strlen(a2));
+	purple_cipher_digest(cipher, result, sizeof(result));
 
 	ha2 = purple_base16_encode(result, 16);
 
 	kd = g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1, nonce, cnonce, ha2);
 
-	purple_cipher_context_reset(context, NULL);
-	purple_cipher_context_append(context, (const guchar *)kd, strlen(kd));
-	purple_cipher_context_digest(context, result, sizeof(result));
-	purple_cipher_context_destroy(context);
+	purple_cipher_reset(cipher);
+	purple_cipher_append(cipher, (const guchar *)kd, strlen(kd));
+	purple_cipher_digest(cipher, result, sizeof(result));
+	g_object_unref(cipher);
 
 	z = purple_base16_encode(result, 16);
 
diff --git a/libpurple/protocols/jabber/auth_scram.c b/libpurple/protocols/jabber/auth_scram.c
--- a/libpurple/protocols/jabber/auth_scram.c
+++ b/libpurple/protocols/jabber/auth_scram.c
@@ -25,11 +25,12 @@
 #include "auth.h"
 #include "auth_scram.h"
 
-#include "cipher.h"
+#include "ciphers/hmac.h"
+#include "ciphers/sha1.h"
 #include "debug.h"
 
 static const JabberScramHash hashes[] = {
-	{ "-SHA-1", "sha1", 20 },
+	{ "-SHA-1", purple_sha1_cipher_new, 20 },
 };
 
 static const JabberScramHash *mech_to_hash(const char *mech)
@@ -76,7 +77,7 @@ static const struct {
 guchar *jabber_scram_hi(const JabberScramHash *hash, const GString *str,
                         GString *salt, guint iterations)
 {
-	PurpleCipherContext *context;
+	PurpleCipher *hasher, *cipher;
 	guchar *result;
 	guint i;
 	guchar *prev, *tmp;
@@ -90,27 +91,28 @@ guchar *jabber_scram_hi(const JabberScra
 	tmp    = g_new0(guint8, hash->size);
 	result = g_new0(guint8, hash->size);
 
-	context = purple_cipher_context_new_by_name("hmac", NULL);
+	hasher = hash->new_cipher();
+	cipher = purple_hmac_cipher_new(hasher);
+	g_object_unref(G_OBJECT(hasher));
 
 	/* Append INT(1), a four-octet encoding of the integer 1, most significant
 	 * octet first. */
 	g_string_append_len(salt, "\0\0\0\1", 4);
 
 	/* Compute U0 */
-	purple_cipher_context_set_option(context, "hash", (gpointer)hash->name);
-	purple_cipher_context_set_key(context, (guchar *)str->str, str->len);
-	purple_cipher_context_append(context, (guchar *)salt->str, salt->len);
-	purple_cipher_context_digest(context, result, hash->size);
+	purple_cipher_set_key(cipher, (guchar *)str->str, str->len);
+	purple_cipher_append(cipher, (guchar *)salt->str, salt->len);
+	purple_cipher_digest(cipher, result, hash->size);
 
 	memcpy(prev, result, hash->size);
 
 	/* Compute U1...Ui */
 	for (i = 1; i < iterations; ++i) {
 		guint j;
-		purple_cipher_context_set_option(context, "hash", (gpointer)hash->name);
-		purple_cipher_context_set_key(context, (guchar *)str->str, str->len);
-		purple_cipher_context_append(context, prev, hash->size);
-		purple_cipher_context_digest(context, tmp, hash->size);
+		purple_cipher_reset(cipher);
+		purple_cipher_set_key(cipher, (guchar *)str->str, str->len);
+		purple_cipher_append(cipher, prev, hash->size);
+		purple_cipher_digest(cipher, tmp, hash->size);
 
 		for (j = 0; j < hash->size; ++j)
 			result[j] ^= tmp[j];
@@ -118,7 +120,7 @@ guchar *jabber_scram_hi(const JabberScra
 		memcpy(prev, tmp, hash->size);
 	}
 
-	purple_cipher_context_destroy(context);
+	g_object_unref(G_OBJECT(cipher));
 	g_free(tmp);
 	g_free(prev);
 	return result;
@@ -136,25 +138,26 @@ guchar *jabber_scram_hi(const JabberScra
 static void
 hmac(const JabberScramHash *hash, guchar *out, const guchar *key, const gchar *str)
 {
-	PurpleCipherContext *context;
+	PurpleCipher *hasher, *cipher;
 
-	context = purple_cipher_context_new_by_name("hmac", NULL);
-	purple_cipher_context_set_option(context, "hash", (gpointer)hash->name);
-	purple_cipher_context_set_key(context, key, hash->size);
-	purple_cipher_context_append(context, (guchar *)str, strlen(str));
-	purple_cipher_context_digest(context, out, hash->size);
-	purple_cipher_context_destroy(context);
+	hasher = hash->new_cipher();
+	cipher = purple_hmac_cipher_new(hasher);
+	g_object_unref(G_OBJECT(hasher));
+	purple_cipher_set_key(cipher, key, hash->size);
+	purple_cipher_append(cipher, (guchar *)str, strlen(str));
+	purple_cipher_digest(cipher, out, hash->size);
+	g_object_unref(G_OBJECT(cipher));
 }
 
 static void
 hash(const JabberScramHash *hash, guchar *out, const guchar *data)
 {
-	PurpleCipherContext *context;
+	PurpleCipher *hasher;
 
-	context = purple_cipher_context_new_by_name(hash->name, NULL);
-	purple_cipher_context_append(context, data, hash->size);
-	purple_cipher_context_digest(context, out, hash->size);
-	purple_cipher_context_destroy(context);
+	hasher = hash->new_cipher();
+	purple_cipher_append(hasher, data, hash->size);
+	purple_cipher_digest(hasher, out, hash->size);
+	g_object_unref(G_OBJECT(hasher));



More information about the Commits mailing list