MD5.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  2. */
  3. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  4. rights reserved.
  5. License to copy and use this software is granted provided that it
  6. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  7. Algorithm" in all material mentioning or referencing this software
  8. or this function.
  9. License is also granted to make and use derivative works provided
  10. that such works are identified as "derived from the RSA Data
  11. Security, Inc. MD5 Message-Digest Algorithm" in all material
  12. mentioning or referencing the derived work.
  13. RSA Data Security, Inc. makes no representations concerning either
  14. the merchantability of this software or the suitability of this
  15. software for any particular purpose. It is provided "as is"
  16. without express or implied warranty of any kind.
  17. These notices must be retained in any copies of any part of this
  18. documentation and/or software.
  19. */
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "md5.h"
  24. #define MD_CTX MD5_CTX
  25. #define MDInit MD5_Init
  26. #define MDUpdate MD5_Update
  27. #define MDFinal MD5_Final
  28. /*
  29. INPUT : Test string , buffer of 16 bytes to
  30. store the digest,length of the string
  31. OUTPUT : Encrypted message in the buffer.
  32. RETURN : NONE
  33. PROCESS :Digests a string and prints the result.
  34. */
  35. extern void AuthCodeCalMD5(
  36. uint8_t *string, /*test string */
  37. uint8_t *MD5Result, /*result of MD5 digest*/
  38. uint16_t LengthOfstring /*Length of input string */
  39. )
  40. {
  41. MD_CTX context;
  42. uint8_t digest[16];
  43. MDInit (&context);
  44. MDUpdate (&context, string, LengthOfstring);
  45. MDFinal (digest, &context);
  46. memcpy(MD5Result,digest,16);
  47. }
  48. unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md)
  49. {
  50. MD5_CTX c;
  51. static unsigned char m[16];
  52. if (md == NULL) md=m;
  53. MD5_Init(&c);
  54. MD5_Update(&c,(unsigned char *)d,n);
  55. MD5_Final(md,&c);
  56. // OPENSSL_cleanse(&c,sizeof(c)); /* security consideration */
  57. return(md);
  58. }