aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-06-10 09:27:25 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-06-10 09:27:25 +0100
commit42c732f32a45f4f3dbea262eff6b251a839eab15 (patch)
treed14f27bd725f7ee3beedebbabd4079cd64e97424 /src/libutil
parentfb326efc2b3fa1c25705d218987199a608b87b87 (diff)
downloadrspamd-42c732f32a45f4f3dbea262eff6b251a839eab15.tar.gz
rspamd-42c732f32a45f4f3dbea262eff6b251a839eab15.zip
[Feature] Add function to generate random hex string
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.c19
-rw-r--r--src/libutil/util.h7
2 files changed, 26 insertions, 0 deletions
diff --git a/src/libutil/util.c b/src/libutil/util.c
index 4ce90ba06..212cb7f1b 100644
--- a/src/libutil/util.c
+++ b/src/libutil/util.c
@@ -1875,6 +1875,25 @@ randombytes (guchar *buf, guint64 len)
ottery_rand_bytes (buf, (size_t)len);
}
+void
+rspamd_random_hex (guchar *buf, guint64 len)
+{
+ static const gchar hexdigests[16] = "0123456789abcdef";
+ gint64 i;
+
+ g_assert (len > 0);
+
+ ottery_rand_bytes (buf, (len / 2.0 + 0.5));
+
+ for (i = (gint64)len - 1; i >= 0; i -= 2) {
+ buf[i] = hexdigests[buf[i / 2] & 0xf];
+
+ if (i > 0) {
+ buf[i - 1] = hexdigests[(buf[i / 2] >> 4) & 0xf];
+ }
+ }
+}
+
void
rspamd_ptr_array_free_hard (gpointer p)
diff --git a/src/libutil/util.h b/src/libutil/util.h
index 48e91cb4f..5c0e82a0d 100644
--- a/src/libutil/util.h
+++ b/src/libutil/util.h
@@ -405,6 +405,13 @@ void rspamd_deinit_libs (struct rspamd_external_libs_ctx *ctx);
guint64 rspamd_hash_seed (void);
/**
+ * Returns random hex string of the specified length
+ * @param buf
+ * @param len
+ */
+void rspamd_random_hex (guchar *buf, guint64 len);
+
+/**
* Return jittered time value
*/
gdouble rspamd_time_jitter (gdouble in, gdouble jitter);