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