Advertisement
FlyFar

md5.c

May 16th, 2024
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | Cybersecurity | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5.  
  6. #include <unistd.h>
  7. #include <sys/wait.h>
  8. #include <sys/types.h>
  9.  
  10. #include "pop3.h"
  11. #include "md5/md5.h"
  12.  
  13. /* added by Glynn Clements <glynn@sensei.co.uk> 1997-07-08 */
  14.  
  15. /*
  16.   modified by Glynn Clements <glynn@sensei.co.uk> 1997-07-11
  17.   to use GNU md5 routines
  18. */
  19.  
  20. /**************************************************************************/
  21.  
  22. static struct md5_ctx ctx;
  23. static const char hex[] = "0123456789abcdef";
  24.  
  25. /**************************************************************************/
  26.  
  27. static void
  28. md5_hex(unsigned char *src, char *dst)
  29. {
  30.     unsigned i, c;
  31.  
  32.     for (i = 0; i < 16; i++)
  33.     {
  34.         c = src[i];
  35.         dst[i * 2 + 0] = hex[c >> 4];
  36.         dst[i * 2 + 1] = hex[c & 0x0F];
  37.     }
  38.  
  39.     dst[32] = '\0';
  40. }
  41.  
  42. /**************************************************************************/
  43.  
  44. void
  45. do_md5_file(FILE *src, long start, long bytes, char *hash)
  46. {
  47.     char buff[1024];
  48.     unsigned char result[16];
  49.     int left, n;
  50.  
  51.     md5_init_ctx(&ctx);
  52.  
  53.     fseek(src, start, SEEK_SET);
  54.  
  55.     while (bytes > 0)
  56.     {
  57.         n = fread(buff, 1, min(bytes, sizeof(buff)), src);
  58.         if (n <= 0)
  59.             break;
  60.         md5_process_bytes(buff, n, &ctx);
  61.         bytes -= n;
  62.     }
  63.  
  64.     md5_finish_ctx(&ctx, result);
  65.     md5_hex(result, hash);
  66. }
  67.  
  68. void
  69. do_md5_string(char *pass, int passlen, char *hash)
  70. {
  71.     unsigned char result[16];
  72.  
  73.     md5_init_ctx(&ctx);
  74.     md5_process_bytes(pass, passlen, &ctx);
  75.     md5_finish_ctx(&ctx, result);
  76.     md5_hex(result, hash);
  77. }
  78.  
  79. /**************************************************************************/
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement