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