md5.h 5.3 KB

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