From 183ed8a3f8e663c149b2a48add3b60eff3a1a71e Mon Sep 17 00:00:00 2001 From: Giovanni Campagna Date: Wed, 20 Apr 2016 11:47:46 -0700 Subject: [PATCH] Add g_compute_hmac_for_bytes() As an introspection-friendly GBytes variant of g_compute_hmac_for_data() https://bugzilla.gnome.org/show_bug.cgi?id=765338 --- docs/reference/glib/glib-sections.txt | 1 + glib/ghmac.c | 36 +++++++++++++++++++++++++++ glib/ghmac.h | 5 ++++ 3 files changed, 42 insertions(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 398ca95c9..463603623 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -3011,6 +3011,7 @@ g_hmac_get_digest g_compute_hmac_for_data g_compute_hmac_for_string +g_compute_hmac_for_bytes
diff --git a/glib/ghmac.c b/glib/ghmac.c index d3334d0b4..bc498ffd7 100644 --- a/glib/ghmac.c +++ b/glib/ghmac.c @@ -370,6 +370,42 @@ g_compute_hmac_for_data (GChecksumType digest_type, return retval; } +/** + * g_compute_hmac_for_bytes: + * @digest_type: a #GChecksumType to use for the HMAC + * @key: the key to use in the HMAC + * @data: binary blob to compute the HMAC of + * + * Computes the HMAC for a binary @data. This is a + * convenience wrapper for g_hmac_new(), g_hmac_get_string() + * and g_hmac_unref(). + * + * The hexadecimal string returned will be in lower case. + * + * Returns: the HMAC of the binary data as a string in hexadecimal. + * The returned string should be freed with g_free() when done using it. + * + * Since: 2.50 + */ +gchar * +g_compute_hmac_for_bytes (GChecksumType digest_type, + GBytes *key, + GBytes *data) +{ + gconstpointer byte_data; + gsize length; + gconstpointer key_data; + gsize key_len; + + g_return_val_if_fail (data != NULL, NULL); + g_return_val_if_fail (key != NULL, NULL); + + byte_data = g_bytes_get_data (data, &length); + key_data = g_bytes_get_data (key, &key_len); + return g_compute_hmac_for_data (digest_type, key_data, key_len, byte_data, length); +} + + /** * g_compute_hmac_for_string: * @digest_type: a #GChecksumType to use for the HMAC diff --git a/glib/ghmac.h b/glib/ghmac.h index 01d53aa31..e099a081a 100644 --- a/glib/ghmac.h +++ b/glib/ghmac.h @@ -72,6 +72,11 @@ gchar *g_compute_hmac_for_string (GChecksumType digest_type, gsize key_len, const gchar *str, gssize length); +GLIB_AVAILABLE_IN_2_50 +gchar *g_compute_hmac_for_bytes (GChecksumType digest_type, + GBytes *key, + GBytes *data); + G_END_DECLS