md5.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* md5.h - Declaration of functions and data types used for MD5 sum
  2. computing library functions.
  3. Copyright 1995, 1996, 2000 Free Software Foundation, Inc.
  4. NOTE: The canonical source of this file is maintained with the GNU C
  5. Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10. This program 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
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  17. #ifndef _MD5_H
  18. #define _MD5_H 1
  19. #include <stdio.h>
  20. #if defined HAVE_LIMITS_H || _LIBC
  21. # include <limits.h>
  22. #endif
  23. #include "ansidecl.h"
  24. /* The following contortions are an attempt to use the C preprocessor
  25. to determine an unsigned integral type that is 32 bits wide. An
  26. alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
  27. doing that would require that the configure script compile and *run*
  28. the resulting executable. Locally running cross-compiled executables
  29. is usually not possible. */
  30. #ifdef _LIBC
  31. # include <sys/types.h>
  32. typedef u_int32_t md5_uint32;
  33. typedef uintptr_t md5_uintptr;
  34. #elif defined (HAVE_SYS_TYPES_H) && defined (HAVE_STDINT_H)
  35. #include <stdint.h>
  36. #include <sys/types.h>
  37. typedef uint32_t md5_uint32;
  38. typedef uintptr_t md5_uintptr;
  39. #else
  40. # define INT_MAX_32_BITS 2147483647
  41. /* If UINT_MAX isn't defined, assume it's a 32-bit type.
  42. This should be valid for all systems GNU cares about because
  43. that doesn't include 16-bit systems, and only modern systems
  44. (that certainly have <limits.h>) have 64+-bit integral types. */
  45. # ifndef INT_MAX
  46. # define INT_MAX INT_MAX_32_BITS
  47. # endif
  48. # if INT_MAX == INT_MAX_32_BITS
  49. typedef unsigned int md5_uint32;
  50. # else
  51. # if SHRT_MAX == INT_MAX_32_BITS
  52. typedef unsigned short md5_uint32;
  53. # else
  54. # if LONG_MAX == INT_MAX_32_BITS
  55. typedef unsigned long md5_uint32;
  56. # else
  57. /* The following line is intended to evoke an error.
  58. Using #error is not portable enough. */
  59. "Cannot determine unsigned 32-bit data type."
  60. # endif
  61. # endif
  62. # endif
  63. /* We have to make a guess about the integer type equivalent in size
  64. to pointers which should always be correct. */
  65. typedef unsigned long int md5_uintptr;
  66. #endif
  67. #ifdef __cplusplus
  68. extern "C" {
  69. #endif
  70. /* Structure to save state of computation between the single steps. */
  71. struct md5_ctx
  72. {
  73. md5_uint32 A;
  74. md5_uint32 B;
  75. md5_uint32 C;
  76. md5_uint32 D;
  77. md5_uint32 total[2];
  78. md5_uint32 buflen;
  79. char buffer[128] ATTRIBUTE_ALIGNED_ALIGNOF(md5_uint32);
  80. };
  81. /*
  82. * The following three functions are build up the low level used in
  83. * the functions `md5_stream' and `md5_buffer'.
  84. */
  85. /* Initialize structure containing state of computation.
  86. (RFC 1321, 3.3: Step 3) */
  87. extern void md5_init_ctx (struct md5_ctx *ctx);
  88. /* Starting with the result of former calls of this function (or the
  89. initialization function update the context for the next LEN bytes
  90. starting at BUFFER.
  91. It is necessary that LEN is a multiple of 64!!! */
  92. extern void md5_process_block (const void *buffer, size_t len,
  93. struct md5_ctx *ctx);
  94. /* Starting with the result of former calls of this function (or the
  95. initialization function update the context for the next LEN bytes
  96. starting at BUFFER.
  97. It is NOT required that LEN is a multiple of 64. */
  98. extern void md5_process_bytes (const void *buffer, size_t len,
  99. struct md5_ctx *ctx);
  100. /* Process the remaining bytes in the buffer and put result from CTX
  101. in first 16 bytes following RESBUF. The result is always in little
  102. endian byte order, so that a byte-wise output yields to the wanted
  103. ASCII representation of the message digest.
  104. IMPORTANT: On some systems it is required that RESBUF is correctly
  105. aligned for a 32 bits value. */
  106. extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
  107. /* Put result from CTX in first 16 bytes following RESBUF. The result is
  108. always in little endian byte order, so that a byte-wise output yields
  109. to the wanted ASCII representation of the message digest.
  110. IMPORTANT: On some systems it is required that RESBUF is correctly
  111. aligned for a 32 bits value. */
  112. extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
  113. /* Compute MD5 message digest for bytes read from STREAM. The
  114. resulting message digest number will be written into the 16 bytes
  115. beginning at RESBLOCK. */
  116. extern int md5_stream (FILE *stream, void *resblock);
  117. /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
  118. result is always in little endian byte order, so that a byte-wise
  119. output yields to the wanted ASCII representation of the message
  120. digest. */
  121. extern void *md5_buffer (const char *buffer, size_t len, void *resblock);
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif