0000: 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 /*.** The code i
0010: 73 20 6d 6f 64 69 66 69 65 64 20 66 6f 72 20 75 s modified for u
0020: 73 65 20 69 6e 20 66 6f 73 73 69 6c 2e 20 20 54 se in fossil. T
0030: 68 65 20 6f 72 69 67 69 6e 61 6c 20 68 65 61 64 he original head
0040: 65 72 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 66 6f er.** comment fo
0050: 6c 6c 6f 77 73 3a 0a 2a 2f 0a 2f 2a 0a 20 2a 20 llows:.*/./*. *
0060: 54 68 69 73 20 63 6f 64 65 20 69 6d 70 6c 65 6d This code implem
0070: 65 6e 74 73 20 74 68 65 20 4d 44 35 20 6d 65 73 ents the MD5 mes
0080: 73 61 67 65 2d 64 69 67 65 73 74 20 61 6c 67 6f sage-digest algo
0090: 72 69 74 68 6d 2e 0a 20 2a 20 54 68 65 20 61 6c rithm.. * The al
00a0: 67 6f 72 69 74 68 6d 20 69 73 20 64 75 65 20 74 gorithm is due t
00b0: 6f 20 52 6f 6e 20 52 69 76 65 73 74 2e 20 20 54 o Ron Rivest. T
00c0: 68 69 73 20 63 6f 64 65 20 77 61 73 0a 20 2a 20 his code was. *
00d0: 77 72 69 74 74 65 6e 20 62 79 20 43 6f 6c 69 6e written by Colin
00e0: 20 50 6c 75 6d 62 20 69 6e 20 31 39 39 33 2c 20 Plumb in 1993,
00f0: 6e 6f 20 63 6f 70 79 72 69 67 68 74 20 69 73 20 no copyright is
0100: 63 6c 61 69 6d 65 64 2e 0a 20 2a 20 54 68 69 73 claimed.. * This
0110: 20 63 6f 64 65 20 69 73 20 69 6e 20 74 68 65 20 code is in the
0120: 70 75 62 6c 69 63 20 64 6f 6d 61 69 6e 3b 20 64 public domain; d
0130: 6f 20 77 69 74 68 20 69 74 20 77 68 61 74 20 79 o with it what y
0140: 6f 75 20 77 69 73 68 2e 0a 20 2a 0a 20 2a 20 45 ou wish.. *. * E
0150: 71 75 69 76 61 6c 65 6e 74 20 63 6f 64 65 20 69 quivalent code i
0160: 73 20 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d s available from
0170: 20 52 53 41 20 44 61 74 61 20 53 65 63 75 72 69 RSA Data Securi
0180: 74 79 2c 20 49 6e 63 2e 0a 20 2a 20 54 68 69 73 ty, Inc.. * This
0190: 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 20 74 code has been t
01a0: 65 73 74 65 64 20 61 67 61 69 6e 73 74 20 74 68 ested against th
01b0: 61 74 2c 20 61 6e 64 20 69 73 20 65 71 75 69 76 at, and is equiv
01c0: 61 6c 65 6e 74 2c 0a 20 2a 20 65 78 63 65 70 74 alent,. * except
01d0: 20 74 68 61 74 20 79 6f 75 20 64 6f 6e 27 74 20 that you don't
01e0: 6e 65 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 20 need to include
01f0: 74 77 6f 20 70 61 67 65 73 20 6f 66 20 6c 65 67 two pages of leg
0200: 61 6c 65 73 65 0a 20 2a 20 77 69 74 68 20 65 76 alese. * with ev
0210: 65 72 79 20 63 6f 70 79 2e 0a 20 2a 0a 20 2a 20 ery copy.. *. *
0220: 54 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6d To compute the m
0230: 65 73 73 61 67 65 20 64 69 67 65 73 74 20 6f 66 essage digest of
0240: 20 61 20 63 68 75 6e 6b 20 6f 66 20 62 79 74 65 a chunk of byte
0250: 73 2c 20 64 65 63 6c 61 72 65 20 61 6e 0a 20 2a s, declare an. *
0260: 20 4d 44 35 43 6f 6e 74 65 78 74 20 73 74 72 75 MD5Context stru
0270: 63 74 75 72 65 2c 20 70 61 73 73 20 69 74 20 74 cture, pass it t
0280: 6f 20 4d 44 35 49 6e 69 74 2c 20 63 61 6c 6c 20 o MD5Init, call
0290: 4d 44 35 55 70 64 61 74 65 20 61 73 0a 20 2a 20 MD5Update as. *
02a0: 6e 65 65 64 65 64 20 6f 6e 20 62 75 66 66 65 72 needed on buffer
02b0: 73 20 66 75 6c 6c 20 6f 66 20 62 79 74 65 73 2c s full of bytes,
02c0: 20 61 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20 4d and then call M
02d0: 44 35 46 69 6e 61 6c 2c 20 77 68 69 63 68 0a 20 D5Final, which.
02e0: 2a 20 77 69 6c 6c 20 66 69 6c 6c 20 61 20 73 75 * will fill a su
02f0: 70 70 6c 69 65 64 20 31 36 2d 62 79 74 65 20 61 pplied 16-byte a
0300: 72 72 61 79 20 77 69 74 68 20 74 68 65 20 64 69 rray with the di
0310: 67 65 73 74 2e 0a 20 2a 2f 0a 23 69 6e 63 6c 75 gest.. */.#inclu
0320: 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 de <string.h>.#i
0330: 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e nclude <stdio.h>
0340: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 71 6c 69 74 .#include <sqlit
0350: 65 33 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 22 e3.h>.#include "
0360: 6d 64 35 2e 68 22 0a 0a 2f 2a 0a 20 2a 20 49 66 md5.h"../*. * If
0370: 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 61 20 6d compiled on a m
0380: 61 63 68 69 6e 65 20 74 68 61 74 20 64 6f 65 73 achine that does
0390: 6e 27 74 20 68 61 76 65 20 61 20 33 32 2d 62 69 n't have a 32-bi
03a0: 74 20 69 6e 74 65 67 65 72 2c 0a 20 2a 20 79 6f t integer,. * yo
03b0: 75 20 6a 75 73 74 20 73 65 74 20 22 75 69 6e 74 u just set "uint
03c0: 33 32 22 20 74 6f 20 74 68 65 20 61 70 70 72 6f 32" to the appro
03d0: 70 72 69 61 74 65 20 64 61 74 61 74 79 70 65 20 priate datatype
03e0: 66 6f 72 20 61 6e 0a 20 2a 20 75 6e 73 69 67 6e for an. * unsign
03f0: 65 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 ed 32-bit intege
0400: 72 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a r. For example:
0410: 0a 20 2a 0a 20 2a 20 20 20 20 20 20 20 63 63 20 . *. * cc
0420: 2d 44 75 69 6e 74 33 32 3d 27 75 6e 73 69 67 6e -Duint32='unsign
0430: 65 64 20 6c 6f 6e 67 27 20 6d 64 35 2e 63 0a 20 ed long' md5.c.
0440: 2a 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 20 75 69 *. */.#ifndef ui
0450: 6e 74 33 32 0a 23 20 20 64 65 66 69 6e 65 20 75 nt32.# define u
0460: 69 6e 74 33 32 20 75 6e 73 69 67 6e 65 64 20 69 int32 unsigned i
0470: 6e 74 0a 23 65 6e 64 69 66 0a 0a 73 74 72 75 63 nt.#endif..struc
0480: 74 20 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 6e t Context {. in
0490: 74 20 69 73 49 6e 69 74 3b 0a 20 20 75 69 6e 74 t isInit;. uint
04a0: 33 32 20 62 75 66 5b 34 5d 3b 0a 20 20 75 69 6e 32 buf[4];. uin
04b0: 74 33 32 20 62 69 74 73 5b 32 5d 3b 0a 20 20 75 t32 bits[2];. u
04c0: 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e 5b nsigned char in[
04d0: 36 34 5d 3b 0a 7d 3b 0a 74 79 70 65 64 65 66 20 64];.};.typedef
04e0: 73 74 72 75 63 74 20 43 6f 6e 74 65 78 74 20 4d struct Context M
04f0: 44 35 43 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 20 D5Context;../*.
0500: 2a 20 4e 6f 74 65 3a 20 74 68 69 73 20 63 6f 64 * Note: this cod
0510: 65 20 69 73 20 68 61 72 6d 6c 65 73 73 20 6f 6e e is harmless on
0520: 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 6d little-endian m
0530: 61 63 68 69 6e 65 73 2e 0a 20 2a 2f 0a 73 74 61 achines.. */.sta
0540: 74 69 63 20 76 6f 69 64 20 62 79 74 65 52 65 76 tic void byteRev
0550: 65 72 73 65 20 28 75 6e 73 69 67 6e 65 64 20 63 erse (unsigned c
0560: 68 61 72 20 2a 62 75 66 2c 20 75 6e 73 69 67 6e har *buf, unsign
0570: 65 64 20 6c 6f 6e 67 73 29 7b 0a 20 20 20 20 20 ed longs){.
0580: 20 20 20 75 69 6e 74 33 32 20 74 3b 0a 20 20 20 uint32 t;.
0590: 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 do {.
05a0: 20 20 20 20 20 20 20 20 20 20 74 20 3d 20 28 75 t = (u
05b0: 69 6e 74 33 32 29 28 28 75 6e 73 69 67 6e 65 64 int32)((unsigned
05c0: 29 62 75 66 5b 33 5d 3c 3c 38 20 7c 20 62 75 66 )buf[3]<<8 | buf
05d0: 5b 32 5d 29 20 3c 3c 20 31 36 20 7c 0a 20 20 20 [2]) << 16 |.
05e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
05f0: 20 20 20 20 20 20 20 20 20 28 28 75 6e 73 69 67 ((unsig
0600: 6e 65 64 29 62 75 66 5b 31 5d 3c 3c 38 20 7c 20 ned)buf[1]<<8 |
0610: 62 75 66 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 buf[0]);.
0620: 20 20 20 20 20 20 20 20 20 2a 28 75 69 6e 74 33 *(uint3
0630: 32 20 2a 29 62 75 66 20 3d 20 74 3b 0a 20 20 20 2 *)buf = t;.
0640: 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 buf
0650: 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d += 4;. }
0660: 20 77 68 69 6c 65 20 28 2d 2d 6c 6f 6e 67 73 29 while (--longs)
0670: 3b 0a 7d 0a 2f 2a 20 54 68 65 20 66 6f 75 72 20 ;.}./* The four
0680: 63 6f 72 65 20 66 75 6e 63 74 69 6f 6e 73 20 2d core functions -
0690: 20 46 31 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 F1 is optimized
06a0: 20 73 6f 6d 65 77 68 61 74 20 2a 2f 0a 0a 2f 2a somewhat */../*
06b0: 20 23 64 65 66 69 6e 65 20 46 31 28 78 2c 20 79 #define F1(x, y
06c0: 2c 20 7a 29 20 28 78 20 26 20 79 20 7c 20 7e 78 , z) (x & y | ~x
06d0: 20 26 20 7a 29 20 2a 2f 0a 23 64 65 66 69 6e 65 & z) */.#define
06e0: 20 46 31 28 78 2c 20 79 2c 20 7a 29 20 28 7a 20 F1(x, y, z) (z
06f0: 5e 20 28 78 20 26 20 28 79 20 5e 20 7a 29 29 29 ^ (x & (y ^ z)))
0700: 0a 23 64 65 66 69 6e 65 20 46 32 28 78 2c 20 79 .#define F2(x, y
0710: 2c 20 7a 29 20 46 31 28 7a 2c 20 78 2c 20 79 29 , z) F1(z, x, y)
0720: 0a 23 64 65 66 69 6e 65 20 46 33 28 78 2c 20 79 .#define F3(x, y
0730: 2c 20 7a 29 20 28 78 20 5e 20 79 20 5e 20 7a 29 , z) (x ^ y ^ z)
0740: 0a 23 64 65 66 69 6e 65 20 46 34 28 78 2c 20 79 .#define F4(x, y
0750: 2c 20 7a 29 20 28 79 20 5e 20 28 78 20 7c 20 7e , z) (y ^ (x | ~
0760: 7a 29 29 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20 z))../* This is
0770: 74 68 65 20 63 65 6e 74 72 61 6c 20 73 74 65 70 the central step
0780: 20 69 6e 20 74 68 65 20 4d 44 35 20 61 6c 67 6f in the MD5 algo
0790: 72 69 74 68 6d 2e 20 2a 2f 0a 23 64 65 66 69 6e rithm. */.#defin
07a0: 65 20 4d 44 35 53 54 45 50 28 66 2c 20 77 2c 20 e MD5STEP(f, w,
07b0: 78 2c 20 79 2c 20 7a 2c 20 64 61 74 61 2c 20 73 x, y, z, data, s
07c0: 29 20 5c 0a 20 20 20 20 20 20 20 20 28 20 77 20 ) \. ( w
07d0: 2b 3d 20 66 28 78 2c 20 79 2c 20 7a 29 20 2b 20 += f(x, y, z) +
07e0: 64 61 74 61 2c 20 20 77 20 3d 20 77 3c 3c 73 20 data, w = w<<s
07f0: 7c 20 77 3e 3e 28 33 32 2d 73 29 2c 20 20 77 20 | w>>(32-s), w
0800: 2b 3d 20 78 20 29 0a 0a 2f 2a 0a 20 2a 20 54 68 += x )../*. * Th
0810: 65 20 63 6f 72 65 20 6f 66 20 74 68 65 20 4d 44 e core of the MD
0820: 35 20 61 6c 67 6f 72 69 74 68 6d 2c 20 74 68 69 5 algorithm, thi
0830: 73 20 61 6c 74 65 72 73 20 61 6e 20 65 78 69 73 s alters an exis
0840: 74 69 6e 67 20 4d 44 35 20 68 61 73 68 20 74 6f ting MD5 hash to
0850: 0a 20 2a 20 72 65 66 6c 65 63 74 20 74 68 65 20 . * reflect the
0860: 61 64 64 69 74 69 6f 6e 20 6f 66 20 31 36 20 6c addition of 16 l
0870: 6f 6e 67 77 6f 72 64 73 20 6f 66 20 6e 65 77 20 ongwords of new
0880: 64 61 74 61 2e 20 20 4d 44 35 55 70 64 61 74 65 data. MD5Update
0890: 20 62 6c 6f 63 6b 73 0a 20 2a 20 74 68 65 20 64 blocks. * the d
08a0: 61 74 61 20 61 6e 64 20 63 6f 6e 76 65 72 74 73 ata and converts
08b0: 20 62 79 74 65 73 20 69 6e 74 6f 20 6c 6f 6e 67 bytes into long
08c0: 77 6f 72 64 73 20 66 6f 72 20 74 68 69 73 20 72 words for this r
08d0: 6f 75 74 69 6e 65 2e 0a 20 2a 2f 0a 73 74 61 74 outine.. */.stat
08e0: 69 63 20 76 6f 69 64 20 4d 44 35 54 72 61 6e 73 ic void MD5Trans
08f0: 66 6f 72 6d 28 75 69 6e 74 33 32 20 62 75 66 5b form(uint32 buf[
0900: 34 5d 2c 20 63 6f 6e 73 74 20 75 69 6e 74 33 32 4], const uint32
0910: 20 69 6e 5b 31 36 5d 29 7b 0a 20 20 20 20 20 20 in[16]){.
0920: 20 20 72 65 67 69 73 74 65 72 20 75 69 6e 74 33 register uint3
0930: 32 20 61 2c 20 62 2c 20 63 2c 20 64 3b 0a 0a 20 2 a, b, c, d;..
0940: 20 20 20 20 20 20 20 61 20 3d 20 62 75 66 5b 30 a = buf[0
0950: 5d 3b 0a 20 20 20 20 20 20 20 20 62 20 3d 20 62 ];. b = b
0960: 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 63 uf[1];. c
0970: 20 3d 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 = buf[2];.
0980: 20 20 20 64 20 3d 20 62 75 66 5b 33 5d 3b 0a 0a d = buf[3];..
0990: 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 28 MD5STEP(
09a0: 46 31 2c 20 61 2c 20 62 2c 20 63 2c 20 64 2c 20 F1, a, b, c, d,
09b0: 69 6e 5b 20 30 5d 2b 30 78 64 37 36 61 61 34 37 in[ 0]+0xd76aa47
09c0: 38 2c 20 20 37 29 3b 0a 20 20 20 20 20 20 20 20 8, 7);.
09d0: 4d 44 35 53 54 45 50 28 46 31 2c 20 64 2c 20 61 MD5STEP(F1, d, a
09e0: 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 31 5d 2b 30 , b, c, in[ 1]+0
09f0: 78 65 38 63 37 62 37 35 36 2c 20 31 32 29 3b 0a xe8c7b756, 12);.
0a00: 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 28 MD5STEP(
0a10: 46 31 2c 20 63 2c 20 64 2c 20 61 2c 20 62 2c 20 F1, c, d, a, b,
0a20: 69 6e 5b 20 32 5d 2b 30 78 32 34 32 30 37 30 64 in[ 2]+0x242070d
0a30: 62 2c 20 31 37 29 3b 0a 20 20 20 20 20 20 20 20 b, 17);.
0a40: 4d 44 35 53 54 45 50 28 46 31 2c 20 62 2c 20 63 MD5STEP(F1, b, c
0a50: 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 33 5d 2b 30 , d, a, in[ 3]+0
0a60: 78 63 31 62 64 63 65 65 65 2c 20 32 32 29 3b 0a xc1bdceee, 22);.
0a70: 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 28 MD5STEP(
0a80: 46 31 2c 20 61 2c 20 62 2c 20 63 2c 20 64 2c 20 F1, a, b, c, d,
0a90: 69 6e 5b 20 34 5d 2b 30 78 66 35 37 63 30 66 61 in[ 4]+0xf57c0fa
0aa0: 66 2c 20 20 37 29 3b 0a 20 20 20 20 20 20 20 20 f, 7);.
0ab0: 4d 44 35 53 54 45 50 28 46 31 2c 20 64 2c 20 61 MD5STEP(F1, d, a
0ac0: 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 35 5d 2b 30 , b, c, in[ 5]+0
0ad0: 78 34 37 38 37 63 36 32 61 2c 20 31 32 29 3b 0a x4787c62a, 12);.
0ae0: 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 28 MD5STEP(
0af0: 46 31 2c 20 63 2c 20 64 2c 20 61 2c 20 62 2c 20 F1, c, d, a, b,
0b00: 69 6e 5b 20 36 5d 2b 30 78 61 38 33 30 34 36 31 in[ 6]+0xa830461
0b10: 33 2c 20 31 37 29 3b 0a 20 20 20 20 20 20 20 20 3, 17);.
0b20: 4d 44 35 53 54 45 50 28 46 31 2c 20 62 2c 20 63 MD5STEP(F1, b, c
0b30: 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 37 5d 2b 30 , d, a, in[ 7]+0
0b40: 78 66 64 34 36 39 35 30 31 2c 20 32 32 29 3b 0a xfd469501, 22);.
0b50: 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 28 MD5STEP(
0b60: 46 31 2c 20 61 2c 20 62 2c 20 63 2c 20 64 2c 20 F1, a, b, c, d,
0b70: 69 6e 5b 20 38 5d 2b 30 78 36 39 38 30 39 38 64 in[ 8]+0x698098d
0b80: 38 2c 20 20 37 29 3b 0a 20 20 20 20 20 20 20 20 8, 7);.
0b90: 4d 44 35 53 54 45 50 28 46 31 2c 20 64 2c 20 61 MD5STEP(F1, d, a
0ba0: 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 39 5d 2b 30 , b, c, in[ 9]+0
0bb0: 78 38 62 34 34 66 37 61 66 2c 20 31 32 29 3b 0a x8b44f7af, 12);.
0bc0: 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 28 MD5STEP(
0bd0: 46 31 2c 20 63 2c 20 64 2c 20 61 2c 20 62 2c 20 F1, c, d, a, b,
0be0: 69 6e 5b 31 30 5d 2b 30 78 66 66 66 66 35 62 62 in[10]+0xffff5bb
0bf0: 31 2c 20 31 37 29 3b 0a 20 20 20 20 20 20 20 20 1, 17);.
0c00: 4d 44 35 53 54 45 50 28 46 31 2c 20 62 2c 20 63 MD5STEP(F1, b, c
0c10: 2c 20 64 2c 20 61 2c 20 69 6e 5b 31 31 5d 2b 30 , d, a, in[11]+0
0c20: 78 38 39 35 63 64 37 62 65 2c 20 32 32 29 3b 0a x895cd7be, 22);.
0c30: 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 28 MD5STEP(
0c40: 46 31 2c 20 61 2c 20 62 2c 20 63 2c 20 64 2c 20 F1, a, b, c, d,
0c50: 69 6e 5b 31 32 5d 2b 30 78 36 62 39 30 31 31 32 in[12]+0x6b90112
0c60: 32 2c 20 20 37 29 3b 0a 20 20 20 20 20 20 20 20 2, 7);.
0c70: 4d 44 35 53 54 45 50 28 46 31 2c 20 64 2c 20 61 MD5STEP(F1, d, a
0c80: 2c 20 62 2c 20 63 2c 20 69 6e 5b 31 33 5d 2b 30 , b, c, in[13]+0
0c90: 78 66 64 39 38 37 31 39 33 2c 20 31 32 29 3b 0a xfd987193, 12);.
0ca0: 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 28 MD5STEP(
0cb0: 46 31 2c 20 63 2c 20 64 2c 20 61 2c 20 62 2c 20 F1, c, d, a, b,
0cc0: 69 6e 5b 31 34 5d 2b 30 78 61 36 37 39 34 33 38 in[14]+0xa679438
0cd0: 65 2c 20 31 37 29 3b 0a 20 20 20 20 20 20 20 20 e, 17);.
0ce0: 4d 44 35 53 54 45 50 28 46 31 2c 20 62 2c 20 63 MD5STEP(F1, b, c
0cf0: 2c 20 64 2c 20 61 2c 20 69 6e 5b 31 35 5d 2b 30 , d, a, in[15]+0
0d00: 78 34 39 62 34 30 38 32 31 2c 20 32 32 29 3b 0a x49b40821, 22);.
0d10: 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 . MD5STEP
0d20: 28 46 32 2c 20 61 2c 20 62 2c 20 63 2c 20 64 2c (F2, a, b, c, d,
0d30: 20 69 6e 5b 20 31 5d 2b 30 78 66 36 31 65 32 35 in[ 1]+0xf61e25
0d40: 36 32 2c 20 20 35 29 3b 0a 20 20 20 20 20 20 20 62, 5);.
0d50: 20 4d 44 35 53 54 45 50 28 46 32 2c 20 64 2c 20 MD5STEP(F2, d,
0d60: 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 36 5d 2b a, b, c, in[ 6]+
0d70: 30 78 63 30 34 30 62 33 34 30 2c 20 20 39 29 3b 0xc040b340, 9);
0d80: 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 . MD5STEP
0d90: 28 46 32 2c 20 63 2c 20 64 2c 20 61 2c 20 62 2c (F2, c, d, a, b,
0da0: 20 69 6e 5b 31 31 5d 2b 30 78 32 36 35 65 35 61 in[11]+0x265e5a
0db0: 35 31 2c 20 31 34 29 3b 0a 20 20 20 20 20 20 20 51, 14);.
0dc0: 20 4d 44 35 53 54 45 50 28 46 32 2c 20 62 2c 20 MD5STEP(F2, b,
0dd0: 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 30 5d 2b c, d, a, in[ 0]+
0de0: 30 78 65 39 62 36 63 37 61 61 2c 20 32 30 29 3b 0xe9b6c7aa, 20);
0df0: 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 . MD5STEP
0e00: 28 46 32 2c 20 61 2c 20 62 2c 20 63 2c 20 64 2c (F2, a, b, c, d,
0e10: 20 69 6e 5b 20 35 5d 2b 30 78 64 36 32 66 31 30 in[ 5]+0xd62f10
0e20: 35 64 2c 20 20 35 29 3b 0a 20 20 20 20 20 20 20 5d, 5);.
0e30: 20 4d 44 35 53 54 45 50 28 46 32 2c 20 64 2c 20 MD5STEP(F2, d,
0e40: 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 31 30 5d 2b a, b, c, in[10]+
0e50: 30 78 30 32 34 34 31 34 35 33 2c 20 20 39 29 3b 0x02441453, 9);
0e60: 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 . MD5STEP
0e70: 28 46 32 2c 20 63 2c 20 64 2c 20 61 2c 20 62 2c (F2, c, d, a, b,
0e80: 20 69 6e 5b 31 35 5d 2b 30 78 64 38 61 31 65 36 in[15]+0xd8a1e6
0e90: 38 31 2c 20 31 34 29 3b 0a 20 20 20 20 20 20 20 81, 14);.
0ea0: 20 4d 44 35 53 54 45 50 28 46 32 2c 20 62 2c 20 MD5STEP(F2, b,
0eb0: 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 34 5d 2b c, d, a, in[ 4]+
0ec0: 30 78 65 37 64 33 66 62 63 38 2c 20 32 30 29 3b 0xe7d3fbc8, 20);
0ed0: 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 . MD5STEP
0ee0: 28 46 32 2c 20 61 2c 20 62 2c 20 63 2c 20 64 2c (F2, a, b, c, d,
0ef0: 20 69 6e 5b 20 39 5d 2b 30 78 32 31 65 31 63 64 in[ 9]+0x21e1cd
0f00: 65 36 2c 20 20 35 29 3b 0a 20 20 20 20 20 20 20 e6, 5);.
0f10: 20 4d 44 35 53 54 45 50 28 46 32 2c 20 64 2c 20 MD5STEP(F2, d,
0f20: 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 31 34 5d 2b a, b, c, in[14]+
0f30: 30 78 63 33 33 37 30 37 64 36 2c 20 20 39 29 3b 0xc33707d6, 9);
0f40: 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 . MD5STEP
0f50: 28 46 32 2c 20 63 2c 20 64 2c 20 61 2c 20 62 2c (F2, c, d, a, b,
0f60: 20 69 6e 5b 20 33 5d 2b 30 78 66 34 64 35 30 64 in[ 3]+0xf4d50d
0f70: 38 37 2c 20 31 34 29 3b 0a 20 20 20 20 20 20 20 87, 14);.
0f80: 20 4d 44 35 53 54 45 50 28 46 32 2c 20 62 2c 20 MD5STEP(F2, b,
0f90: 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 38 5d 2b c, d, a, in[ 8]+
0fa0: 30 78 34 35 35 61 31 34 65 64 2c 20 32 30 29 3b 0x455a14ed, 20);
0fb0: 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 . MD5STEP
0fc0: 28 46 32 2c 20 61 2c 20 62 2c 20 63 2c 20 64 2c (F2, a, b, c, d,
0fd0: 20 69 6e 5b 31 33 5d 2b 30 78 61 39 65 33 65 39 in[13]+0xa9e3e9
0fe0: 30 35 2c 20 20 35 29 3b 0a 20 20 20 20 20 20 20 05, 5);.
0ff0: 20 4d 44 35 53 54 45 50 28 46 32 2c 20 64 2c 20 MD5STEP(F2, d,
1000: 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 32 5d 2b a, b, c, in[ 2]+
1010: 30 78 66 63 65 66 61 33 66 38 2c 20 20 39 29 3b 0xfcefa3f8, 9);
1020: 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 50 . MD5STEP
1030: 28 46 32 2c 20 63 2c 20 64 2c 20 61 2c 20 62 2c (F2, c, d, a, b,
1040: 20 69 6e 5b 20 37 5d 2b 30 78 36 37 36 66 30 32 in[ 7]+0x676f02
1050: 64 39 2c 20 31 34 29 3b 0a 20 20 20 20 20 20 20 d9, 14);.
1060: 20 4d 44 35 53 54 45 50 28 46 32 2c 20 62 2c 20 MD5STEP(F2, b,
1070: 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 31 32 5d 2b c, d, a, in[12]+
1080: 30 78 38 64 32 61 34 63 38 61 2c 20 32 30 29 3b 0x8d2a4c8a, 20);
1090: 0a 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 .. MD5STE
10a0: 50 28 46 33 2c 20 61 2c 20 62 2c 20 63 2c 20 64 P(F3, a, b, c, d
10b0: 2c 20 69 6e 5b 20 35 5d 2b 30 78 66 66 66 61 33 , in[ 5]+0xfffa3
10c0: 39 34 32 2c 20 20 34 29 3b 0a 20 20 20 20 20 20 942, 4);.
10d0: 20 20 4d 44 35 53 54 45 50 28 46 33 2c 20 64 2c MD5STEP(F3, d,
10e0: 20 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 38 5d a, b, c, in[ 8]
10f0: 2b 30 78 38 37 37 31 66 36 38 31 2c 20 31 31 29 +0x8771f681, 11)
1100: 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 ;. MD5STE
1110: 50 28 46 33 2c 20 63 2c 20 64 2c 20 61 2c 20 62 P(F3, c, d, a, b
1120: 2c 20 69 6e 5b 31 31 5d 2b 30 78 36 64 39 64 36 , in[11]+0x6d9d6
1130: 31 32 32 2c 20 31 36 29 3b 0a 20 20 20 20 20 20 122, 16);.
1140: 20 20 4d 44 35 53 54 45 50 28 46 33 2c 20 62 2c MD5STEP(F3, b,
1150: 20 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 31 34 5d c, d, a, in[14]
1160: 2b 30 78 66 64 65 35 33 38 30 63 2c 20 32 33 29 +0xfde5380c, 23)
1170: 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 ;. MD5STE
1180: 50 28 46 33 2c 20 61 2c 20 62 2c 20 63 2c 20 64 P(F3, a, b, c, d
1190: 2c 20 69 6e 5b 20 31 5d 2b 30 78 61 34 62 65 65 , in[ 1]+0xa4bee
11a0: 61 34 34 2c 20 20 34 29 3b 0a 20 20 20 20 20 20 a44, 4);.
11b0: 20 20 4d 44 35 53 54 45 50 28 46 33 2c 20 64 2c MD5STEP(F3, d,
11c0: 20 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 34 5d a, b, c, in[ 4]
11d0: 2b 30 78 34 62 64 65 63 66 61 39 2c 20 31 31 29 +0x4bdecfa9, 11)
11e0: 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 ;. MD5STE
11f0: 50 28 46 33 2c 20 63 2c 20 64 2c 20 61 2c 20 62 P(F3, c, d, a, b
1200: 2c 20 69 6e 5b 20 37 5d 2b 30 78 66 36 62 62 34 , in[ 7]+0xf6bb4
1210: 62 36 30 2c 20 31 36 29 3b 0a 20 20 20 20 20 20 b60, 16);.
1220: 20 20 4d 44 35 53 54 45 50 28 46 33 2c 20 62 2c MD5STEP(F3, b,
1230: 20 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 31 30 5d c, d, a, in[10]
1240: 2b 30 78 62 65 62 66 62 63 37 30 2c 20 32 33 29 +0xbebfbc70, 23)
1250: 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 ;. MD5STE
1260: 50 28 46 33 2c 20 61 2c 20 62 2c 20 63 2c 20 64 P(F3, a, b, c, d
1270: 2c 20 69 6e 5b 31 33 5d 2b 30 78 32 38 39 62 37 , in[13]+0x289b7
1280: 65 63 36 2c 20 20 34 29 3b 0a 20 20 20 20 20 20 ec6, 4);.
1290: 20 20 4d 44 35 53 54 45 50 28 46 33 2c 20 64 2c MD5STEP(F3, d,
12a0: 20 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 30 5d a, b, c, in[ 0]
12b0: 2b 30 78 65 61 61 31 32 37 66 61 2c 20 31 31 29 +0xeaa127fa, 11)
12c0: 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 ;. MD5STE
12d0: 50 28 46 33 2c 20 63 2c 20 64 2c 20 61 2c 20 62 P(F3, c, d, a, b
12e0: 2c 20 69 6e 5b 20 33 5d 2b 30 78 64 34 65 66 33 , in[ 3]+0xd4ef3
12f0: 30 38 35 2c 20 31 36 29 3b 0a 20 20 20 20 20 20 085, 16);.
1300: 20 20 4d 44 35 53 54 45 50 28 46 33 2c 20 62 2c MD5STEP(F3, b,
1310: 20 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 36 5d c, d, a, in[ 6]
1320: 2b 30 78 30 34 38 38 31 64 30 35 2c 20 32 33 29 +0x04881d05, 23)
1330: 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 ;. MD5STE
1340: 50 28 46 33 2c 20 61 2c 20 62 2c 20 63 2c 20 64 P(F3, a, b, c, d
1350: 2c 20 69 6e 5b 20 39 5d 2b 30 78 64 39 64 34 64 , in[ 9]+0xd9d4d
1360: 30 33 39 2c 20 20 34 29 3b 0a 20 20 20 20 20 20 039, 4);.
1370: 20 20 4d 44 35 53 54 45 50 28 46 33 2c 20 64 2c MD5STEP(F3, d,
1380: 20 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 31 32 5d a, b, c, in[12]
1390: 2b 30 78 65 36 64 62 39 39 65 35 2c 20 31 31 29 +0xe6db99e5, 11)
13a0: 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 45 ;. MD5STE
13b0: 50 28 46 33 2c 20 63 2c 20 64 2c 20 61 2c 20 62 P(F3, c, d, a, b
13c0: 2c 20 69 6e 5b 31 35 5d 2b 30 78 31 66 61 32 37 , in[15]+0x1fa27
13d0: 63 66 38 2c 20 31 36 29 3b 0a 20 20 20 20 20 20 cf8, 16);.
13e0: 20 20 4d 44 35 53 54 45 50 28 46 33 2c 20 62 2c MD5STEP(F3, b,
13f0: 20 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 32 5d c, d, a, in[ 2]
1400: 2b 30 78 63 34 61 63 35 36 36 35 2c 20 32 33 29 +0xc4ac5665, 23)
1410: 3b 0a 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 ;.. MD5ST
1420: 45 50 28 46 34 2c 20 61 2c 20 62 2c 20 63 2c 20 EP(F4, a, b, c,
1430: 64 2c 20 69 6e 5b 20 30 5d 2b 30 78 66 34 32 39 d, in[ 0]+0xf429
1440: 32 32 34 34 2c 20 20 36 29 3b 0a 20 20 20 20 20 2244, 6);.
1450: 20 20 20 4d 44 35 53 54 45 50 28 46 34 2c 20 64 MD5STEP(F4, d
1460: 2c 20 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 37 , a, b, c, in[ 7
1470: 5d 2b 30 78 34 33 32 61 66 66 39 37 2c 20 31 30 ]+0x432aff97, 10
1480: 29 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 );. MD5ST
1490: 45 50 28 46 34 2c 20 63 2c 20 64 2c 20 61 2c 20 EP(F4, c, d, a,
14a0: 62 2c 20 69 6e 5b 31 34 5d 2b 30 78 61 62 39 34 b, in[14]+0xab94
14b0: 32 33 61 37 2c 20 31 35 29 3b 0a 20 20 20 20 20 23a7, 15);.
14c0: 20 20 20 4d 44 35 53 54 45 50 28 46 34 2c 20 62 MD5STEP(F4, b
14d0: 2c 20 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 35 , c, d, a, in[ 5
14e0: 5d 2b 30 78 66 63 39 33 61 30 33 39 2c 20 32 31 ]+0xfc93a039, 21
14f0: 29 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 );. MD5ST
1500: 45 50 28 46 34 2c 20 61 2c 20 62 2c 20 63 2c 20 EP(F4, a, b, c,
1510: 64 2c 20 69 6e 5b 31 32 5d 2b 30 78 36 35 35 62 d, in[12]+0x655b
1520: 35 39 63 33 2c 20 20 36 29 3b 0a 20 20 20 20 20 59c3, 6);.
1530: 20 20 20 4d 44 35 53 54 45 50 28 46 34 2c 20 64 MD5STEP(F4, d
1540: 2c 20 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 20 33 , a, b, c, in[ 3
1550: 5d 2b 30 78 38 66 30 63 63 63 39 32 2c 20 31 30 ]+0x8f0ccc92, 10
1560: 29 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 );. MD5ST
1570: 45 50 28 46 34 2c 20 63 2c 20 64 2c 20 61 2c 20 EP(F4, c, d, a,
1580: 62 2c 20 69 6e 5b 31 30 5d 2b 30 78 66 66 65 66 b, in[10]+0xffef
1590: 66 34 37 64 2c 20 31 35 29 3b 0a 20 20 20 20 20 f47d, 15);.
15a0: 20 20 20 4d 44 35 53 54 45 50 28 46 34 2c 20 62 MD5STEP(F4, b
15b0: 2c 20 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 31 , c, d, a, in[ 1
15c0: 5d 2b 30 78 38 35 38 34 35 64 64 31 2c 20 32 31 ]+0x85845dd1, 21
15d0: 29 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 );. MD5ST
15e0: 45 50 28 46 34 2c 20 61 2c 20 62 2c 20 63 2c 20 EP(F4, a, b, c,
15f0: 64 2c 20 69 6e 5b 20 38 5d 2b 30 78 36 66 61 38 d, in[ 8]+0x6fa8
1600: 37 65 34 66 2c 20 20 36 29 3b 0a 20 20 20 20 20 7e4f, 6);.
1610: 20 20 20 4d 44 35 53 54 45 50 28 46 34 2c 20 64 MD5STEP(F4, d
1620: 2c 20 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 31 35 , a, b, c, in[15
1630: 5d 2b 30 78 66 65 32 63 65 36 65 30 2c 20 31 30 ]+0xfe2ce6e0, 10
1640: 29 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 );. MD5ST
1650: 45 50 28 46 34 2c 20 63 2c 20 64 2c 20 61 2c 20 EP(F4, c, d, a,
1660: 62 2c 20 69 6e 5b 20 36 5d 2b 30 78 61 33 30 31 b, in[ 6]+0xa301
1670: 34 33 31 34 2c 20 31 35 29 3b 0a 20 20 20 20 20 4314, 15);.
1680: 20 20 20 4d 44 35 53 54 45 50 28 46 34 2c 20 62 MD5STEP(F4, b
1690: 2c 20 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 31 33 , c, d, a, in[13
16a0: 5d 2b 30 78 34 65 30 38 31 31 61 31 2c 20 32 31 ]+0x4e0811a1, 21
16b0: 29 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 );. MD5ST
16c0: 45 50 28 46 34 2c 20 61 2c 20 62 2c 20 63 2c 20 EP(F4, a, b, c,
16d0: 64 2c 20 69 6e 5b 20 34 5d 2b 30 78 66 37 35 33 d, in[ 4]+0xf753
16e0: 37 65 38 32 2c 20 20 36 29 3b 0a 20 20 20 20 20 7e82, 6);.
16f0: 20 20 20 4d 44 35 53 54 45 50 28 46 34 2c 20 64 MD5STEP(F4, d
1700: 2c 20 61 2c 20 62 2c 20 63 2c 20 69 6e 5b 31 31 , a, b, c, in[11
1710: 5d 2b 30 78 62 64 33 61 66 32 33 35 2c 20 31 30 ]+0xbd3af235, 10
1720: 29 3b 0a 20 20 20 20 20 20 20 20 4d 44 35 53 54 );. MD5ST
1730: 45 50 28 46 34 2c 20 63 2c 20 64 2c 20 61 2c 20 EP(F4, c, d, a,
1740: 62 2c 20 69 6e 5b 20 32 5d 2b 30 78 32 61 64 37 b, in[ 2]+0x2ad7
1750: 64 32 62 62 2c 20 31 35 29 3b 0a 20 20 20 20 20 d2bb, 15);.
1760: 20 20 20 4d 44 35 53 54 45 50 28 46 34 2c 20 62 MD5STEP(F4, b
1770: 2c 20 63 2c 20 64 2c 20 61 2c 20 69 6e 5b 20 39 , c, d, a, in[ 9
1780: 5d 2b 30 78 65 62 38 36 64 33 39 31 2c 20 32 31 ]+0xeb86d391, 21
1790: 29 3b 0a 0a 20 20 20 20 20 20 20 20 62 75 66 5b );.. buf[
17a0: 30 5d 20 2b 3d 20 61 3b 0a 20 20 20 20 20 20 20 0] += a;.
17b0: 20 62 75 66 5b 31 5d 20 2b 3d 20 62 3b 0a 20 20 buf[1] += b;.
17c0: 20 20 20 20 20 20 62 75 66 5b 32 5d 20 2b 3d 20 buf[2] +=
17d0: 63 3b 0a 20 20 20 20 20 20 20 20 62 75 66 5b 33 c;. buf[3
17e0: 5d 20 2b 3d 20 64 3b 0a 7d 0a 0a 2f 2a 0a 20 2a ] += d;.}../*. *
17f0: 20 53 74 61 72 74 20 4d 44 35 20 61 63 63 75 6d Start MD5 accum
1800: 75 6c 61 74 69 6f 6e 2e 20 20 53 65 74 20 62 69 ulation. Set bi
1810: 74 20 63 6f 75 6e 74 20 74 6f 20 30 20 61 6e 64 t count to 0 and
1820: 20 62 75 66 66 65 72 20 74 6f 20 6d 79 73 74 65 buffer to myste
1830: 72 69 6f 75 73 0a 20 2a 20 69 6e 69 74 69 61 6c rious. * initial
1840: 69 7a 61 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 ization constant
1850: 73 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f s.. */.static vo
1860: 69 64 20 4d 44 35 49 6e 69 74 28 4d 44 35 43 6f id MD5Init(MD5Co
1870: 6e 74 65 78 74 20 2a 63 74 78 29 7b 0a 09 63 74 ntext *ctx){..ct
1880: 78 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 x->isInit = 1;.
1890: 20 20 20 20 20 20 20 63 74 78 2d 3e 62 75 66 5b ctx->buf[
18a0: 30 5d 20 3d 20 30 78 36 37 34 35 32 33 30 31 3b 0] = 0x67452301;
18b0: 0a 20 20 20 20 20 20 20 20 63 74 78 2d 3e 62 75 . ctx->bu
18c0: 66 5b 31 5d 20 3d 20 30 78 65 66 63 64 61 62 38 f[1] = 0xefcdab8
18d0: 39 3b 0a 20 20 20 20 20 20 20 20 63 74 78 2d 3e 9;. ctx->
18e0: 62 75 66 5b 32 5d 20 3d 20 30 78 39 38 62 61 64 buf[2] = 0x98bad
18f0: 63 66 65 3b 0a 20 20 20 20 20 20 20 20 63 74 78 cfe;. ctx
1900: 2d 3e 62 75 66 5b 33 5d 20 3d 20 30 78 31 30 33 ->buf[3] = 0x103
1910: 32 35 34 37 36 3b 0a 20 20 20 20 20 20 20 20 63 25476;. c
1920: 74 78 2d 3e 62 69 74 73 5b 30 5d 20 3d 20 30 3b tx->bits[0] = 0;
1930: 0a 20 20 20 20 20 20 20 20 63 74 78 2d 3e 62 69 . ctx->bi
1940: 74 73 5b 31 5d 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a ts[1] = 0;.}../*
1950: 0a 20 2a 20 55 70 64 61 74 65 20 63 6f 6e 74 65 . * Update conte
1960: 78 74 20 74 6f 20 72 65 66 6c 65 63 74 20 74 68 xt to reflect th
1970: 65 20 63 6f 6e 63 61 74 65 6e 61 74 69 6f 6e 20 e concatenation
1980: 6f 66 20 61 6e 6f 74 68 65 72 20 62 75 66 66 65 of another buffe
1990: 72 20 66 75 6c 6c 0a 20 2a 20 6f 66 20 62 79 74 r full. * of byt
19a0: 65 73 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 0a es.. */.static .
19b0: 76 6f 69 64 20 4d 44 35 55 70 64 61 74 65 28 4d void MD5Update(M
19c0: 44 35 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c D5Context *pCtx,
19d0: 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
19e0: 63 68 61 72 20 2a 62 75 66 2c 20 75 6e 73 69 67 char *buf, unsig
19f0: 6e 65 64 20 69 6e 74 20 6c 65 6e 29 7b 0a 20 20 ned int len){.
1a00: 20 20 20 20 20 20 73 74 72 75 63 74 20 43 6f 6e struct Con
1a10: 74 65 78 74 20 2a 63 74 78 20 3d 20 28 73 74 72 text *ctx = (str
1a20: 75 63 74 20 43 6f 6e 74 65 78 74 20 2a 29 70 43 uct Context *)pC
1a30: 74 78 3b 0a 20 20 20 20 20 20 20 20 75 69 6e 74 tx;. uint
1a40: 33 32 20 74 3b 0a 0a 20 20 20 20 20 20 20 20 2f 32 t;.. /
1a50: 2a 20 55 70 64 61 74 65 20 62 69 74 63 6f 75 6e * Update bitcoun
1a60: 74 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20 74 20 t */.. t
1a70: 3d 20 63 74 78 2d 3e 62 69 74 73 5b 30 5d 3b 0a = ctx->bits[0];.
1a80: 20 20 20 20 20 20 20 20 69 66 20 28 28 63 74 78 if ((ctx
1a90: 2d 3e 62 69 74 73 5b 30 5d 20 3d 20 74 20 2b 20 ->bits[0] = t +
1aa0: 28 28 75 69 6e 74 33 32 29 6c 65 6e 20 3c 3c 20 ((uint32)len <<
1ab0: 33 29 29 20 3c 20 74 29 0a 20 20 20 20 20 20 20 3)) < t).
1ac0: 20 20 20 20 20 20 20 20 20 63 74 78 2d 3e 62 69 ctx->bi
1ad0: 74 73 5b 31 5d 2b 2b 3b 20 2f 2a 20 43 61 72 72 ts[1]++; /* Carr
1ae0: 79 20 66 72 6f 6d 20 6c 6f 77 20 74 6f 20 68 69 y from low to hi
1af0: 67 68 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 74 gh */. ct
1b00: 78 2d 3e 62 69 74 73 5b 31 5d 20 2b 3d 20 6c 65 x->bits[1] += le
1b10: 6e 20 3e 3e 20 32 39 3b 0a 0a 20 20 20 20 20 20 n >> 29;..
1b20: 20 20 74 20 3d 20 28 74 20 3e 3e 20 33 29 20 26 t = (t >> 3) &
1b30: 20 30 78 33 66 3b 20 20 20 20 2f 2a 20 42 79 74 0x3f; /* Byt
1b40: 65 73 20 61 6c 72 65 61 64 79 20 69 6e 20 73 68 es already in sh
1b50: 73 49 6e 66 6f 2d 3e 64 61 74 61 20 2a 2f 0a 0a sInfo->data */..
1b60: 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c /* Handl
1b70: 65 20 61 6e 79 20 6c 65 61 64 69 6e 67 20 6f 64 e any leading od
1b80: 64 2d 73 69 7a 65 64 20 63 68 75 6e 6b 73 20 2a d-sized chunks *
1b90: 2f 0a 0a 20 20 20 20 20 20 20 20 69 66 20 28 20 /.. if (
1ba0: 74 20 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 t ) {.
1bb0: 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 unsigned c
1bc0: 68 61 72 20 2a 70 20 3d 20 28 75 6e 73 69 67 6e har *p = (unsign
1bd0: 65 64 20 63 68 61 72 20 2a 29 63 74 78 2d 3e 69 ed char *)ctx->i
1be0: 6e 20 2b 20 74 3b 0a 0a 20 20 20 20 20 20 20 20 n + t;..
1bf0: 20 20 20 20 20 20 20 20 74 20 3d 20 36 34 2d 74 t = 64-t
1c00: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1c10: 20 20 69 66 20 28 6c 65 6e 20 3c 20 74 29 20 7b if (len < t) {
1c20: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1c30: 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
1c40: 70 2c 20 62 75 66 2c 20 6c 65 6e 29 3b 0a 20 20 p, buf, len);.
1c50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c60: 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
1c70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a }.
1c80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c90: 6d 65 6d 63 70 79 28 70 2c 20 62 75 66 2c 20 74 memcpy(p, buf, t
1ca0: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
1cb0: 20 20 20 62 79 74 65 52 65 76 65 72 73 65 28 63 byteReverse(c
1cc0: 74 78 2d 3e 69 6e 2c 20 31 36 29 3b 0a 20 20 20 tx->in, 16);.
1cd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 4d 44 35 MD5
1ce0: 54 72 61 6e 73 66 6f 72 6d 28 63 74 78 2d 3e 62 Transform(ctx->b
1cf0: 75 66 2c 20 28 75 69 6e 74 33 32 20 2a 29 63 74 uf, (uint32 *)ct
1d00: 78 2d 3e 69 6e 29 3b 0a 20 20 20 20 20 20 20 20 x->in);.
1d10: 20 20 20 20 20 20 20 20 62 75 66 20 2b 3d 20 74 buf += t
1d20: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1d30: 20 20 6c 65 6e 20 2d 3d 20 74 3b 0a 20 20 20 20 len -= t;.
1d40: 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f }.. /
1d50: 2a 20 50 72 6f 63 65 73 73 20 64 61 74 61 20 69 * Process data i
1d60: 6e 20 36 34 2d 62 79 74 65 20 63 68 75 6e 6b 73 n 64-byte chunks
1d70: 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20 77 68 69 */.. whi
1d80: 6c 65 20 28 6c 65 6e 20 3e 3d 20 36 34 29 20 7b le (len >= 64) {
1d90: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1da0: 20 6d 65 6d 63 70 79 28 63 74 78 2d 3e 69 6e 2c memcpy(ctx->in,
1db0: 20 62 75 66 2c 20 36 34 29 3b 0a 20 20 20 20 20 buf, 64);.
1dc0: 20 20 20 20 20 20 20 20 20 20 20 62 79 74 65 52 byteR
1dd0: 65 76 65 72 73 65 28 63 74 78 2d 3e 69 6e 2c 20 everse(ctx->in,
1de0: 31 36 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 16);.
1df0: 20 20 20 20 20 4d 44 35 54 72 61 6e 73 66 6f 72 MD5Transfor
1e00: 6d 28 63 74 78 2d 3e 62 75 66 2c 20 28 75 69 6e m(ctx->buf, (uin
1e10: 74 33 32 20 2a 29 63 74 78 2d 3e 69 6e 29 3b 0a t32 *)ctx->in);.
1e20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e30: 62 75 66 20 2b 3d 20 36 34 3b 0a 20 20 20 20 20 buf += 64;.
1e40: 20 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 2d len -
1e50: 3d 20 36 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a = 64;. }.
1e60: 0a 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 . /* Hand
1e70: 6c 65 20 61 6e 79 20 72 65 6d 61 69 6e 69 6e 67 le any remaining
1e80: 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e 20 bytes of data.
1e90: 2a 2f 0a 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 */.. memc
1ea0: 70 79 28 63 74 78 2d 3e 69 6e 2c 20 62 75 66 2c py(ctx->in, buf,
1eb0: 20 6c 65 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 20 len);.}../*. *
1ec0: 46 69 6e 61 6c 20 77 72 61 70 75 70 20 2d 20 70 Final wrapup - p
1ed0: 61 64 20 74 6f 20 36 34 2d 62 79 74 65 20 62 6f ad to 64-byte bo
1ee0: 75 6e 64 61 72 79 20 77 69 74 68 20 74 68 65 20 undary with the
1ef0: 62 69 74 20 70 61 74 74 65 72 6e 20 0a 20 2a 20 bit pattern . *
1f00: 31 20 30 2a 20 28 36 34 2d 62 69 74 20 63 6f 75 1 0* (64-bit cou
1f10: 6e 74 20 6f 66 20 62 69 74 73 20 70 72 6f 63 65 nt of bits proce
1f20: 73 73 65 64 2c 20 4d 53 42 2d 66 69 72 73 74 29 ssed, MSB-first)
1f30: 0a 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 . */.static void
1f40: 20 4d 44 35 46 69 6e 61 6c 28 75 6e 73 69 67 6e MD5Final(unsign
1f50: 65 64 20 63 68 61 72 20 64 69 67 65 73 74 5b 31 ed char digest[1
1f60: 36 5d 2c 20 4d 44 35 43 6f 6e 74 65 78 74 20 2a 6], MD5Context *
1f70: 70 43 74 78 29 7b 0a 20 20 20 20 20 20 20 20 73 pCtx){. s
1f80: 74 72 75 63 74 20 43 6f 6e 74 65 78 74 20 2a 63 truct Context *c
1f90: 74 78 20 3d 20 28 73 74 72 75 63 74 20 43 6f 6e tx = (struct Con
1fa0: 74 65 78 74 20 2a 29 70 43 74 78 3b 0a 20 20 20 text *)pCtx;.
1fb0: 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 6f unsigned co
1fc0: 75 6e 74 3b 0a 20 20 20 20 20 20 20 20 75 6e 73 unt;. uns
1fd0: 69 67 6e 65 64 20 63 68 61 72 20 2a 70 3b 0a 0a igned char *p;..
1fe0: 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 75 /* Compu
1ff0: 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 te number of byt
2000: 65 73 20 6d 6f 64 20 36 34 20 2a 2f 0a 20 20 20 es mod 64 */.
2010: 20 20 20 20 20 63 6f 75 6e 74 20 3d 20 28 63 74 count = (ct
2020: 78 2d 3e 62 69 74 73 5b 30 5d 20 3e 3e 20 33 29 x->bits[0] >> 3)
2030: 20 26 20 30 78 33 46 3b 0a 0a 20 20 20 20 20 20 & 0x3F;..
2040: 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 69 72 /* Set the fir
2050: 73 74 20 63 68 61 72 20 6f 66 20 70 61 64 64 69 st char of paddi
2060: 6e 67 20 74 6f 20 30 78 38 30 2e 20 20 54 68 69 ng to 0x80. Thi
2070: 73 20 69 73 20 73 61 66 65 20 73 69 6e 63 65 20 s is safe since
2080: 74 68 65 72 65 20 69 73 0a 20 20 20 20 20 20 20 there is.
2090: 20 20 20 20 61 6c 77 61 79 73 20 61 74 20 6c 65 always at le
20a0: 61 73 74 20 6f 6e 65 20 62 79 74 65 20 66 72 65 ast one byte fre
20b0: 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 20 3d e */. p =
20c0: 20 63 74 78 2d 3e 69 6e 20 2b 20 63 6f 75 6e 74 ctx->in + count
20d0: 3b 0a 20 20 20 20 20 20 20 20 2a 70 2b 2b 20 3d ;. *p++ =
20e0: 20 30 78 38 30 3b 0a 0a 20 20 20 20 20 20 20 20 0x80;..
20f0: 2f 2a 20 42 79 74 65 73 20 6f 66 20 70 61 64 64 /* Bytes of padd
2100: 69 6e 67 20 6e 65 65 64 65 64 20 74 6f 20 6d 61 ing needed to ma
2110: 6b 65 20 36 34 20 62 79 74 65 73 20 2a 2f 0a 20 ke 64 bytes */.
2120: 20 20 20 20 20 20 20 63 6f 75 6e 74 20 3d 20 36 count = 6
2130: 34 20 2d 20 31 20 2d 20 63 6f 75 6e 74 3b 0a 0a 4 - 1 - count;..
2140: 20 20 20 20 20 20 20 20 2f 2a 20 50 61 64 20 6f /* Pad o
2150: 75 74 20 74 6f 20 35 36 20 6d 6f 64 20 36 34 20 ut to 56 mod 64
2160: 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 20 28 63 */. if (c
2170: 6f 75 6e 74 20 3c 20 38 29 20 7b 0a 20 20 20 20 ount < 8) {.
2180: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
2190: 77 6f 20 6c 6f 74 73 20 6f 66 20 70 61 64 64 69 wo lots of paddi
21a0: 6e 67 3a 20 20 50 61 64 20 74 68 65 20 66 69 72 ng: Pad the fir
21b0: 73 74 20 62 6c 6f 63 6b 20 74 6f 20 36 34 20 62 st block to 64 b
21c0: 79 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 ytes */.
21d0: 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 memset(p
21e0: 2c 20 30 2c 20 63 6f 75 6e 74 29 3b 0a 20 20 20 , 0, count);.
21f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 62 79 74 byt
2200: 65 52 65 76 65 72 73 65 28 63 74 78 2d 3e 69 6e eReverse(ctx->in
2210: 2c 20 31 36 29 3b 0a 20 20 20 20 20 20 20 20 20 , 16);.
2220: 20 20 20 20 20 20 20 4d 44 35 54 72 61 6e 73 66 MD5Transf
2230: 6f 72 6d 28 63 74 78 2d 3e 62 75 66 2c 20 28 75 orm(ctx->buf, (u
2240: 69 6e 74 33 32 20 2a 29 63 74 78 2d 3e 69 6e 29 int32 *)ctx->in)
2250: 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ;..
2260: 20 20 20 2f 2a 20 4e 6f 77 20 66 69 6c 6c 20 74 /* Now fill t
2270: 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 77 69 he next block wi
2280: 74 68 20 35 36 20 62 79 74 65 73 20 2a 2f 0a 20 th 56 bytes */.
2290: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d m
22a0: 65 6d 73 65 74 28 63 74 78 2d 3e 69 6e 2c 20 30 emset(ctx->in, 0
22b0: 2c 20 35 36 29 3b 0a 20 20 20 20 20 20 20 20 7d , 56);. }
22c0: 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 else {.
22d0: 20 20 20 20 20 20 20 20 2f 2a 20 50 61 64 20 62 /* Pad b
22e0: 6c 6f 63 6b 20 74 6f 20 35 36 20 62 79 74 65 73 lock to 56 bytes
22f0: 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
2300: 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c memset(p, 0,
2310: 20 63 6f 75 6e 74 2d 38 29 3b 0a 20 20 20 20 20 count-8);.
2320: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 79 74 }. byt
2330: 65 52 65 76 65 72 73 65 28 63 74 78 2d 3e 69 6e eReverse(ctx->in
2340: 2c 20 31 34 29 3b 0a 0a 20 20 20 20 20 20 20 20 , 14);..
2350: 2f 2a 20 41 70 70 65 6e 64 20 6c 65 6e 67 74 68 /* Append length
2360: 20 69 6e 20 62 69 74 73 20 61 6e 64 20 74 72 61 in bits and tra
2370: 6e 73 66 6f 72 6d 20 2a 2f 0a 20 20 20 20 20 20 nsform */.
2380: 20 20 28 28 75 69 6e 74 33 32 20 2a 29 63 74 78 ((uint32 *)ctx
2390: 2d 3e 69 6e 29 5b 20 31 34 20 5d 20 3d 20 63 74 ->in)[ 14 ] = ct
23a0: 78 2d 3e 62 69 74 73 5b 30 5d 3b 0a 20 20 20 20 x->bits[0];.
23b0: 20 20 20 20 28 28 75 69 6e 74 33 32 20 2a 29 63 ((uint32 *)c
23c0: 74 78 2d 3e 69 6e 29 5b 20 31 35 20 5d 20 3d 20 tx->in)[ 15 ] =
23d0: 63 74 78 2d 3e 62 69 74 73 5b 31 5d 3b 0a 0a 20 ctx->bits[1];..
23e0: 20 20 20 20 20 20 20 4d 44 35 54 72 61 6e 73 66 MD5Transf
23f0: 6f 72 6d 28 63 74 78 2d 3e 62 75 66 2c 20 28 75 orm(ctx->buf, (u
2400: 69 6e 74 33 32 20 2a 29 63 74 78 2d 3e 69 6e 29 int32 *)ctx->in)
2410: 3b 0a 20 20 20 20 20 20 20 20 62 79 74 65 52 65 ;. byteRe
2420: 76 65 72 73 65 28 28 75 6e 73 69 67 6e 65 64 20 verse((unsigned
2430: 63 68 61 72 20 2a 29 63 74 78 2d 3e 62 75 66 2c char *)ctx->buf,
2440: 20 34 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 4);. mem
2450: 63 70 79 28 64 69 67 65 73 74 2c 20 63 74 78 2d cpy(digest, ctx-
2460: 3e 62 75 66 2c 20 31 36 29 3b 0a 20 20 20 20 20 >buf, 16);.
2470: 20 20 20 6d 65 6d 73 65 74 28 63 74 78 2c 20 30 memset(ctx, 0
2480: 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 20 , sizeof(ctx));
2490: 20 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 69 74 /* In case it
24a0: 27 73 20 73 65 6e 73 69 74 69 76 65 20 2a 2f 0a 's sensitive */.
24b0: 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
24c0: 20 61 20 64 69 67 65 73 74 20 69 6e 74 6f 20 62 a digest into b
24d0: 61 73 65 2d 31 36 2e 20 20 64 69 67 65 73 74 20 ase-16. digest
24e0: 73 68 6f 75 6c 64 20 62 65 20 64 65 63 6c 61 72 should be declar
24f0: 65 64 20 61 73 0a 2a 2a 20 22 75 6e 73 69 67 6e ed as.** "unsign
2500: 65 64 20 63 68 61 72 20 64 69 67 65 73 74 5b 31 ed char digest[1
2510: 36 5d 22 20 69 6e 20 74 68 65 20 63 61 6c 6c 69 6]" in the calli
2520: 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 ng function. Th
2530: 65 20 4d 44 35 0a 2a 2a 20 64 69 67 65 73 74 20 e MD5.** digest
2540: 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 is stored in the
2550: 20 66 69 72 73 74 20 31 36 20 62 79 74 65 73 2e first 16 bytes.
2560: 20 20 7a 42 75 66 20 73 68 6f 75 6c 64 0a 2a 2a zBuf should.**
2570: 20 62 65 20 22 63 68 61 72 20 7a 42 75 66 5b 33 be "char zBuf[3
2580: 33 5d 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 3]"..*/.static v
2590: 6f 69 64 20 44 69 67 65 73 74 54 6f 42 61 73 65 oid DigestToBase
25a0: 31 36 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 16(unsigned char
25b0: 20 2a 64 69 67 65 73 74 2c 20 63 68 61 72 20 2a *digest, char *
25c0: 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 69 63 20 zBuf){. static
25d0: 63 68 61 72 20 63 6f 6e 73 74 20 7a 45 6e 63 6f char const zEnco
25e0: 64 65 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 de[] = "01234567
25f0: 38 39 61 62 63 64 65 66 22 3b 0a 20 20 69 6e 74 89abcdef";. int
2600: 20 69 2c 20 6a 3b 0a 0a 20 20 66 6f 72 28 6a 3d i, j;.. for(j=
2610: 69 3d 30 3b 20 69 3c 31 36 3b 20 69 2b 2b 29 7b i=0; i<16; i++){
2620: 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 64 69 67 . int a = dig
2630: 65 73 74 5b 69 5d 3b 0a 20 20 20 20 7a 42 75 66 est[i];. zBuf
2640: 5b 6a 2b 2b 5d 20 3d 20 7a 45 6e 63 6f 64 65 5b [j++] = zEncode[
2650: 28 61 3e 3e 34 29 26 30 78 66 5d 3b 0a 20 20 20 (a>>4)&0xf];.
2660: 20 7a 42 75 66 5b 6a 2b 2b 5d 20 3d 20 7a 45 6e zBuf[j++] = zEn
2670: 63 6f 64 65 5b 61 20 26 20 30 78 66 5d 3b 0a 20 code[a & 0xf];.
2680: 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 }. zBuf[j] = 0
2690: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 ;.}../*.** The s
26a0: 74 61 74 65 20 6f 66 20 61 20 69 6e 63 72 65 6d tate of a increm
26b0: 65 6e 74 61 6c 20 4d 44 35 20 63 68 65 63 6b 73 ental MD5 checks
26c0: 75 6d 20 63 6f 6d 70 75 74 61 74 69 6f 6e 2e 20 um computation.
26d0: 20 4f 6e 6c 79 20 6f 6e 65 0a 2a 2a 20 73 75 63 Only one.** suc
26e0: 68 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 63 61 h computation ca
26f0: 6e 20 62 65 20 75 6e 64 65 72 77 61 79 20 61 74 n be underway at
2700: 20 61 20 74 69 6d 65 2c 20 6f 66 20 63 6f 75 72 a time, of cour
2710: 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 44 se..*/.static MD
2720: 35 43 6f 6e 74 65 78 74 20 69 6e 63 72 43 74 78 5Context incrCtx
2730: 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63 ;.static int inc
2740: 72 49 6e 69 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a rInit = 0;../*.*
2750: 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
2760: 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 4d 44 35 incremental MD5
2770: 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2f 0a 76 6f checksum..*/.vo
2780: 69 64 20 6d 64 35 73 75 6d 5f 69 6e 69 74 28 76 id md5sum_init(v
2790: 6f 69 64 29 7b 0a 20 20 69 6e 63 72 49 6e 69 74 oid){. incrInit
27a0: 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 = 0;.}../*.** A
27b0: 64 64 20 6d 6f 72 65 20 74 65 78 74 20 74 6f 20 dd more text to
27c0: 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 the incremental
27d0: 4d 44 35 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2f MD5 checksum..*/
27e0: 0a 76 6f 69 64 20 6d 64 35 73 75 6d 5f 73 74 65 .void md5sum_ste
27f0: 70 5f 74 65 78 74 28 63 6f 6e 73 74 20 63 68 61 p_text(const cha
2800: 72 20 2a 7a 54 65 78 74 2c 20 69 6e 74 20 6e 42 r *zText, int nB
2810: 79 74 65 73 29 7b 0a 20 20 69 66 28 20 21 69 6e ytes){. if( !in
2820: 63 72 49 6e 69 74 20 29 7b 0a 20 20 20 20 4d 44 crInit ){. MD
2830: 35 49 6e 69 74 28 26 69 6e 63 72 43 74 78 29 3b 5Init(&incrCtx);
2840: 0a 20 20 20 20 69 6e 63 72 49 6e 69 74 20 3d 20 . incrInit =
2850: 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 1;. }. if( nBy
2860: 74 65 73 3c 3d 30 20 29 7b 0a 20 20 20 20 69 66 tes<=0 ){. if
2870: 28 20 6e 42 79 74 65 73 3d 3d 30 20 29 20 72 65 ( nBytes==0 ) re
2880: 74 75 72 6e 3b 0a 20 20 20 20 6e 42 79 74 65 73 turn;. nBytes
2890: 20 3d 20 73 74 72 6c 65 6e 28 7a 54 65 78 74 29 = strlen(zText)
28a0: 3b 0a 20 20 7d 0a 20 20 4d 44 35 55 70 64 61 74 ;. }. MD5Updat
28b0: 65 28 26 69 6e 63 72 43 74 78 2c 20 28 75 6e 73 e(&incrCtx, (uns
28c0: 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 54 65 78 igned char*)zTex
28d0: 74 2c 20 6e 42 79 74 65 73 29 3b 0a 7d 0a 0a 2f t, nBytes);.}../
28e0: 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 63 6f 6e *.** Add the con
28f0: 74 65 6e 74 20 6f 66 20 61 20 62 6c 6f 62 20 74 tent of a blob t
2900: 6f 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 o the incrementa
2910: 6c 20 4d 44 35 20 63 68 65 63 6b 73 75 6d 2e 0a l MD5 checksum..
2920: 2a 2f 0a 76 6f 69 64 20 6d 64 35 73 75 6d 5f 73 */.void md5sum_s
2930: 74 65 70 5f 62 6c 6f 62 28 42 6c 6f 62 20 2a 70 tep_blob(Blob *p
2940: 29 7b 0a 20 20 6d 64 35 73 75 6d 5f 73 74 65 70 ){. md5sum_step
2950: 5f 74 65 78 74 28 62 6c 6f 62 5f 62 75 66 66 65 _text(blob_buffe
2960: 72 28 70 29 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 r(p), blob_size(
2970: 70 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 p));.}../*.** Fi
2980: 6e 69 73 68 20 74 68 65 20 69 6e 63 72 65 6d 65 nish the increme
2990: 6e 74 61 6c 20 4d 44 35 20 63 68 65 63 6b 73 75 ntal MD5 checksu
29a0: 6d 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72 65 m. Store the re
29b0: 73 75 6c 74 20 69 6e 20 62 6c 6f 62 20 70 4f 75 sult in blob pOu
29c0: 74 0a 2a 2a 20 69 66 20 70 4f 75 74 21 3d 30 2e t.** if pOut!=0.
29d0: 20 20 41 6c 73 6f 20 72 65 74 75 72 6e 20 61 20 Also return a
29e0: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 pointer to the r
29f0: 65 73 75 6c 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 esult. .**.** T
2a00: 68 69 73 20 72 65 73 65 74 73 20 74 68 65 20 69 his resets the i
2a10: 6e 63 72 65 6d 65 6e 74 61 6c 20 63 68 65 63 6b ncremental check
2a20: 73 75 6d 20 70 72 65 70 61 72 69 6e 67 20 66 6f sum preparing fo
2a30: 72 20 74 68 65 20 6e 65 78 74 20 72 6f 75 6e 64 r the next round
2a40: 0a 2a 2a 20 6f 66 20 63 6f 6d 70 75 74 61 74 69 .** of computati
2a50: 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 on. The return
2a60: 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74 pointer points t
2a70: 6f 20 61 20 73 74 61 74 69 63 20 62 75 66 66 65 o a static buffe
2a80: 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 6f 76 65 r that.** is ove
2a90: 72 77 72 69 74 74 65 6e 20 62 79 20 73 75 62 73 rwritten by subs
2aa0: 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 equent calls to
2ab0: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a this function..*
2ac0: 2f 0a 63 68 61 72 20 2a 6d 64 35 73 75 6d 5f 66 /.char *md5sum_f
2ad0: 69 6e 69 73 68 28 42 6c 6f 62 20 2a 70 4f 75 74 inish(Blob *pOut
2ae0: 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 ){. unsigned ch
2af0: 61 72 20 7a 52 65 73 75 6c 74 5b 31 36 5d 3b 0a ar zResult[16];.
2b00: 20 20 73 74 61 74 69 63 20 63 68 61 72 20 7a 4f static char zO
2b10: 75 74 5b 33 33 5d 3b 0a 20 20 6d 64 35 73 75 6d ut[33];. md5sum
2b20: 5f 73 74 65 70 5f 74 65 78 74 28 30 2c 30 29 3b _step_text(0,0);
2b30: 0a 20 20 4d 44 35 46 69 6e 61 6c 28 7a 52 65 73 . MD5Final(zRes
2b40: 75 6c 74 2c 20 26 69 6e 63 72 43 74 78 29 3b 0a ult, &incrCtx);.
2b50: 20 20 69 6e 63 72 49 6e 69 74 20 3d 20 30 3b 0a incrInit = 0;.
2b60: 20 20 44 69 67 65 73 74 54 6f 42 61 73 65 31 36 DigestToBase16
2b70: 28 7a 52 65 73 75 6c 74 2c 20 7a 4f 75 74 29 3b (zResult, zOut);
2b80: 0a 20 20 69 66 28 20 70 4f 75 74 20 29 7b 0a 20 . if( pOut ){.
2b90: 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 70 4f 75 blob_zero(pOu
2ba0: 74 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 t);. blob_app
2bb0: 65 6e 64 28 70 4f 75 74 2c 20 7a 4f 75 74 2c 20 end(pOut, zOut,
2bc0: 33 32 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 32);. }. retur
2bd0: 6e 20 7a 4f 75 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a n zOut;.}.../*.*
2be0: 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 4d 44 * Compute the MD
2bf0: 35 20 63 68 65 63 6b 73 75 6d 20 6f 66 20 61 20 5 checksum of a
2c00: 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 20 53 file on disk. S
2c10: 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 69 tore the resulti
2c20: 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 20 69 ng.** checksum i
2c30: 6e 20 74 68 65 20 62 6c 6f 62 20 70 43 6b 73 75 n the blob pCksu
2c40: 6d 2e 20 20 70 43 6b 73 75 6d 20 69 73 20 61 73 m. pCksum is as
2c50: 73 75 6d 65 64 20 74 6f 20 62 65 20 69 6e 69 6e sumed to be inin
2c60: 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a itialized..**.**
2c70: 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
2c80: 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2f er of errors..*/
2c90: 0a 69 6e 74 20 6d 64 35 73 75 6d 5f 66 69 6c 65 .int md5sum_file
2ca0: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 (const char *zFi
2cb0: 6c 65 6e 61 6d 65 2c 20 42 6c 6f 62 20 2a 70 43 lename, Blob *pC
2cc0: 6b 73 75 6d 29 7b 0a 20 20 46 49 4c 45 20 2a 69 ksum){. FILE *i
2cd0: 6e 3b 0a 20 20 4d 44 35 43 6f 6e 74 65 78 74 20 n;. MD5Context
2ce0: 63 74 78 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 ctx;. unsigned
2cf0: 63 68 61 72 20 7a 52 65 73 75 6c 74 5b 31 36 5d char zResult[16]
2d00: 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 ;. char zBuf[10
2d10: 32 34 30 5d 3b 0a 0a 20 20 69 6e 20 3d 20 66 6f 240];.. in = fo
2d20: 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 72 pen(zFilename,"r
2d30: 62 22 29 3b 0a 20 20 69 66 28 20 69 6e 3d 3d 30 b");. if( in==0
2d40: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ){. return 1
2d50: 3b 0a 20 20 7d 0a 20 20 4d 44 35 49 6e 69 74 28 ;. }. MD5Init(
2d60: 26 63 74 78 29 3b 0a 20 20 66 6f 72 28 3b 3b 29 &ctx);. for(;;)
2d70: 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 {. int n;.
2d80: 20 6e 20 3d 20 66 72 65 61 64 28 7a 42 75 66 2c n = fread(zBuf,
2d90: 20 31 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 1, sizeof(zBuf)
2da0: 2c 20 69 6e 29 3b 0a 20 20 20 20 69 66 28 20 6e , in);. if( n
2db0: 3c 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 <=0 ) break;.
2dc0: 20 4d 44 35 55 70 64 61 74 65 28 26 63 74 78 2c MD5Update(&ctx,
2dd0: 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a (unsigned char*
2de0: 29 7a 42 75 66 2c 20 28 75 6e 73 69 67 6e 65 64 )zBuf, (unsigned
2df0: 29 6e 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 )n);. }. fclos
2e00: 65 28 69 6e 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 e(in);. blob_ze
2e10: 72 6f 28 70 43 6b 73 75 6d 29 3b 0a 20 20 62 6c ro(pCksum);. bl
2e20: 6f 62 5f 72 65 73 69 7a 65 28 70 43 6b 73 75 6d ob_resize(pCksum
2e30: 2c 20 33 32 29 3b 0a 20 20 4d 44 35 46 69 6e 61 , 32);. MD5Fina
2e40: 6c 28 7a 52 65 73 75 6c 74 2c 20 26 63 74 78 29 l(zResult, &ctx)
2e50: 3b 0a 20 20 44 69 67 65 73 74 54 6f 42 61 73 65 ;. DigestToBase
2e60: 31 36 28 7a 52 65 73 75 6c 74 2c 20 62 6c 6f 62 16(zResult, blob
2e70: 5f 62 75 66 66 65 72 28 70 43 6b 73 75 6d 29 29 _buffer(pCksum))
2e80: 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a ;. return 0;.}.
2e90: 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 ./*.** Compute t
2ea0: 68 65 20 4d 44 35 20 63 68 65 63 6b 73 75 6d 20 he MD5 checksum
2eb0: 6f 66 20 61 20 62 6c 6f 62 20 69 6e 20 6d 65 6d of a blob in mem
2ec0: 6f 72 79 2e 20 20 53 74 6f 72 65 20 74 68 65 20 ory. Store the
2ed0: 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 63 68 65 resulting.** che
2ee0: 63 6b 73 75 6d 20 69 6e 20 74 68 65 20 62 6c 6f cksum in the blo
2ef0: 62 20 70 43 6b 73 75 6d 2e 20 20 70 43 6b 73 75 b pCksum. pCksu
2f00: 6d 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 m is assumed to
2f10: 62 65 20 65 69 74 68 65 72 0a 2a 2a 20 75 6e 69 be either.** uni
2f20: 6e 69 74 69 61 6c 69 7a 65 64 20 6f 72 20 74 68 nitialized or th
2f30: 65 20 73 61 6d 65 20 62 6c 6f 62 20 61 73 20 70 e same blob as p
2f40: 49 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e In..**.** Return
2f50: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
2f60: 72 72 6f 72 73 2e 0a 2a 2f 0a 69 6e 74 20 6d 64 rrors..*/.int md
2f70: 35 73 75 6d 5f 62 6c 6f 62 28 63 6f 6e 73 74 20 5sum_blob(const
2f80: 42 6c 6f 62 20 2a 70 49 6e 2c 20 42 6c 6f 62 20 Blob *pIn, Blob
2f90: 2a 70 43 6b 73 75 6d 29 7b 0a 20 20 4d 44 35 43 *pCksum){. MD5C
2fa0: 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 75 6e ontext ctx;. un
2fb0: 73 69 67 6e 65 64 20 63 68 61 72 20 7a 52 65 73 signed char zRes
2fc0: 75 6c 74 5b 31 36 5d 3b 0a 0a 20 20 4d 44 35 49 ult[16];.. MD5I
2fd0: 6e 69 74 28 26 63 74 78 29 3b 0a 20 20 4d 44 35 nit(&ctx);. MD5
2fe0: 55 70 64 61 74 65 28 26 63 74 78 2c 20 28 75 6e Update(&ctx, (un
2ff0: 73 69 67 6e 65 64 20 63 68 61 72 2a 29 62 6c 6f signed char*)blo
3000: 62 5f 62 75 66 66 65 72 28 70 49 6e 29 2c 20 62 b_buffer(pIn), b
3010: 6c 6f 62 5f 73 69 7a 65 28 70 49 6e 29 29 3b 0a lob_size(pIn));.
3020: 20 20 69 66 28 20 70 49 6e 3d 3d 70 43 6b 73 75 if( pIn==pCksu
3030: 6d 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 m ){. blob_re
3040: 73 65 74 28 70 43 6b 73 75 6d 29 3b 0a 20 20 7d set(pCksum);. }
3050: 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62 5f 7a else{. blob_z
3060: 65 72 6f 28 70 43 6b 73 75 6d 29 3b 0a 20 20 7d ero(pCksum);. }
3070: 0a 20 20 62 6c 6f 62 5f 72 65 73 69 7a 65 28 70 . blob_resize(p
3080: 43 6b 73 75 6d 2c 20 33 32 29 3b 0a 20 20 4d 44 Cksum, 32);. MD
3090: 35 46 69 6e 61 6c 28 7a 52 65 73 75 6c 74 2c 20 5Final(zResult,
30a0: 26 63 74 78 29 3b 0a 20 20 44 69 67 65 73 74 54 &ctx);. DigestT
30b0: 6f 42 61 73 65 31 36 28 7a 52 65 73 75 6c 74 2c oBase16(zResult,
30c0: 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 70 43 6b blob_buffer(pCk
30d0: 73 75 6d 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 sum));. return
30e0: 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 4f 4d 0;.}.../*.** COM
30f0: 4d 41 4e 44 3a 20 74 65 73 74 2d 6d 64 35 73 75 MAND: test-md5su
3100: 6d 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 m.**.** Compute
3110: 61 6e 20 4d 44 35 20 63 68 65 63 6b 73 75 6d 20 an MD5 checksum
3120: 6f 66 20 61 6c 6c 20 66 69 6c 65 73 20 6e 61 6d of all files nam
3130: 65 64 20 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e ed on the comman
3140: 64 2d 6c 69 6e 65 2e 0a 2a 2a 20 49 66 20 61 6e d-line..** If an
3150: 20 66 69 6c 65 20 69 73 20 6e 61 6d 65 64 20 22 file is named "
3160: 2d 22 20 74 68 65 6e 20 74 61 6b 65 20 69 74 73 -" then take its
3170: 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 73 74 content from st
3180: 61 6e 64 61 72 64 20 69 6e 70 75 74 2e 0a 2a 2f andard input..*/
3190: 0a 76 6f 69 64 20 6d 64 35 73 75 6d 5f 74 65 73 .void md5sum_tes
31a0: 74 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 69 t(void){. int i
31b0: 3b 0a 20 20 42 6c 6f 62 20 69 6e 3b 0a 20 20 42 ;. Blob in;. B
31c0: 6c 6f 62 20 63 6b 73 75 6d 3b 0a 20 20 0a 20 20 lob cksum;. .
31d0: 66 6f 72 28 69 3d 32 3b 20 69 3c 67 2e 61 72 67 for(i=2; i<g.arg
31e0: 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 c; i++){. if(
31f0: 20 67 2e 61 72 67 76 5b 69 5d 5b 30 5d 3d 3d 27 g.argv[i][0]=='
3200: 2d 27 20 26 26 20 67 2e 61 72 67 76 5b 69 5d 5b -' && g.argv[i][
3210: 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 62 1]==0 ){. b
3220: 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 63 68 lob_read_from_ch
3230: 61 6e 6e 65 6c 28 26 69 6e 2c 20 73 74 64 69 6e annel(&in, stdin
3240: 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 6d 64 35 , -1);. md5
3250: 73 75 6d 5f 62 6c 6f 62 28 26 69 6e 2c 20 26 63 sum_blob(&in, &c
3260: 6b 73 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 ksum);. }else
3270: 7b 0a 20 20 20 20 20 20 6d 64 35 73 75 6d 5f 66 {. md5sum_f
3280: 69 6c 65 28 67 2e 61 72 67 76 5b 69 5d 2c 20 26 ile(g.argv[i], &
3290: 63 6b 73 75 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 cksum);. }.
32a0: 20 20 70 72 69 6e 74 66 28 22 25 73 20 20 25 73 printf("%s %s
32b0: 5c 6e 22 2c 20 62 6c 6f 62 5f 73 74 72 28 26 63 \n", blob_str(&c
32c0: 6b 73 75 6d 29 2c 20 67 2e 61 72 67 76 5b 69 5d ksum), g.argv[i]
32d0: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 );. blob_rese
32e0: 74 28 26 63 6b 73 75 6d 29 3b 0a 20 20 7d 0a 7d t(&cksum);. }.}
32f0: 0a .