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