[epiphany/wip/ephy-sync] sync-crypto: Generate random strings directly from /dev/urandom



commit 01b376794a5e3d7a7fef61646b37bb23f5e65a5a
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Mon Aug 8 21:12:06 2016 +0300

    sync-crypto: Generate random strings directly from /dev/urandom

 src/ephy-sync-crypto.c |   26 +++++++++++++++-----------
 1 files changed, 15 insertions(+), 11 deletions(-)
---
diff --git a/src/ephy-sync-crypto.c b/src/ephy-sync-crypto.c
index c6e5cdb..43e9644 100644
--- a/src/ephy-sync-crypto.c
+++ b/src/ephy-sync-crypto.c
@@ -947,22 +947,26 @@ out:
 gchar *
 ephy_sync_crypto_generate_random_string (gsize length)
 {
-  guchar *bytes;
-  gchar *base64_string;
-  gchar *string;
+  FILE *fp;
+  gsize num_bytes;
+  guint8 *bytes;
+  gchar *hex;
+  gchar *out;
 
-  bytes = g_malloc (length);
-  for (gsize i = 0; i < length; i++)
-    bytes[i] = g_random_int ();
+  g_assert (length > 0);
+  num_bytes = (length + 1) / 2;
+  bytes = g_malloc (num_bytes);
 
-  base64_string = g_base64_encode (bytes, length);
-  base64_to_base64_urlsafe (base64_string);
-  string = g_strndup (base64_string, length);
+  fp = fopen ("/dev/urandom", "r");
+  fread (bytes, sizeof (guint8), num_bytes, fp);
+  hex = ephy_sync_crypto_encode_hex (bytes, num_bytes);
+  out = g_strndup (hex, length);
 
   g_free (bytes);
-  g_free (base64_string);
+  g_free (hex);
+  fclose (fp);
 
-  return string;
+  return out;
 }
 
 gchar *


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