summaryrefslogtreecommitdiff
path: root/src/core/misc.c
diff options
context:
space:
mode:
authorAlexander Færøy <ahf@0x90.dk>2016-10-16 13:20:14 +0200
committerAlexander Færøy <ahf@0x90.dk>2016-10-22 20:36:50 +0200
commitda67d3e8e69eb5fb702a3dd39356d38a1ee9d8cd (patch)
treeb3faa43fd92fd001e09ce5b5007330e7bf669dae /src/core/misc.c
parent6300dfec71d376c96351708f76a6c4ee4a187eb5 (diff)
downloadirssi-da67d3e8e69eb5fb702a3dd39356d38a1ee9d8cd.zip
Add function to convert a buffer to a colon-delimited hex string.
This patch adds binary_to_hex(), which can take an input buffer and convert it to colon-delimited hex strings suitable for printing for fingerprints.
Diffstat (limited to 'src/core/misc.c')
-rw-r--r--src/core/misc.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/core/misc.c b/src/core/misc.c
index bc9f504e..0bb1f7e6 100644
--- a/src/core/misc.c
+++ b/src/core/misc.c
@@ -930,3 +930,23 @@ char **strsplit_len(const char *str, int len, gboolean onspace)
return ret;
}
+
+char *binary_to_hex(unsigned char *buffer, size_t size)
+{
+ static const char hex[] = "0123456789ABCDEF";
+ char *result = NULL;
+ int i;
+
+ if (buffer == NULL || size == 0)
+ return NULL;
+
+ result = g_malloc(3 * size);
+
+ for (i = 0; i < size; i++) {
+ result[i * 3 + 0] = hex[(buffer[i] >> 4) & 0xf];
+ result[i * 3 + 1] = hex[(buffer[i] >> 0) & 0xf];
+ result[i * 3 + 2] = i == size - 1 ? '\0' : ':';
+ }
+
+ return result;
+}