Last change
on this file since 0ca1d79 was
34afea7,
checked in by dequis <dx@…>, at 2015-01-31T23:58:57Z
|
Use glib's GChecksum for md5/sha1
This changes behavior slightly:
- md5_init()/sha1_init() allocate a GChecksum
- md5_finish()/sha1_finish() close and free() it
- md5_digest_keep() was added (no sha1 equivalent needed)
And yes, glib has this concept of "closing" the GChecksum, which means
it can't be used anymore after g_checksum_get_digest().
jabber_cache_add() actually seems to need to do that to generate some
random-ish values, so i kept that working by adding a md5_digest_keep()
function that copies the GChecksum before it gets closed
GChecksum was introduced in glib 2.16, so the configure script version
was bumped. We were already depending on glib 2.16 accidentally
(some post-3.2.2 code uses GHashTableIter)
|
-
Property mode set to
100644
|
File size:
926 bytes
|
Line | |
---|
1 | #include "md5.h" |
---|
2 | |
---|
3 | /* Creates a new GChecksum in ctx */ |
---|
4 | void md5_init(md5_state_t *ctx) |
---|
5 | { |
---|
6 | *ctx = g_checksum_new(G_CHECKSUM_MD5); |
---|
7 | } |
---|
8 | |
---|
9 | /* Wrapper for g_checksum_update */ |
---|
10 | void md5_append(md5_state_t *ctx, const guint8 *buf, unsigned int len) |
---|
11 | { |
---|
12 | g_checksum_update(*ctx, buf, len); |
---|
13 | } |
---|
14 | |
---|
15 | /* Wrapper for g_checksum_get_digest |
---|
16 | * Also takes care of g_checksum_free(), since it can't be reused anyway |
---|
17 | * (the GChecksum is closed after get_digest) */ |
---|
18 | void md5_finish(md5_state_t *ctx, guint8 digest[MD5_HASH_SIZE]) |
---|
19 | { |
---|
20 | gsize digest_len = MD5_HASH_SIZE; |
---|
21 | g_checksum_get_digest(*ctx, digest, &digest_len); |
---|
22 | g_checksum_free(*ctx); |
---|
23 | } |
---|
24 | |
---|
25 | /* Variant of md5_finish that copies the GChecksum |
---|
26 | * and finishes that one instead of the original */ |
---|
27 | void md5_digest_keep(md5_state_t *ctx, guint8 digest[MD5_HASH_SIZE]) |
---|
28 | { |
---|
29 | md5_state_t copy = g_checksum_copy(*ctx); |
---|
30 | md5_finish(©, digest); |
---|
31 | } |
---|
32 | |
---|
33 | void md5_free(md5_state_t *ctx) |
---|
34 | { |
---|
35 | g_checksum_free(*ctx); |
---|
36 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.