md5.h
author Ryan C. Gordon <icculus@icculus.org>
Fri, 28 Mar 2008 06:57:04 +0000
changeset 109 9fae2fac7b94
parent 0 31eee864ed5d
permissions -rw-r--r--
Updated copyright info.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
  Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
  This software is provided 'as-is', without any express or implied
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
  arising from the use of this software.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
  freely, subject to the following restrictions:
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
  1. The origin of this software must not be misrepresented; you must not
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
     claim that you wrote the original software. If you use this software
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
     in a product, an acknowledgment in the product documentation would be
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
     appreciated but is not required.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
  2. Altered source versions must be plainly marked as such, and must not be
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
     misrepresented as being the original software.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
  3. This notice may not be removed or altered from any source distribution.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
  L. Peter Deutsch
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
  ghost@@aladdin.com
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
/*$Id: md5.h,v 1.1 2004/01/05 18:54:19 icculus Exp $ */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
/*
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
  Independent implementation of MD5 (RFC 1321).
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
  This code implements the MD5 Algorithm defined in RFC 1321.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
  It is derived directly from the text of the RFC and not from the
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
  reference implementation.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
  The original and principal author of md5.h is L. Peter Deutsch
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
  <ghost@@aladdin.com>.  Other authors are noted in the change history
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
  that follows (in reverse chronological order):
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
  1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
  1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
	added conditionalization for C++ compilation from Martin
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
	Purschke <purschke@@bnl.gov>.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
  1999-05-03 lpd Original version.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
 */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#ifndef md5_INCLUDED
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#  define md5_INCLUDED
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#if ((PLATFORM_WIN32) || (defined WIN32))
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#define ARCH_IS_BIG_ENDIAN 0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
#elif (defined PLATFORM_UNIX)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
/* thanks to SDL headers for this. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
#if  defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    (defined(__alpha__) || defined(__alpha)) || \
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
     defined(__arm__) || \
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    (defined(__mips__) && defined(__MIPSEL__)) || \
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
     defined(__LITTLE_ENDIAN__)
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
  #define ARCH_IS_BIG_ENDIAN 0
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
#else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
  #define ARCH_IS_BIG_ENDIAN 1
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
#else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
#error Please add your platform.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
/*
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
 * This code has some adaptations for the Ghostscript environment, but it
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
 * will compile and run correctly in any environment with 8-bit chars and
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
 * 32-bit ints.  Specifically, it assumes that if the following are
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
 * defined, they have the same meaning as in Ghostscript: P1, P2, P3,
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
 * ARCH_IS_BIG_ENDIAN.
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
 */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
typedef unsigned char md5_byte_t; /* 8-bit byte */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
typedef unsigned int md5_word_t; /* 32-bit word */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
/* Define the state of the MD5 Algorithm. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
typedef struct md5_state_s {
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    md5_word_t count[2];	/* message length in bits, lsw first */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    md5_word_t abcd[4];		/* digest buffer */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    md5_byte_t buf[64];		/* accumulate block */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
} md5_state_t;
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
#ifdef __cplusplus
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
extern "C" 
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
{
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
/* Initialize the algorithm. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
#ifdef P1
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
void md5_init(P1(md5_state_t *pms));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
#else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
void md5_init(md5_state_t *pms);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
/* Append a string to the message. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
#ifdef P3
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
void md5_append(P3(md5_state_t *pms, const md5_byte_t *data, int nbytes));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
#else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
/* Finish the message and return the digest. */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
#ifdef P2
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
void md5_finish(P2(md5_state_t *pms, md5_byte_t digest[16]));
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
#else
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
#ifdef __cplusplus
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
}  /* end extern "C" */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
#endif
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
#endif /* md5_INCLUDED */
31eee864ed5d Initial revision
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115