mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-09 12:25:48 +01:00
gchecksum: Add g_compute_checksum_for_bytes()
* Add a GBytes based version of g_compute_checksum_for_data() * Add appropriate tests https://bugzilla.gnome.org/show_bug.cgi?id=680912
This commit is contained in:
parent
dd2ecf7488
commit
278fe0c67f
@ -2845,6 +2845,7 @@ g_checksum_get_digest
|
|||||||
<SUBSECTION>
|
<SUBSECTION>
|
||||||
g_compute_checksum_for_data
|
g_compute_checksum_for_data
|
||||||
g_compute_checksum_for_string
|
g_compute_checksum_for_string
|
||||||
|
g_compute_checksum_for_bytes
|
||||||
</SECTION>
|
</SECTION>
|
||||||
|
|
||||||
<SECTION>
|
<SECTION>
|
||||||
|
@ -1462,3 +1462,33 @@ g_compute_checksum_for_string (GChecksumType checksum_type,
|
|||||||
|
|
||||||
return g_compute_checksum_for_data (checksum_type, (const guchar *) str, length);
|
return g_compute_checksum_for_data (checksum_type, (const guchar *) str, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_compute_checksum_for_bytes:
|
||||||
|
* @checksum_type: a #GChecksumType
|
||||||
|
* @data: binary blob to compute the digest of
|
||||||
|
*
|
||||||
|
* Computes the checksum for a binary @data. This is a
|
||||||
|
* convenience wrapper for g_checksum_new(), g_checksum_get_string()
|
||||||
|
* and g_checksum_free().
|
||||||
|
*
|
||||||
|
* The hexadecimal string returned will be in lower case.
|
||||||
|
*
|
||||||
|
* Return value: the digest of the binary data as a string in hexadecimal.
|
||||||
|
* The returned string should be freed with g_free() when done using it.
|
||||||
|
*
|
||||||
|
* Since: 2.34
|
||||||
|
*/
|
||||||
|
gchar *
|
||||||
|
g_compute_checksum_for_bytes (GChecksumType checksum_type,
|
||||||
|
GBytes *data)
|
||||||
|
{
|
||||||
|
gconstpointer byte_data;
|
||||||
|
gsize length;
|
||||||
|
|
||||||
|
g_return_val_if_fail (IS_VALID_TYPE (checksum_type), NULL);
|
||||||
|
g_return_val_if_fail (data != NULL, NULL);
|
||||||
|
|
||||||
|
byte_data = g_bytes_get_data (data, &length);
|
||||||
|
return g_compute_checksum_for_data (checksum_type, byte_data, length);
|
||||||
|
}
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
#define __G_CHECKSUM_H__
|
#define __G_CHECKSUM_H__
|
||||||
|
|
||||||
#include <glib/gtypes.h>
|
#include <glib/gtypes.h>
|
||||||
|
#include <glib/gbytes.h>
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
@ -81,6 +82,10 @@ gchar *g_compute_checksum_for_string (GChecksumType checksum_t
|
|||||||
const gchar *str,
|
const gchar *str,
|
||||||
gssize length);
|
gssize length);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_34
|
||||||
|
gchar *g_compute_checksum_for_bytes (GChecksumType checksum_type,
|
||||||
|
GBytes *data);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
#endif /* __G_CHECKSUM_H__ */
|
#endif /* __G_CHECKSUM_H__ */
|
||||||
|
@ -169,6 +169,7 @@ g_checksum_get_string
|
|||||||
g_checksum_get_digest
|
g_checksum_get_digest
|
||||||
g_compute_checksum_for_data
|
g_compute_checksum_for_data
|
||||||
g_compute_checksum_for_string
|
g_compute_checksum_for_string
|
||||||
|
g_compute_checksum_for_bytes
|
||||||
g_completion_add_items
|
g_completion_add_items
|
||||||
g_completion_clear_items
|
g_completion_clear_items
|
||||||
g_completion_complete
|
g_completion_complete
|
||||||
|
@ -700,12 +700,12 @@ test_checksum_reset (gconstpointer d)
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
GChecksumType checksum_type;
|
GChecksumType checksum_type;
|
||||||
const gchar **sums;
|
const gchar **sums;
|
||||||
} ChecksumStringTest;
|
} ChecksumComputeTest;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_checksum_string (gconstpointer d)
|
test_checksum_string (gconstpointer d)
|
||||||
{
|
{
|
||||||
const ChecksumStringTest *test = d;
|
const ChecksumComputeTest *test = d;
|
||||||
int length;
|
int length;
|
||||||
gchar *checksum;
|
gchar *checksum;
|
||||||
|
|
||||||
@ -725,6 +725,25 @@ test_checksum_string (gconstpointer d)
|
|||||||
g_free (checksum);
|
g_free (checksum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_checksum_bytes (gconstpointer d)
|
||||||
|
{
|
||||||
|
const ChecksumComputeTest *test = d;
|
||||||
|
GBytes *input;
|
||||||
|
int length;
|
||||||
|
gchar *checksum;
|
||||||
|
|
||||||
|
for (length = 0; length <= FIXED_LEN; length++)
|
||||||
|
{
|
||||||
|
input = g_bytes_new_static (FIXED_STR, length);
|
||||||
|
checksum = g_compute_checksum_for_bytes (test->checksum_type, input);
|
||||||
|
g_bytes_unref (input);
|
||||||
|
|
||||||
|
g_assert_cmpstr (checksum, ==, test->sums[length]);
|
||||||
|
g_free (checksum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
add_checksum_test (GChecksumType type,
|
add_checksum_test (GChecksumType type,
|
||||||
const char *type_name,
|
const char *type_name,
|
||||||
@ -750,9 +769,9 @@ add_checksum_string_test (GChecksumType type,
|
|||||||
const gchar *type_name,
|
const gchar *type_name,
|
||||||
const gchar **sums)
|
const gchar **sums)
|
||||||
{
|
{
|
||||||
ChecksumStringTest *test;
|
ChecksumComputeTest *test;
|
||||||
gchar *path;
|
gchar *path;
|
||||||
test = g_new0 (ChecksumStringTest, 1);
|
test = g_new0 (ChecksumComputeTest, 1);
|
||||||
test->checksum_type = type;
|
test->checksum_type = type;
|
||||||
test->sums = sums;
|
test->sums = sums;
|
||||||
path = g_strdup_printf ("/checksum/%s/string", type_name);
|
path = g_strdup_printf ("/checksum/%s/string", type_name);
|
||||||
@ -760,6 +779,21 @@ add_checksum_string_test (GChecksumType type,
|
|||||||
g_free (path);
|
g_free (path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
add_checksum_bytes_test (GChecksumType type,
|
||||||
|
const gchar *type_name,
|
||||||
|
const gchar **sums)
|
||||||
|
{
|
||||||
|
ChecksumComputeTest *test;
|
||||||
|
gchar *path;
|
||||||
|
test = g_new0 (ChecksumComputeTest, 1);
|
||||||
|
test->checksum_type = type;
|
||||||
|
test->sums = sums;
|
||||||
|
path = g_strdup_printf ("/checksum/%s/bytes", type_name);
|
||||||
|
g_test_add_data_func (path, test, test_checksum_bytes);
|
||||||
|
g_free (path);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_unsupported (void)
|
test_unsupported (void)
|
||||||
{
|
{
|
||||||
@ -779,14 +813,17 @@ main (int argc, char *argv[])
|
|||||||
for (length = 0; length <= FIXED_LEN; length++)
|
for (length = 0; length <= FIXED_LEN; length++)
|
||||||
add_checksum_test (G_CHECKSUM_MD5, "MD5", MD5_sums[length], length);
|
add_checksum_test (G_CHECKSUM_MD5, "MD5", MD5_sums[length], length);
|
||||||
add_checksum_string_test (G_CHECKSUM_MD5, "MD5", MD5_sums);
|
add_checksum_string_test (G_CHECKSUM_MD5, "MD5", MD5_sums);
|
||||||
|
add_checksum_bytes_test (G_CHECKSUM_MD5, "MD5", MD5_sums);
|
||||||
|
|
||||||
for (length = 0; length <= FIXED_LEN; length++)
|
for (length = 0; length <= FIXED_LEN; length++)
|
||||||
add_checksum_test (G_CHECKSUM_SHA1, "SHA1", SHA1_sums[length], length);
|
add_checksum_test (G_CHECKSUM_SHA1, "SHA1", SHA1_sums[length], length);
|
||||||
add_checksum_string_test (G_CHECKSUM_SHA1, "SHA1", SHA1_sums);
|
add_checksum_string_test (G_CHECKSUM_SHA1, "SHA1", SHA1_sums);
|
||||||
|
add_checksum_bytes_test (G_CHECKSUM_SHA1, "SHA1", SHA1_sums);
|
||||||
|
|
||||||
for (length = 0; length <= FIXED_LEN; length++)
|
for (length = 0; length <= FIXED_LEN; length++)
|
||||||
add_checksum_test (G_CHECKSUM_SHA256, "SHA256", SHA256_sums[length], length);
|
add_checksum_test (G_CHECKSUM_SHA256, "SHA256", SHA256_sums[length], length);
|
||||||
add_checksum_string_test (G_CHECKSUM_SHA256, "SHA256", SHA256_sums);
|
add_checksum_string_test (G_CHECKSUM_SHA256, "SHA256", SHA256_sums);
|
||||||
|
add_checksum_bytes_test (G_CHECKSUM_SHA256, "SHA256", SHA256_sums);
|
||||||
|
|
||||||
return g_test_run ();
|
return g_test_run ();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user