GDelayedSettingsBackend: another mandatory fixup
[glib.git] / glib / gchecksum.c
blobad9072790c2b2786010746c68e32b178a059ba44
1 /* gchecksum.h - data hashing functions
3 * Copyright (C) 2007 Emmanuele Bassi <ebassi@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "config.h"
23 #include <string.h>
25 #include "glibconfig.h"
26 #include "gchecksum.h"
27 #include "glib.h"
28 #include "glibintl.h"
30 #include "galias.h"
32 /**
33 * SECTION: checksum
34 * @title: Data Checksums
35 * @short_description: Computes the checksum for data
37 * GLib provides a generic API for computing checksums (or "digests")
38 * for a sequence of arbitrary bytes, using various hashing algorithms
39 * like MD5, SHA-1 and SHA-256. Checksums are commonly used in various
40 * environments and specifications.
42 * GLib supports incremental checksums using the GChecksum data
43 * structure, by calling g_checksum_update() as long as there's data
44 * available and then using g_checksum_get_string() or
45 * g_checksum_get_digest() to compute the checksum and return it either
46 * as a string in hexadecimal form, or as a raw sequence of bytes. To
47 * compute the checksum for binary blobs and NUL-terminated strings in
48 * one go, use the convenience functions g_compute_checksum_for_data()
49 * and g_compute_checksum_for_string(), respectively.
51 * Support for checksums has been added in GLib 2.16
52 **/
54 #define IS_VALID_TYPE(type) ((type) >= G_CHECKSUM_MD5 && (type) <= G_CHECKSUM_SHA256)
56 /* The fact that these are lower case characters is part of the ABI */
57 static const gchar hex_digits[] = "0123456789abcdef";
59 #define MD5_DATASIZE 64
60 #define MD5_DIGEST_LEN 16
62 typedef struct
64 guint32 buf[4];
65 guint32 bits[2];
67 guchar data[MD5_DATASIZE];
69 guchar digest[MD5_DIGEST_LEN];
70 } Md5sum;
72 #define SHA1_DATASIZE 64
73 #define SHA1_DIGEST_LEN 20
75 typedef struct
77 guint32 buf[5];
78 guint32 bits[2];
80 /* we pack 64 unsigned chars into 16 32-bit unsigned integers */
81 guint32 data[16];
83 guchar digest[SHA1_DIGEST_LEN];
84 } Sha1sum;
86 #define SHA256_DATASIZE 64
87 #define SHA256_DIGEST_LEN 32
89 typedef struct
91 guint32 buf[8];
92 guint32 bits[2];
94 guint8 data[SHA256_DATASIZE];
96 guchar digest[SHA256_DIGEST_LEN];
97 } Sha256sum;
99 struct _GChecksum
101 GChecksumType type;
103 gchar *digest_str;
105 union {
106 Md5sum md5;
107 Sha1sum sha1;
108 Sha256sum sha256;
109 } sum;
112 /* we need different byte swapping functions because MD5 expects buffers
113 * to be little-endian, while SHA1 and SHA256 expect them in big-endian
114 * form.
117 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
118 #define md5_byte_reverse(buffer,length)
119 #else
120 /* assume that the passed buffer is integer aligned */
121 static inline void
122 md5_byte_reverse (guchar *buffer,
123 gulong length)
125 guint32 bit;
129 bit = (guint32) ((unsigned) buffer[3] << 8 | buffer[2]) << 16 |
130 ((unsigned) buffer[1] << 8 | buffer[0]);
131 * (guint32 *) buffer = bit;
132 buffer += 4;
134 while (--length);
136 #endif /* G_BYTE_ORDER == G_LITTLE_ENDIAN */
138 #if G_BYTE_ORDER == G_BIG_ENDIAN
139 #define sha_byte_reverse(buffer,length)
140 #else
141 static inline void
142 sha_byte_reverse (guint32 *buffer,
143 gint length)
145 length /= sizeof (guint32);
146 while (length--)
148 *buffer = GUINT32_SWAP_LE_BE (*buffer);
149 ++buffer;
152 #endif /* G_BYTE_ORDER == G_BIG_ENDIAN */
154 static gchar *
155 digest_to_string (guint8 *digest,
156 gsize digest_len)
158 gint len = digest_len * 2;
159 gint i;
160 gchar *retval;
162 retval = g_new (gchar, len + 1);
164 for (i = 0; i < digest_len; i++)
166 guint8 byte = digest[i];
168 retval[2 * i] = hex_digits[byte >> 4];
169 retval[2 * i + 1] = hex_digits[byte & 0xf];
172 retval[len] = 0;
174 return retval;
178 * MD5 Checksum
181 /* This MD5 digest computation is based on the equivalent code
182 * written by Colin Plumb. It came with this notice:
184 * This code implements the MD5 message-digest algorithm.
185 * The algorithm is due to Ron Rivest. This code was
186 * written by Colin Plumb in 1993, no copyright is claimed.
187 * This code is in the public domain; do with it what you wish.
189 * Equivalent code is available from RSA Data Security, Inc.
190 * This code has been tested against that, and is equivalent,
191 * except that you don't need to include two pages of legalese
192 * with every copy.
195 static void
196 md5_sum_init (Md5sum *md5)
198 /* arbitrary constants */
199 md5->buf[0] = 0x67452301;
200 md5->buf[1] = 0xefcdab89;
201 md5->buf[2] = 0x98badcfe;
202 md5->buf[3] = 0x10325476;
204 md5->bits[0] = md5->bits[1] = 0;
208 * The core of the MD5 algorithm, this alters an existing MD5 hash to
209 * reflect the addition of 16 longwords of new data. md5_sum_update()
210 * blocks the data and converts bytes into longwords for this routine.
212 static void
213 md5_transform (guint32 buf[4],
214 guint32 const in[16])
216 register guint32 a, b, c, d;
218 /* The four core functions - F1 is optimized somewhat */
219 #define F1(x, y, z) (z ^ (x & (y ^ z)))
220 #define F2(x, y, z) F1 (z, x, y)
221 #define F3(x, y, z) (x ^ y ^ z)
222 #define F4(x, y, z) (y ^ (x | ~z))
224 /* This is the central step in the MD5 algorithm. */
225 #define md5_step(f, w, x, y, z, data, s) \
226 ( w += f (x, y, z) + data, w = w << s | w >> (32 - s), w += x )
228 a = buf[0];
229 b = buf[1];
230 c = buf[2];
231 d = buf[3];
233 md5_step (F1, a, b, c, d, in[0] + 0xd76aa478, 7);
234 md5_step (F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
235 md5_step (F1, c, d, a, b, in[2] + 0x242070db, 17);
236 md5_step (F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
237 md5_step (F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
238 md5_step (F1, d, a, b, c, in[5] + 0x4787c62a, 12);
239 md5_step (F1, c, d, a, b, in[6] + 0xa8304613, 17);
240 md5_step (F1, b, c, d, a, in[7] + 0xfd469501, 22);
241 md5_step (F1, a, b, c, d, in[8] + 0x698098d8, 7);
242 md5_step (F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
243 md5_step (F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
244 md5_step (F1, b, c, d, a, in[11] + 0x895cd7be, 22);
245 md5_step (F1, a, b, c, d, in[12] + 0x6b901122, 7);
246 md5_step (F1, d, a, b, c, in[13] + 0xfd987193, 12);
247 md5_step (F1, c, d, a, b, in[14] + 0xa679438e, 17);
248 md5_step (F1, b, c, d, a, in[15] + 0x49b40821, 22);
250 md5_step (F2, a, b, c, d, in[1] + 0xf61e2562, 5);
251 md5_step (F2, d, a, b, c, in[6] + 0xc040b340, 9);
252 md5_step (F2, c, d, a, b, in[11] + 0x265e5a51, 14);
253 md5_step (F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
254 md5_step (F2, a, b, c, d, in[5] + 0xd62f105d, 5);
255 md5_step (F2, d, a, b, c, in[10] + 0x02441453, 9);
256 md5_step (F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
257 md5_step (F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
258 md5_step (F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
259 md5_step (F2, d, a, b, c, in[14] + 0xc33707d6, 9);
260 md5_step (F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
261 md5_step (F2, b, c, d, a, in[8] + 0x455a14ed, 20);
262 md5_step (F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
263 md5_step (F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
264 md5_step (F2, c, d, a, b, in[7] + 0x676f02d9, 14);
265 md5_step (F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
267 md5_step (F3, a, b, c, d, in[5] + 0xfffa3942, 4);
268 md5_step (F3, d, a, b, c, in[8] + 0x8771f681, 11);
269 md5_step (F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
270 md5_step (F3, b, c, d, a, in[14] + 0xfde5380c, 23);
271 md5_step (F3, a, b, c, d, in[1] + 0xa4beea44, 4);
272 md5_step (F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
273 md5_step (F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
274 md5_step (F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
275 md5_step (F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
276 md5_step (F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
277 md5_step (F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
278 md5_step (F3, b, c, d, a, in[6] + 0x04881d05, 23);
279 md5_step (F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
280 md5_step (F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
281 md5_step (F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
282 md5_step (F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
284 md5_step (F4, a, b, c, d, in[0] + 0xf4292244, 6);
285 md5_step (F4, d, a, b, c, in[7] + 0x432aff97, 10);
286 md5_step (F4, c, d, a, b, in[14] + 0xab9423a7, 15);
287 md5_step (F4, b, c, d, a, in[5] + 0xfc93a039, 21);
288 md5_step (F4, a, b, c, d, in[12] + 0x655b59c3, 6);
289 md5_step (F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
290 md5_step (F4, c, d, a, b, in[10] + 0xffeff47d, 15);
291 md5_step (F4, b, c, d, a, in[1] + 0x85845dd1, 21);
292 md5_step (F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
293 md5_step (F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
294 md5_step (F4, c, d, a, b, in[6] + 0xa3014314, 15);
295 md5_step (F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
296 md5_step (F4, a, b, c, d, in[4] + 0xf7537e82, 6);
297 md5_step (F4, d, a, b, c, in[11] + 0xbd3af235, 10);
298 md5_step (F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
299 md5_step (F4, b, c, d, a, in[9] + 0xeb86d391, 21);
301 buf[0] += a;
302 buf[1] += b;
303 buf[2] += c;
304 buf[3] += d;
306 #undef F1
307 #undef F2
308 #undef F3
309 #undef F4
310 #undef md5_step
313 static void
314 md5_sum_update (Md5sum *md5,
315 const guchar *data,
316 gsize length)
318 guint32 bit;
320 bit = md5->bits[0];
321 md5->bits[0] = bit + ((guint32) length << 3);
323 /* carry from low to high */
324 if (md5->bits[0] < bit)
325 md5->bits[1] += 1;
327 md5->bits[1] += length >> 29;
329 /* bytes already in Md5sum->data */
330 bit = (bit >> 3) & 0x3f;
332 /* handle any leading odd-sized chunks */
333 if (bit)
335 guchar *p = (guchar *) md5->data + bit;
337 bit = MD5_DATASIZE - bit;
338 if (length < bit)
340 memcpy (p, data, length);
341 return;
344 memcpy (p, data, bit);
346 md5_byte_reverse (md5->data, 16);
347 md5_transform (md5->buf, (guint32 *) md5->data);
349 data += bit;
350 length -= bit;
353 /* process data in 64-byte chunks */
354 while (length >= MD5_DATASIZE)
356 memcpy (md5->data, data, MD5_DATASIZE);
358 md5_byte_reverse (md5->data, 16);
359 md5_transform (md5->buf, (guint32 *) md5->data);
361 data += MD5_DATASIZE;
362 length -= MD5_DATASIZE;
365 /* handle any remaining bytes of data */
366 memcpy (md5->data, data, length);
369 /* closes a checksum */
370 static void
371 md5_sum_close (Md5sum *md5)
373 guint count;
374 guchar *p;
376 /* Compute number of bytes mod 64 */
377 count = (md5->bits[0] >> 3) & 0x3F;
379 /* Set the first char of padding to 0x80.
380 * This is safe since there is always at least one byte free
382 p = md5->data + count;
383 *p++ = 0x80;
385 /* Bytes of padding needed to make 64 bytes */
386 count = MD5_DATASIZE - 1 - count;
388 /* Pad out to 56 mod 64 */
389 if (count < 8)
391 /* Two lots of padding: Pad the first block to 64 bytes */
392 memset (p, 0, count);
394 md5_byte_reverse (md5->data, 16);
395 md5_transform (md5->buf, (guint32 *) md5->data);
397 /* Now fill the next block with 56 bytes */
398 memset (md5->data, 0, MD5_DATASIZE - 8);
400 else
402 /* Pad block to 56 bytes */
403 memset (p, 0, count - 8);
406 md5_byte_reverse (md5->data, 14);
408 /* Append length in bits and transform */
409 ((guint32 *) md5->data)[14] = md5->bits[0];
410 ((guint32 *) md5->data)[15] = md5->bits[1];
412 md5_transform (md5->buf, (guint32 *) md5->data);
413 md5_byte_reverse ((guchar *) md5->buf, 4);
415 memcpy (md5->digest, md5->buf, 16);
417 /* Reset buffers in case they contain sensitive data */
418 memset (md5->buf, 0, sizeof (md5->buf));
419 memset (md5->data, 0, sizeof (md5->data));
422 static gchar *
423 md5_sum_to_string (Md5sum *md5)
425 return digest_to_string (md5->digest, MD5_DIGEST_LEN);
428 static void
429 md5_sum_digest (Md5sum *md5,
430 guint8 *digest)
432 gint i;
434 for (i = 0; i < MD5_DIGEST_LEN; i++)
435 digest[i] = md5->digest[i];
439 * SHA-1 Checksum
442 /* The following implementation comes from D-Bus dbus-sha.c. I've changed
443 * it to use GLib types and to work more like the MD5 implementation above.
444 * I left the comments to have an history of this code.
445 * -- Emmanuele Bassi, ebassi@gnome.org
448 /* The following comments have the history of where this code
449 * comes from. I actually copied it from GNet in GNOME CVS.
450 * - hp@redhat.com
454 * sha.h : Implementation of the Secure Hash Algorithm
456 * Part of the Python Cryptography Toolkit, version 1.0.0
458 * Copyright (C) 1995, A.M. Kuchling
460 * Distribute and use freely; there are no restrictions on further
461 * dissemination and usage except those imposed by the laws of your
462 * country of residence.
466 /* SHA: NIST's Secure Hash Algorithm */
468 /* Based on SHA code originally posted to sci.crypt by Peter Gutmann
469 in message <30ajo5$oe8@ccu2.auckland.ac.nz>.
470 Modified to test for endianness on creation of SHA objects by AMK.
471 Also, the original specification of SHA was found to have a weakness
472 by NSA/NIST. This code implements the fixed version of SHA.
475 /* Here's the first paragraph of Peter Gutmann's posting:
477 The following is my SHA (FIPS 180) code updated to allow use of the "fixed"
478 SHA, thanks to Jim Gillogly and an anonymous contributor for the information on
479 what's changed in the new version. The fix is a simple change which involves
480 adding a single rotate in the initial expansion function. It is unknown
481 whether this is an optimal solution to the problem which was discovered in the
482 SHA or whether it's simply a bandaid which fixes the problem with a minimum of
483 effort (for example the reengineering of a great many Capstone chips).
486 static void
487 sha1_sum_init (Sha1sum *sha1)
489 /* initialize constants */
490 sha1->buf[0] = 0x67452301L;
491 sha1->buf[1] = 0xEFCDAB89L;
492 sha1->buf[2] = 0x98BADCFEL;
493 sha1->buf[3] = 0x10325476L;
494 sha1->buf[4] = 0xC3D2E1F0L;
496 /* initialize bits */
497 sha1->bits[0] = sha1->bits[1] = 0;
500 /* The SHA f()-functions. */
502 #define f1(x,y,z) (z ^ (x & (y ^ z))) /* Rounds 0-19 */
503 #define f2(x,y,z) (x ^ y ^ z) /* Rounds 20-39 */
504 #define f3(x,y,z) (( x & y) | (z & (x | y))) /* Rounds 40-59 */
505 #define f4(x,y,z) (x ^ y ^ z) /* Rounds 60-79 */
507 /* The SHA Mysterious Constants */
508 #define K1 0x5A827999L /* Rounds 0-19 */
509 #define K2 0x6ED9EBA1L /* Rounds 20-39 */
510 #define K3 0x8F1BBCDCL /* Rounds 40-59 */
511 #define K4 0xCA62C1D6L /* Rounds 60-79 */
513 /* 32-bit rotate left - kludged with shifts */
514 #define ROTL(n,X) (((X) << n ) | ((X) >> (32 - n)))
516 /* The initial expanding function. The hash function is defined over an
517 80-word expanded input array W, where the first 16 are copies of the input
518 data, and the remaining 64 are defined by
520 W[ i ] = W[ i - 16 ] ^ W[ i - 14 ] ^ W[ i - 8 ] ^ W[ i - 3 ]
522 This implementation generates these values on the fly in a circular
523 buffer - thanks to Colin Plumb, colin@nyx10.cs.du.edu for this
524 optimization.
526 The updated SHA changes the expanding function by adding a rotate of 1
527 bit. Thanks to Jim Gillogly, jim@rand.org, and an anonymous contributor
528 for this information */
530 #define expand(W,i) (W[ i & 15 ] = ROTL (1, (W[ i & 15] ^ \
531 W[(i - 14) & 15] ^ \
532 W[(i - 8) & 15] ^ \
533 W[(i - 3) & 15])))
536 /* The prototype SHA sub-round. The fundamental sub-round is:
538 a' = e + ROTL( 5, a ) + f( b, c, d ) + k + data;
539 b' = a;
540 c' = ROTL( 30, b );
541 d' = c;
542 e' = d;
544 but this is implemented by unrolling the loop 5 times and renaming the
545 variables ( e, a, b, c, d ) = ( a', b', c', d', e' ) each iteration.
546 This code is then replicated 20 times for each of the 4 functions, using
547 the next 20 values from the W[] array each time */
549 #define subRound(a, b, c, d, e, f, k, data) \
550 (e += ROTL (5, a) + f(b, c, d) + k + data, b = ROTL (30, b))
552 static void
553 sha1_transform (guint32 buf[5],
554 guint32 in[16])
556 guint32 A, B, C, D, E;
558 A = buf[0];
559 B = buf[1];
560 C = buf[2];
561 D = buf[3];
562 E = buf[4];
564 /* Heavy mangling, in 4 sub-rounds of 20 interations each. */
565 subRound (A, B, C, D, E, f1, K1, in[0]);
566 subRound (E, A, B, C, D, f1, K1, in[1]);
567 subRound (D, E, A, B, C, f1, K1, in[2]);
568 subRound (C, D, E, A, B, f1, K1, in[3]);
569 subRound (B, C, D, E, A, f1, K1, in[4]);
570 subRound (A, B, C, D, E, f1, K1, in[5]);
571 subRound (E, A, B, C, D, f1, K1, in[6]);
572 subRound (D, E, A, B, C, f1, K1, in[7]);
573 subRound (C, D, E, A, B, f1, K1, in[8]);
574 subRound (B, C, D, E, A, f1, K1, in[9]);
575 subRound (A, B, C, D, E, f1, K1, in[10]);
576 subRound (E, A, B, C, D, f1, K1, in[11]);
577 subRound (D, E, A, B, C, f1, K1, in[12]);
578 subRound (C, D, E, A, B, f1, K1, in[13]);
579 subRound (B, C, D, E, A, f1, K1, in[14]);
580 subRound (A, B, C, D, E, f1, K1, in[15]);
581 subRound (E, A, B, C, D, f1, K1, expand (in, 16));
582 subRound (D, E, A, B, C, f1, K1, expand (in, 17));
583 subRound (C, D, E, A, B, f1, K1, expand (in, 18));
584 subRound (B, C, D, E, A, f1, K1, expand (in, 19));
586 subRound (A, B, C, D, E, f2, K2, expand (in, 20));
587 subRound (E, A, B, C, D, f2, K2, expand (in, 21));
588 subRound (D, E, A, B, C, f2, K2, expand (in, 22));
589 subRound (C, D, E, A, B, f2, K2, expand (in, 23));
590 subRound (B, C, D, E, A, f2, K2, expand (in, 24));
591 subRound (A, B, C, D, E, f2, K2, expand (in, 25));
592 subRound (E, A, B, C, D, f2, K2, expand (in, 26));
593 subRound (D, E, A, B, C, f2, K2, expand (in, 27));
594 subRound (C, D, E, A, B, f2, K2, expand (in, 28));
595 subRound (B, C, D, E, A, f2, K2, expand (in, 29));
596 subRound (A, B, C, D, E, f2, K2, expand (in, 30));
597 subRound (E, A, B, C, D, f2, K2, expand (in, 31));
598 subRound (D, E, A, B, C, f2, K2, expand (in, 32));
599 subRound (C, D, E, A, B, f2, K2, expand (in, 33));
600 subRound (B, C, D, E, A, f2, K2, expand (in, 34));
601 subRound (A, B, C, D, E, f2, K2, expand (in, 35));
602 subRound (E, A, B, C, D, f2, K2, expand (in, 36));
603 subRound (D, E, A, B, C, f2, K2, expand (in, 37));
604 subRound (C, D, E, A, B, f2, K2, expand (in, 38));
605 subRound (B, C, D, E, A, f2, K2, expand (in, 39));
607 subRound (A, B, C, D, E, f3, K3, expand (in, 40));
608 subRound (E, A, B, C, D, f3, K3, expand (in, 41));
609 subRound (D, E, A, B, C, f3, K3, expand (in, 42));
610 subRound (C, D, E, A, B, f3, K3, expand (in, 43));
611 subRound (B, C, D, E, A, f3, K3, expand (in, 44));
612 subRound (A, B, C, D, E, f3, K3, expand (in, 45));
613 subRound (E, A, B, C, D, f3, K3, expand (in, 46));
614 subRound (D, E, A, B, C, f3, K3, expand (in, 47));
615 subRound (C, D, E, A, B, f3, K3, expand (in, 48));
616 subRound (B, C, D, E, A, f3, K3, expand (in, 49));
617 subRound (A, B, C, D, E, f3, K3, expand (in, 50));
618 subRound (E, A, B, C, D, f3, K3, expand (in, 51));
619 subRound (D, E, A, B, C, f3, K3, expand (in, 52));
620 subRound (C, D, E, A, B, f3, K3, expand (in, 53));
621 subRound (B, C, D, E, A, f3, K3, expand (in, 54));
622 subRound (A, B, C, D, E, f3, K3, expand (in, 55));
623 subRound (E, A, B, C, D, f3, K3, expand (in, 56));
624 subRound (D, E, A, B, C, f3, K3, expand (in, 57));
625 subRound (C, D, E, A, B, f3, K3, expand (in, 58));
626 subRound (B, C, D, E, A, f3, K3, expand (in, 59));
628 subRound (A, B, C, D, E, f4, K4, expand (in, 60));
629 subRound (E, A, B, C, D, f4, K4, expand (in, 61));
630 subRound (D, E, A, B, C, f4, K4, expand (in, 62));
631 subRound (C, D, E, A, B, f4, K4, expand (in, 63));
632 subRound (B, C, D, E, A, f4, K4, expand (in, 64));
633 subRound (A, B, C, D, E, f4, K4, expand (in, 65));
634 subRound (E, A, B, C, D, f4, K4, expand (in, 66));
635 subRound (D, E, A, B, C, f4, K4, expand (in, 67));
636 subRound (C, D, E, A, B, f4, K4, expand (in, 68));
637 subRound (B, C, D, E, A, f4, K4, expand (in, 69));
638 subRound (A, B, C, D, E, f4, K4, expand (in, 70));
639 subRound (E, A, B, C, D, f4, K4, expand (in, 71));
640 subRound (D, E, A, B, C, f4, K4, expand (in, 72));
641 subRound (C, D, E, A, B, f4, K4, expand (in, 73));
642 subRound (B, C, D, E, A, f4, K4, expand (in, 74));
643 subRound (A, B, C, D, E, f4, K4, expand (in, 75));
644 subRound (E, A, B, C, D, f4, K4, expand (in, 76));
645 subRound (D, E, A, B, C, f4, K4, expand (in, 77));
646 subRound (C, D, E, A, B, f4, K4, expand (in, 78));
647 subRound (B, C, D, E, A, f4, K4, expand (in, 79));
649 /* Build message digest */
650 buf[0] += A;
651 buf[1] += B;
652 buf[2] += C;
653 buf[3] += D;
654 buf[4] += E;
657 #undef K1
658 #undef K2
659 #undef K3
660 #undef K4
661 #undef f1
662 #undef f2
663 #undef f3
664 #undef f4
665 #undef ROTL
666 #undef expand
667 #undef subRound
669 static void
670 sha1_sum_update (Sha1sum *sha1,
671 const guchar *buffer,
672 gsize count)
674 guint32 tmp;
675 guint dataCount;
677 /* Update bitcount */
678 tmp = sha1->bits[0];
679 if ((sha1->bits[0] = tmp + ((guint32) count << 3) ) < tmp)
680 sha1->bits[1] += 1; /* Carry from low to high */
681 sha1->bits[1] += count >> 29;
683 /* Get count of bytes already in data */
684 dataCount = (guint) (tmp >> 3) & 0x3F;
686 /* Handle any leading odd-sized chunks */
687 if (dataCount)
689 guchar *p = (guchar *) sha1->data + dataCount;
691 dataCount = SHA1_DATASIZE - dataCount;
692 if (count < dataCount)
694 memcpy (p, buffer, count);
695 return;
698 memcpy (p, buffer, dataCount);
700 sha_byte_reverse (sha1->data, SHA1_DATASIZE);
701 sha1_transform (sha1->buf, sha1->data);
703 buffer += dataCount;
704 count -= dataCount;
707 /* Process data in SHA1_DATASIZE chunks */
708 while (count >= SHA1_DATASIZE)
710 memcpy (sha1->data, buffer, SHA1_DATASIZE);
712 sha_byte_reverse (sha1->data, SHA1_DATASIZE);
713 sha1_transform (sha1->buf, sha1->data);
715 buffer += SHA1_DATASIZE;
716 count -= SHA1_DATASIZE;
719 /* Handle any remaining bytes of data. */
720 memcpy (sha1->data, buffer, count);
723 /* Final wrapup - pad to SHA_DATASIZE-byte boundary with the bit pattern
724 1 0* (64-bit count of bits processed, MSB-first) */
725 static void
726 sha1_sum_close (Sha1sum *sha1)
728 gint count;
729 guchar *data_p;
731 /* Compute number of bytes mod 64 */
732 count = (gint) ((sha1->bits[0] >> 3) & 0x3f);
734 /* Set the first char of padding to 0x80. This is safe since there is
735 always at least one byte free */
736 data_p = (guchar *) sha1->data + count;
737 *data_p++ = 0x80;
739 /* Bytes of padding needed to make 64 bytes */
740 count = SHA1_DATASIZE - 1 - count;
742 /* Pad out to 56 mod 64 */
743 if (count < 8)
745 /* Two lots of padding: Pad the first block to 64 bytes */
746 memset (data_p, 0, count);
748 sha_byte_reverse (sha1->data, SHA1_DATASIZE);
749 sha1_transform (sha1->buf, sha1->data);
751 /* Now fill the next block with 56 bytes */
752 memset (sha1->data, 0, SHA1_DATASIZE - 8);
754 else
756 /* Pad block to 56 bytes */
757 memset (data_p, 0, count - 8);
760 /* Append length in bits and transform */
761 sha1->data[14] = sha1->bits[1];
762 sha1->data[15] = sha1->bits[0];
764 sha_byte_reverse (sha1->data, SHA1_DATASIZE - 8);
765 sha1_transform (sha1->buf, sha1->data);
766 sha_byte_reverse (sha1->buf, SHA1_DIGEST_LEN);
768 memcpy (sha1->digest, sha1->buf, SHA1_DIGEST_LEN);
770 /* Reset buffers in case they contain sensitive data */
771 memset (sha1->buf, 0, sizeof (sha1->buf));
772 memset (sha1->data, 0, sizeof (sha1->data));
775 static gchar *
776 sha1_sum_to_string (Sha1sum *sha1)
778 return digest_to_string (sha1->digest, SHA1_DIGEST_LEN);
781 static void
782 sha1_sum_digest (Sha1sum *sha1,
783 guint8 *digest)
785 gint i;
787 for (i = 0; i < SHA1_DIGEST_LEN; i++)
788 digest[i] = sha1->digest[i];
792 * SHA-256 Checksum
795 /* adapted from the SHA256 implementation in gsk/src/hash/gskhash.c.
797 * Copyright (C) 2006 Dave Benson
798 * Released under the terms of the GNU Lesser General Public License
801 static void
802 sha256_sum_init (Sha256sum *sha256)
804 sha256->buf[0] = 0x6a09e667;
805 sha256->buf[1] = 0xbb67ae85;
806 sha256->buf[2] = 0x3c6ef372;
807 sha256->buf[3] = 0xa54ff53a;
808 sha256->buf[4] = 0x510e527f;
809 sha256->buf[5] = 0x9b05688c;
810 sha256->buf[6] = 0x1f83d9ab;
811 sha256->buf[7] = 0x5be0cd19;
813 sha256->bits[0] = sha256->bits[1] = 0;
816 #define GET_UINT32(n,b,i) G_STMT_START{ \
817 (n) = ((guint32) (b)[(i) ] << 24) \
818 | ((guint32) (b)[(i) + 1] << 16) \
819 | ((guint32) (b)[(i) + 2] << 8) \
820 | ((guint32) (b)[(i) + 3] ); } G_STMT_END
822 #define PUT_UINT32(n,b,i) G_STMT_START{ \
823 (b)[(i) ] = (guint8) ((n) >> 24); \
824 (b)[(i) + 1] = (guint8) ((n) >> 16); \
825 (b)[(i) + 2] = (guint8) ((n) >> 8); \
826 (b)[(i) + 3] = (guint8) ((n) ); } G_STMT_END
828 static void
829 sha256_transform (guint32 buf[8],
830 guint8 const data[64])
832 guint32 temp1, temp2, W[64];
833 guint32 A, B, C, D, E, F, G, H;
835 GET_UINT32 (W[0], data, 0);
836 GET_UINT32 (W[1], data, 4);
837 GET_UINT32 (W[2], data, 8);
838 GET_UINT32 (W[3], data, 12);
839 GET_UINT32 (W[4], data, 16);
840 GET_UINT32 (W[5], data, 20);
841 GET_UINT32 (W[6], data, 24);
842 GET_UINT32 (W[7], data, 28);
843 GET_UINT32 (W[8], data, 32);
844 GET_UINT32 (W[9], data, 36);
845 GET_UINT32 (W[10], data, 40);
846 GET_UINT32 (W[11], data, 44);
847 GET_UINT32 (W[12], data, 48);
848 GET_UINT32 (W[13], data, 52);
849 GET_UINT32 (W[14], data, 56);
850 GET_UINT32 (W[15], data, 60);
852 #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
853 #define ROTR(x,n) (SHR (x,n) | (x << (32 - n)))
855 #define S0(x) (ROTR (x, 7) ^ ROTR (x,18) ^ SHR (x, 3))
856 #define S1(x) (ROTR (x,17) ^ ROTR (x,19) ^ SHR (x,10))
857 #define S2(x) (ROTR (x, 2) ^ ROTR (x,13) ^ ROTR (x,22))
858 #define S3(x) (ROTR (x, 6) ^ ROTR (x,11) ^ ROTR (x,25))
860 #define F0(x,y,z) ((x & y) | (z & (x | y)))
861 #define F1(x,y,z) (z ^ (x & (y ^ z)))
863 #define R(t) (W[t] = S1(W[t - 2]) + W[t - 7] + \
864 S0(W[t - 15]) + W[t - 16])
866 #define P(a,b,c,d,e,f,g,h,x,K) G_STMT_START { \
867 temp1 = h + S3(e) + F1(e,f,g) + K + x; \
868 temp2 = S2(a) + F0(a,b,c); \
869 d += temp1; h = temp1 + temp2; } G_STMT_END
871 A = buf[0];
872 B = buf[1];
873 C = buf[2];
874 D = buf[3];
875 E = buf[4];
876 F = buf[5];
877 G = buf[6];
878 H = buf[7];
880 P (A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98);
881 P (H, A, B, C, D, E, F, G, W[ 1], 0x71374491);
882 P (G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF);
883 P (F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5);
884 P (E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B);
885 P (D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1);
886 P (C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4);
887 P (B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5);
888 P (A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98);
889 P (H, A, B, C, D, E, F, G, W[ 9], 0x12835B01);
890 P (G, H, A, B, C, D, E, F, W[10], 0x243185BE);
891 P (F, G, H, A, B, C, D, E, W[11], 0x550C7DC3);
892 P (E, F, G, H, A, B, C, D, W[12], 0x72BE5D74);
893 P (D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE);
894 P (C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7);
895 P (B, C, D, E, F, G, H, A, W[15], 0xC19BF174);
896 P (A, B, C, D, E, F, G, H, R(16), 0xE49B69C1);
897 P (H, A, B, C, D, E, F, G, R(17), 0xEFBE4786);
898 P (G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6);
899 P (F, G, H, A, B, C, D, E, R(19), 0x240CA1CC);
900 P (E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F);
901 P (D, E, F, G, H, A, B, C, R(21), 0x4A7484AA);
902 P (C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC);
903 P (B, C, D, E, F, G, H, A, R(23), 0x76F988DA);
904 P (A, B, C, D, E, F, G, H, R(24), 0x983E5152);
905 P (H, A, B, C, D, E, F, G, R(25), 0xA831C66D);
906 P (G, H, A, B, C, D, E, F, R(26), 0xB00327C8);
907 P (F, G, H, A, B, C, D, E, R(27), 0xBF597FC7);
908 P (E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3);
909 P (D, E, F, G, H, A, B, C, R(29), 0xD5A79147);
910 P (C, D, E, F, G, H, A, B, R(30), 0x06CA6351);
911 P (B, C, D, E, F, G, H, A, R(31), 0x14292967);
912 P (A, B, C, D, E, F, G, H, R(32), 0x27B70A85);
913 P (H, A, B, C, D, E, F, G, R(33), 0x2E1B2138);
914 P (G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC);
915 P (F, G, H, A, B, C, D, E, R(35), 0x53380D13);
916 P (E, F, G, H, A, B, C, D, R(36), 0x650A7354);
917 P (D, E, F, G, H, A, B, C, R(37), 0x766A0ABB);
918 P (C, D, E, F, G, H, A, B, R(38), 0x81C2C92E);
919 P (B, C, D, E, F, G, H, A, R(39), 0x92722C85);
920 P (A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1);
921 P (H, A, B, C, D, E, F, G, R(41), 0xA81A664B);
922 P (G, H, A, B, C, D, E, F, R(42), 0xC24B8B70);
923 P (F, G, H, A, B, C, D, E, R(43), 0xC76C51A3);
924 P (E, F, G, H, A, B, C, D, R(44), 0xD192E819);
925 P (D, E, F, G, H, A, B, C, R(45), 0xD6990624);
926 P (C, D, E, F, G, H, A, B, R(46), 0xF40E3585);
927 P (B, C, D, E, F, G, H, A, R(47), 0x106AA070);
928 P (A, B, C, D, E, F, G, H, R(48), 0x19A4C116);
929 P (H, A, B, C, D, E, F, G, R(49), 0x1E376C08);
930 P (G, H, A, B, C, D, E, F, R(50), 0x2748774C);
931 P (F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5);
932 P (E, F, G, H, A, B, C, D, R(52), 0x391C0CB3);
933 P (D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A);
934 P (C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F);
935 P (B, C, D, E, F, G, H, A, R(55), 0x682E6FF3);
936 P (A, B, C, D, E, F, G, H, R(56), 0x748F82EE);
937 P (H, A, B, C, D, E, F, G, R(57), 0x78A5636F);
938 P (G, H, A, B, C, D, E, F, R(58), 0x84C87814);
939 P (F, G, H, A, B, C, D, E, R(59), 0x8CC70208);
940 P (E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA);
941 P (D, E, F, G, H, A, B, C, R(61), 0xA4506CEB);
942 P (C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7);
943 P (B, C, D, E, F, G, H, A, R(63), 0xC67178F2);
945 #undef SHR
946 #undef ROTR
947 #undef S0
948 #undef S1
949 #undef S2
950 #undef S3
951 #undef F0
952 #undef F1
953 #undef R
954 #undef P
956 buf[0] += A;
957 buf[1] += B;
958 buf[2] += C;
959 buf[3] += D;
960 buf[4] += E;
961 buf[5] += F;
962 buf[6] += G;
963 buf[7] += H;
966 static void
967 sha256_sum_update (Sha256sum *sha256,
968 const guchar *buffer,
969 gsize length)
971 guint32 left, fill;
972 const guint8 *input = buffer;
974 if (length == 0)
975 return;
977 left = sha256->bits[0] & 0x3F;
978 fill = 64 - left;
980 sha256->bits[0] += length;
981 sha256->bits[0] &= 0xFFFFFFFF;
983 if (sha256->bits[0] < length)
984 sha256->bits[1]++;
986 if (left > 0 && length >= fill)
988 memcpy ((sha256->data + left), input, fill);
990 sha256_transform (sha256->buf, sha256->data);
991 length -= fill;
992 input += fill;
994 left = 0;
997 while (length >= SHA256_DATASIZE)
999 sha256_transform (sha256->buf, input);
1001 length -= 64;
1002 input += 64;
1005 if (length)
1006 memcpy (sha256->data + left, input, length);
1009 static guint8 sha256_padding[64] =
1011 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1012 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1013 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1014 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1017 static void
1018 sha256_sum_close (Sha256sum *sha256)
1020 guint32 last, padn;
1021 guint32 high, low;
1022 guint8 msglen[8];
1024 high = (sha256->bits[0] >> 29)
1025 | (sha256->bits[1] << 3);
1026 low = (sha256->bits[0] << 3);
1028 PUT_UINT32 (high, msglen, 0);
1029 PUT_UINT32 (low, msglen, 4);
1031 last = sha256->bits[0] & 0x3F;
1032 padn = (last < 56) ? (56 - last) : (120 - last);
1034 sha256_sum_update (sha256, sha256_padding, padn);
1035 sha256_sum_update (sha256, msglen, 8);
1037 PUT_UINT32 (sha256->buf[0], sha256->digest, 0);
1038 PUT_UINT32 (sha256->buf[1], sha256->digest, 4);
1039 PUT_UINT32 (sha256->buf[2], sha256->digest, 8);
1040 PUT_UINT32 (sha256->buf[3], sha256->digest, 12);
1041 PUT_UINT32 (sha256->buf[4], sha256->digest, 16);
1042 PUT_UINT32 (sha256->buf[5], sha256->digest, 20);
1043 PUT_UINT32 (sha256->buf[6], sha256->digest, 24);
1044 PUT_UINT32 (sha256->buf[7], sha256->digest, 28);
1047 #undef PUT_UINT32
1048 #undef GET_UINT32
1050 static gchar *
1051 sha256_sum_to_string (Sha256sum *sha256)
1053 return digest_to_string (sha256->digest, SHA256_DIGEST_LEN);
1056 static void
1057 sha256_sum_digest (Sha256sum *sha256,
1058 guint8 *digest)
1060 gint i;
1062 for (i = 0; i < SHA256_DIGEST_LEN; i++)
1063 digest[i] = sha256->digest[i];
1068 * Public API
1072 * g_checksum_type_get_length:
1073 * @checksum_type: a #GChecksumType
1075 * Gets the length in bytes of digests of type @checksum_type
1077 * Return value: the checksum length, or -1 if @checksum_type is
1078 * not supported.
1080 * Since: 2.16
1082 gssize
1083 g_checksum_type_get_length (GChecksumType checksum_type)
1085 gssize len = -1;
1087 switch (checksum_type)
1089 case G_CHECKSUM_MD5:
1090 len = MD5_DIGEST_LEN;
1091 break;
1092 case G_CHECKSUM_SHA1:
1093 len = SHA1_DIGEST_LEN;
1094 break;
1095 case G_CHECKSUM_SHA256:
1096 len = SHA256_DIGEST_LEN;
1097 break;
1098 default:
1099 len = -1;
1100 break;
1103 return len;
1107 * g_checksum_new:
1108 * @checksum_type: the desired type of checksum
1110 * Creates a new #GChecksum, using the checksum algorithm @checksum_type.
1111 * If the @checksum_type is not known, %NULL is returned.
1112 * A #GChecksum can be used to compute the checksum, or digest, of an
1113 * arbitrary binary blob, using different hashing algorithms.
1115 * A #GChecksum works by feeding a binary blob through g_checksum_update()
1116 * until there is data to be checked; the digest can then be extracted
1117 * using g_checksum_get_string(), which will return the checksum as a
1118 * hexadecimal string; or g_checksum_get_digest(), which will return a
1119 * vector of raw bytes. Once either g_checksum_get_string() or
1120 * g_checksum_get_digest() have been called on a #GChecksum, the checksum
1121 * will be closed and it won't be possible to call g_checksum_update()
1122 * on it anymore.
1124 * Return value: the newly created #GChecksum, or %NULL.
1125 * Use g_checksum_free() to free the memory allocated by it.
1127 * Since: 2.16
1129 GChecksum *
1130 g_checksum_new (GChecksumType checksum_type)
1132 GChecksum *checksum;
1134 if (! IS_VALID_TYPE (checksum_type))
1135 return NULL;
1137 checksum = g_slice_new0 (GChecksum);
1138 checksum->type = checksum_type;
1140 g_checksum_reset (checksum);
1142 return checksum;
1146 * g_checksum_reset:
1147 * @checksum: the #GChecksum to reset
1149 * Resets the state of the @checksum back to its initial state.
1151 * Since: 2.18
1153 void
1154 g_checksum_reset (GChecksum *checksum)
1156 g_return_if_fail (checksum != NULL);
1158 g_free (checksum->digest_str);
1159 checksum->digest_str = NULL;
1161 switch (checksum->type)
1163 case G_CHECKSUM_MD5:
1164 md5_sum_init (&(checksum->sum.md5));
1165 break;
1166 case G_CHECKSUM_SHA1:
1167 sha1_sum_init (&(checksum->sum.sha1));
1168 break;
1169 case G_CHECKSUM_SHA256:
1170 sha256_sum_init (&(checksum->sum.sha256));
1171 break;
1172 default:
1173 g_assert_not_reached ();
1174 break;
1179 * g_checksum_copy:
1180 * @checksum: the #GChecksum to copy
1182 * Copies a #GChecksum. If @checksum has been closed, by calling
1183 * g_checksum_get_string() or g_checksum_get_digest(), the copied
1184 * checksum will be closed as well.
1186 * Return value: the copy of the passed #GChecksum. Use g_checksum_free()
1187 * when finished using it.
1189 * Since: 2.16
1191 GChecksum *
1192 g_checksum_copy (const GChecksum *checksum)
1194 GChecksum *copy;
1196 g_return_val_if_fail (checksum != NULL, NULL);
1198 copy = g_slice_new (GChecksum);
1199 *copy = *checksum;
1201 copy->digest_str = g_strdup (checksum->digest_str);
1203 return copy;
1207 * g_checksum_free:
1208 * @checksum: a #GChecksum
1210 * Frees the memory allocated for @checksum.
1212 * Since: 2.16
1214 void
1215 g_checksum_free (GChecksum *checksum)
1217 if (G_LIKELY (checksum))
1219 g_free (checksum->digest_str);
1221 g_slice_free (GChecksum, checksum);
1226 * g_checksum_update:
1227 * @checksum: a #GChecksum
1228 * @data: buffer used to compute the checksum
1229 * @length: size of the buffer, or -1 if it is a null-terminated string.
1231 * Feeds @data into an existing #GChecksum. The checksum must still be
1232 * open, that is g_checksum_get_string() or g_checksum_get_digest() must
1233 * not have been called on @checksum.
1235 * Since: 2.16
1237 void
1238 g_checksum_update (GChecksum *checksum,
1239 const guchar *data,
1240 gssize length)
1242 g_return_if_fail (checksum != NULL);
1243 g_return_if_fail (data != NULL);
1245 if (length < 0)
1246 length = strlen ((const gchar *) data);
1248 if (checksum->digest_str)
1250 g_warning ("The checksum `%s' has been closed and cannot be updated "
1251 "anymore.",
1252 checksum->digest_str);
1253 return;
1256 switch (checksum->type)
1258 case G_CHECKSUM_MD5:
1259 md5_sum_update (&(checksum->sum.md5), data, length);
1260 break;
1261 case G_CHECKSUM_SHA1:
1262 sha1_sum_update (&(checksum->sum.sha1), data, length);
1263 break;
1264 case G_CHECKSUM_SHA256:
1265 sha256_sum_update (&(checksum->sum.sha256), data, length);
1266 break;
1267 default:
1268 g_assert_not_reached ();
1269 break;
1274 * g_checksum_get_string:
1275 * @checksum: a #GChecksum
1277 * Gets the digest as an hexadecimal string.
1279 * Once this function has been called the #GChecksum can no longer be
1280 * updated with g_checksum_update().
1282 * The hexadecimal characters will be lower case.
1284 * Return value: the hexadecimal representation of the checksum. The
1285 * returned string is owned by the checksum and should not be modified
1286 * or freed.
1288 * Since: 2.16
1290 G_CONST_RETURN gchar *
1291 g_checksum_get_string (GChecksum *checksum)
1293 gchar *str = NULL;
1295 g_return_val_if_fail (checksum != NULL, NULL);
1297 if (checksum->digest_str)
1298 return checksum->digest_str;
1300 switch (checksum->type)
1302 case G_CHECKSUM_MD5:
1303 md5_sum_close (&(checksum->sum.md5));
1304 str = md5_sum_to_string (&(checksum->sum.md5));
1305 break;
1306 case G_CHECKSUM_SHA1:
1307 sha1_sum_close (&(checksum->sum.sha1));
1308 str = sha1_sum_to_string (&(checksum->sum.sha1));
1309 break;
1310 case G_CHECKSUM_SHA256:
1311 sha256_sum_close (&(checksum->sum.sha256));
1312 str = sha256_sum_to_string (&(checksum->sum.sha256));
1313 break;
1314 default:
1315 g_assert_not_reached ();
1316 break;
1319 checksum->digest_str = str;
1321 return checksum->digest_str;
1325 * g_checksum_get_digest:
1326 * @checksum: a #GChecksum
1327 * @buffer: output buffer
1328 * @digest_len: an inout parameter. The caller initializes it to the size of @buffer.
1329 * After the call it contains the length of the digest.
1331 * Gets the digest from @checksum as a raw binary vector and places it
1332 * into @buffer. The size of the digest depends on the type of checksum.
1334 * Once this function has been called, the #GChecksum is closed and can
1335 * no longer be updated with g_checksum_update().
1337 * Since: 2.16
1339 void
1340 g_checksum_get_digest (GChecksum *checksum,
1341 guint8 *buffer,
1342 gsize *digest_len)
1344 gboolean checksum_open = FALSE;
1345 gchar *str = NULL;
1346 gsize len;
1348 g_return_if_fail (checksum != NULL);
1350 len = g_checksum_type_get_length (checksum->type);
1351 g_return_if_fail (*digest_len >= len);
1353 checksum_open = !!(checksum->digest_str == NULL);
1355 switch (checksum->type)
1357 case G_CHECKSUM_MD5:
1358 if (checksum_open)
1360 md5_sum_close (&(checksum->sum.md5));
1361 str = md5_sum_to_string (&(checksum->sum.md5));
1363 md5_sum_digest (&(checksum->sum.md5), buffer);
1364 break;
1365 case G_CHECKSUM_SHA1:
1366 if (checksum_open)
1368 sha1_sum_close (&(checksum->sum.sha1));
1369 str = sha1_sum_to_string (&(checksum->sum.sha1));
1371 sha1_sum_digest (&(checksum->sum.sha1), buffer);
1372 break;
1373 case G_CHECKSUM_SHA256:
1374 if (checksum_open)
1376 sha256_sum_close (&(checksum->sum.sha256));
1377 str = sha256_sum_to_string (&(checksum->sum.sha256));
1379 sha256_sum_digest (&(checksum->sum.sha256), buffer);
1380 break;
1381 default:
1382 g_assert_not_reached ();
1383 break;
1386 if (str)
1387 checksum->digest_str = str;
1389 *digest_len = len;
1393 * g_compute_checksum_for_data:
1394 * @checksum_type: a #GChecksumType
1395 * @data: binary blob to compute the digest of
1396 * @length: length of @data
1398 * Computes the checksum for a binary @data of @length. This is a
1399 * convenience wrapper for g_checksum_new(), g_checksum_get_string()
1400 * and g_checksum_free().
1402 * The hexadecimal string returned will be in lower case.
1404 * Return value: the digest of the binary data as a string in hexadecimal.
1405 * The returned string should be freed with g_free() when done using it.
1407 * Since: 2.16
1409 gchar *
1410 g_compute_checksum_for_data (GChecksumType checksum_type,
1411 const guchar *data,
1412 gsize length)
1414 GChecksum *checksum;
1415 gchar *retval;
1417 g_return_val_if_fail (IS_VALID_TYPE (checksum_type), NULL);
1418 g_return_val_if_fail (data != NULL, NULL);
1420 checksum = g_checksum_new (checksum_type);
1421 if (!checksum)
1422 return NULL;
1424 g_checksum_update (checksum, data, length);
1425 retval = g_strdup (g_checksum_get_string (checksum));
1426 g_checksum_free (checksum);
1428 return retval;
1432 * g_compute_checksum_for_string:
1433 * @checksum_type: a #GChecksumType
1434 * @str: the string to compute the checksum of
1435 * @length: the length of the string, or -1 if the string is null-terminated.
1437 * Computes the checksum of a string.
1439 * The hexadecimal string returned will be in lower case.
1441 * Return value: the checksum as a hexadecimal string. The returned string
1442 * should be freed with g_free() when done using it.
1444 * Since: 2.16
1446 gchar *
1447 g_compute_checksum_for_string (GChecksumType checksum_type,
1448 const gchar *str,
1449 gssize length)
1451 g_return_val_if_fail (IS_VALID_TYPE (checksum_type), NULL);
1452 g_return_val_if_fail (str != NULL, NULL);
1454 if (length < 0)
1455 length = strlen (str);
1457 return g_compute_checksum_for_data (checksum_type, (const guchar *) str, length);
1460 #define __G_CHECKSUM_C__
1461 #include "galiasdef.c"