[epiphany/wip/ephy-sync: 60/81] New ephy-sync-utils module



commit 82a5800fb2a0e7683a980a1204c5ac91f2a936ef
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Mon Jun 20 14:04:22 2016 +0300

    New ephy-sync-utils module

 src/Makefile.am         |   14 ++++++----
 src/ephy-sync-crypto.c  |   45 ---------------------------------
 src/ephy-sync-crypto.h  |   38 +++++++++++-----------------
 src/ephy-sync-service.c |   23 +++++++++--------
 src/ephy-sync-service.h |   29 +++++++++++----------
 src/ephy-sync-utils.c   |   64 +++++++++++++++++++++++++++++++++++++++++++++++
 src/ephy-sync-utils.h   |   44 ++++++++++++++++++++++++++++++++
 src/ephy-sync-window.c  |   18 ++++++------
 src/ephy-sync-window.h  |    2 +-
 9 files changed, 168 insertions(+), 109 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 857f3c7..1cf8d9f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -43,12 +43,6 @@ libephymain_la_SOURCES = \
        ephy-home-action.h                      \
        ephy-history-window.c                   \
        ephy-history-window.h                   \
-       ephy-sync-window.c                      \
-       ephy-sync-window.h                      \
-       ephy-sync-service.c                     \
-       ephy-sync-service.h                     \
-       ephy-sync-crypto.c                      \
-       ephy-sync-crypto.h                      \
        ephy-link.c                             \
        ephy-link.h                             \
        ephy-link-action.c                      \
@@ -67,6 +61,14 @@ libephymain_la_SOURCES = \
        ephy-session.h                          \
        ephy-shell.c                            \
        ephy-shell.h                            \
+       ephy-sync-crypto.c                      \
+       ephy-sync-crypto.h                      \
+       ephy-sync-service.c                     \
+       ephy-sync-service.h                     \
+       ephy-sync-utils.c                       \
+       ephy-sync-utils.h                       \
+       ephy-sync-window.c                      \
+       ephy-sync-window.h                      \
        ephy-title-box.c                        \
        ephy-title-box.h                        \
        ephy-toolbar.c                          \
diff --git a/src/ephy-sync-crypto.c b/src/ephy-sync-crypto.c
index 056c7ce..00480cf 100644
--- a/src/ephy-sync-crypto.c
+++ b/src/ephy-sync-crypto.c
@@ -16,46 +16,12 @@
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "ephy-debug.h"
 #include "ephy-sync-crypto.h"
 
 #include <nettle/hmac.h>
 #include <nettle/pbkdf2.h>
 #include <string.h>
 
-static const gchar hex_digits[] = "0123456789abcdef";
-
-gchar *
-ephy_sync_crypto_kw (const gchar *name)
-{
-  return g_strconcat ("identity.mozilla.com/picl/v1/", name, NULL);
-}
-
-gchar *
-ephy_sync_crypto_kwe (const gchar *name,
-                      const gchar *emailUTF8)
-{
-  return g_strconcat ("identity.mozilla.com/picl/v1/", name, ":", emailUTF8, NULL);
-}
-
-gchar *
-ephy_sync_crypto_encode_hex (guint8 *data,
-                             gsize   data_length)
-{
-  gchar *retval = g_malloc (data_length * 2 + 1);
-
-  for (gsize i = 0; i < data_length; i++) {
-    guint8 byte = data[i];
-
-    retval[2 * i] = hex_digits[byte >> 4];
-    retval[2 * i + 1] = hex_digits[byte & 0xf];
-  }
-
-  retval[data_length * 2] = 0;
-
-  return retval;
-}
-
 /*
  * Runs 1000 iterations of PBKDF2.
  * Uses sha256 as hash function.
@@ -128,14 +94,3 @@ ephy_sync_crypto_hkdf (guint8 *in,
   g_free (tmp);
   g_free (prk);
 }
-
-/* FIXME: Only for debugging, remove when no longer needed */
-void
-ephy_sync_crypto_display_hex (guint8      *data,
-                              gsize        data_length,
-                              const gchar *data_name)
-{
-LOG ("%s:", data_name);
-for (gsize i = 0; i < data_length; i++)
-  LOG ("%02x", data[i]);
-}
diff --git a/src/ephy-sync-crypto.h b/src/ephy-sync-crypto.h
index cb83395..ec4cd3a 100644
--- a/src/ephy-sync-crypto.h
+++ b/src/ephy-sync-crypto.h
@@ -23,29 +23,21 @@
 
 G_BEGIN_DECLS
 
-gchar *ephy_sync_crypto_kw          (const gchar *name);
-gchar *ephy_sync_crypto_kwe         (const gchar *name,
-                                     const gchar *emailUTF8);
-gchar *ephy_sync_crypto_encode_hex  (guint8 *data,
-                                     gsize   data_length);
-void   ephy_sync_crypto_pbkdf2_1k   (guint8 *key,
-                                     gsize   key_length,
-                                     guint8 *salt,
-                                     gsize   salt_length,
-                                     guint8 *out,
-                                     gsize   out_length);
-void   ephy_sync_crypto_hkdf        (guint8 *in,
-                                     gsize   in_length,
-                                     guint8 *salt,
-                                     gsize   salt_length,
-                                     guint8 *info,
-                                     gsize   info_length,
-                                     guint8 *out,
-                                     gsize   out_length);
-/* FIXME: Only for debugging, remove when no longer needed */
-void   ephy_sync_crypto_display_hex (guint8      *data,
-                                     gsize        data_length,
-                                     const gchar *data_name);
+void ephy_sync_crypto_pbkdf2_1k (guint8 *key,
+                                 gsize   key_length,
+                                 guint8 *salt,
+                                 gsize   salt_length,
+                                 guint8 *out,
+                                 gsize   out_length);
+
+void ephy_sync_crypto_hkdf      (guint8 *in,
+                                 gsize   in_length,
+                                 guint8 *salt,
+                                 gsize   salt_length,
+                                 guint8 *info,
+                                 gsize   info_length,
+                                 guint8 *out,
+                                 gsize   out_length);
 
 G_END_DECLS
 
diff --git a/src/ephy-sync-service.c b/src/ephy-sync-service.c
index a610f9a..693ebda 100644
--- a/src/ephy-sync-service.c
+++ b/src/ephy-sync-service.c
@@ -19,6 +19,7 @@
 #include "ephy-debug.h"
 #include "ephy-sync-crypto.h"
 #include "ephy-sync-service.h"
+#include "ephy-sync-utils.h"
 
 #include <json-glib/json-glib.h>
 #include <libsoup/soup.h>
@@ -92,7 +93,7 @@ LOG ("%s:%d", __func__, __LINE__);
   message = soup_message_new (SOUP_METHOD_POST,
                               "https://api.accounts.firefox.com/v1/account/login";);
 
-  authPW_hex = ephy_sync_crypto_encode_hex (authPW, EPHY_SYNC_SERVICE_TOKEN_LENGTH);
+  authPW_hex = ephy_sync_utils_encode_hex (authPW, EPHY_SYNC_TOKEN_LENGTH);
   request_body = g_strconcat ("{\"authPW\": \"",
                               authPW_hex,
                               "\", \"email\": \"",
@@ -128,34 +129,34 @@ ephy_sync_service_stretch (EphySyncService *self,
 
 LOG ("%s:%d", __func__, __LINE__);
 
-  salt_stretch = ephy_sync_crypto_kwe ("quickStretch", emailUTF8);
-  quickStretchedPW = g_malloc (EPHY_SYNC_SERVICE_TOKEN_LENGTH);
+  salt_stretch = ephy_sync_utils_kwe ("quickStretch", emailUTF8);
+  quickStretchedPW = g_malloc (EPHY_SYNC_TOKEN_LENGTH);
   ephy_sync_crypto_pbkdf2_1k ((guint8 *) passwordUTF8,
                               strlen (passwordUTF8),
                               (guint8 *) salt_stretch,
                               strlen (salt_stretch),
                               quickStretchedPW,
-                              EPHY_SYNC_SERVICE_TOKEN_LENGTH);
+                              EPHY_SYNC_TOKEN_LENGTH);
 
-ephy_sync_crypto_display_hex (quickStretchedPW, EPHY_SYNC_SERVICE_TOKEN_LENGTH, "quickStretchedPW");
+ephy_sync_utils_display_hex ("quickStretchedPW", quickStretchedPW, EPHY_SYNC_TOKEN_LENGTH);
 
-  info_auth = ephy_sync_crypto_kw ("authPW");
+  info_auth = ephy_sync_utils_kw ("authPW");
   ephy_sync_crypto_hkdf (quickStretchedPW,
-                         EPHY_SYNC_SERVICE_TOKEN_LENGTH,
+                         EPHY_SYNC_TOKEN_LENGTH,
                          NULL, 0,
                          (guint8 *) info_auth,
                          strlen (info_auth),
                          authPW,
-                         EPHY_SYNC_SERVICE_TOKEN_LENGTH);
+                         EPHY_SYNC_TOKEN_LENGTH);
 
-  info_unwrap = ephy_sync_crypto_kw ("unwrapBkey");
+  info_unwrap = ephy_sync_utils_kw ("unwrapBkey");
   ephy_sync_crypto_hkdf (quickStretchedPW,
-                         EPHY_SYNC_SERVICE_TOKEN_LENGTH,
+                         EPHY_SYNC_TOKEN_LENGTH,
                          NULL, 0,
                          (guint8 *) info_unwrap,
                          strlen (info_unwrap),
                          unwrapBKey,
-                         EPHY_SYNC_SERVICE_TOKEN_LENGTH);
+                         EPHY_SYNC_TOKEN_LENGTH);
 
   g_free (salt_stretch);
   g_free (info_unwrap);
diff --git a/src/ephy-sync-service.h b/src/ephy-sync-service.h
index 9f6b2a7..bc22597 100644
--- a/src/ephy-sync-service.h
+++ b/src/ephy-sync-service.h
@@ -23,23 +23,24 @@
 
 G_BEGIN_DECLS
 
-#define EPHY_TYPE_SYNC_SERVICE          (ephy_sync_service_get_type ())
-#define EPHY_SYNC_SERVICE_TOKEN_LENGTH  32
+#define EPHY_TYPE_SYNC_SERVICE (ephy_sync_service_get_type ())
 
 G_DECLARE_FINAL_TYPE (EphySyncService, ephy_sync_service, EPHY, SYNC_SERVICE, GObject)
 
-EphySyncService *ephy_sync_service_new          (void);
-void             ephy_sync_service_stretch      (EphySyncService *self,
-                                                 const gchar *emailUTF8,
-                                                 const gchar *passwordUTF8,
-                                                 guint8 *authPW,
-                                                 guint8 *unwrapBKey);
-void             ephy_sync_service_try_login    (EphySyncService *self,
-                                                 gboolean login_with_keys,
-                                                 const gchar *emailUTF8,
-                                                 guint8 *authPW,
-                                                 guint8 *sessionToken,
-                                                 guint8 *keyFetchToken);
+EphySyncService *ephy_sync_service_new       (void);
+
+void             ephy_sync_service_stretch   (EphySyncService *self,
+                                              const gchar *emailUTF8,
+                                              const gchar *passwordUTF8,
+                                              guint8 *authPW,
+                                              guint8 *unwrapBKey);
+
+void             ephy_sync_service_try_login (EphySyncService *self,
+                                              gboolean login_with_keys,
+                                              const gchar *emailUTF8,
+                                              guint8 *authPW,
+                                              guint8 *sessionToken,
+                                              guint8 *keyFetchToken);
 
 G_END_DECLS
 
diff --git a/src/ephy-sync-utils.c b/src/ephy-sync-utils.c
new file mode 100644
index 0000000..f1d82d0
--- /dev/null
+++ b/src/ephy-sync-utils.c
@@ -0,0 +1,64 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ *  Copyright © 2016 Gabriel Ivascu <ivascu gabriel59 gmail com>
+ *
+ *  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
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ephy-debug.h"
+#include "ephy-sync-utils.h"
+
+static const gchar hex_digits[] = "0123456789abcdef";
+
+gchar *
+ephy_sync_utils_kw (const gchar *name)
+{
+  return g_strconcat ("identity.mozilla.com/picl/v1/", name, NULL);
+}
+
+gchar *
+ephy_sync_utils_kwe (const gchar *name,
+                     const gchar *emailUTF8)
+{
+  return g_strconcat ("identity.mozilla.com/picl/v1/", name, ":", emailUTF8, NULL);
+}
+
+gchar *
+ephy_sync_utils_encode_hex (guint8 *data,
+                            gsize   data_length)
+{
+  gchar *retval = g_malloc (data_length * 2 + 1);
+
+  for (gsize i = 0; i < data_length; i++) {
+    guint8 byte = data[i];
+
+    retval[2 * i] = hex_digits[byte >> 4];
+    retval[2 * i + 1] = hex_digits[byte & 0xf];
+  }
+
+  retval[data_length * 2] = 0;
+
+  return retval;
+}
+
+/* FIXME: Only for debugging, remove when no longer needed */
+void
+ephy_sync_utils_display_hex (const gchar *data_name,
+                             guint8      *data,
+                             gsize        data_length)
+{
+LOG ("%s:", data_name);
+for (gsize i = 0; i < data_length; i++)
+  LOG ("%02x", data[i]);
+}
diff --git a/src/ephy-sync-utils.h b/src/ephy-sync-utils.h
new file mode 100644
index 0000000..538bd7b
--- /dev/null
+++ b/src/ephy-sync-utils.h
@@ -0,0 +1,44 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ *  Copyright © 2016 Gabriel Ivascu <ivascu gabriel59 gmail com>
+ *
+ *  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
+ *  the Free Software Foundation; either version 2, or (at your option)
+ *  any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EPHY_SYNC_UTILS_H
+#define EPHY_SYNC_UTILS_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define EPHY_SYNC_TOKEN_LENGTH 32
+
+gchar *ephy_sync_utils_kw          (const gchar *name);
+
+gchar *ephy_sync_utils_kwe         (const gchar *name,
+                                    const gchar *emailUTF8);
+
+gchar *ephy_sync_utils_encode_hex  (guint8 *data,
+                                    gsize   data_length);
+
+/* FIXME: Only for debugging, remove when no longer needed */
+void   ephy_sync_utils_display_hex (const gchar *data_name,
+                                    guint8      *data,
+                                    gsize        data_length);
+
+
+G_END_DECLS
+
+#endif
diff --git a/src/ephy-sync-window.c b/src/ephy-sync-window.c
index 51dc2be..d7e4e78 100644
--- a/src/ephy-sync-window.c
+++ b/src/ephy-sync-window.c
@@ -18,7 +18,7 @@
 
 #include "ephy-debug.h"
 #include "ephy-gui.h"
-#include "ephy-sync-crypto.h"
+#include "ephy-sync-utils.h"
 #include "ephy-sync-window.h"
 
 #include <gtk/gtk.h>
@@ -71,26 +71,26 @@ LOG ("password: %s", passwordUTF8);
     passwordUTF8 = g_strdup ("pässwörd");
   }
 
-  authPW = g_malloc (EPHY_SYNC_SERVICE_TOKEN_LENGTH);
-  unwrapBKey = g_malloc (EPHY_SYNC_SERVICE_TOKEN_LENGTH);
+  authPW = g_malloc (EPHY_SYNC_TOKEN_LENGTH);
+  unwrapBKey = g_malloc (EPHY_SYNC_TOKEN_LENGTH);
   ephy_sync_service_stretch (self->sync_service,
                              emailUTF8,
                              passwordUTF8,
                              authPW,
                              unwrapBKey);
-ephy_sync_crypto_display_hex (authPW, EPHY_SYNC_SERVICE_TOKEN_LENGTH, "authPW");
-ephy_sync_crypto_display_hex (unwrapBKey, EPHY_SYNC_SERVICE_TOKEN_LENGTH, "unwrapBKey");
+ephy_sync_utils_display_hex ("authPW", authPW, EPHY_SYNC_TOKEN_LENGTH);
+ephy_sync_utils_display_hex ("unwrapBKey", unwrapBKey, EPHY_SYNC_TOKEN_LENGTH);
 
-  sessionToken = g_malloc (EPHY_SYNC_SERVICE_TOKEN_LENGTH);
-  keyFetchToken = g_malloc (EPHY_SYNC_SERVICE_TOKEN_LENGTH);
+  sessionToken = g_malloc (EPHY_SYNC_TOKEN_LENGTH);
+  keyFetchToken = g_malloc (EPHY_SYNC_TOKEN_LENGTH);
   ephy_sync_service_try_login (self->sync_service,
                                FALSE,
                                emailUTF8,
                                authPW,
                                sessionToken,
                                keyFetchToken);
-ephy_sync_crypto_display_hex (sessionToken, EPHY_SYNC_SERVICE_TOKEN_LENGTH, "sessionToken");
-ephy_sync_crypto_display_hex (keyFetchToken, EPHY_SYNC_SERVICE_TOKEN_LENGTH, "keyFetchToken");
+ephy_sync_utils_display_hex ("sessionToken", sessionToken, EPHY_SYNC_TOKEN_LENGTH);
+ephy_sync_utils_display_hex ("keyFetchToken", keyFetchToken, EPHY_SYNC_TOKEN_LENGTH);
 
   g_free (authPW);
   g_free (unwrapBKey);
diff --git a/src/ephy-sync-window.h b/src/ephy-sync-window.h
index 0f95448..42f19df 100644
--- a/src/ephy-sync-window.h
+++ b/src/ephy-sync-window.h
@@ -29,7 +29,7 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (EphySyncWindow, ephy_sync_window, EPHY, SYNC_WINDOW, GtkDialog)
 
-GtkWidget       *ephy_sync_window_new        (EphySyncService *sync_service);
+GtkWidget *ephy_sync_window_new (EphySyncService *sync_service);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]