md5class.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // md5class.cpp: implementation of the CMD5 class.
  2. //See internet RFC 1321, "The MD5 Message-Digest Algorithm"
  3. //
  4. //Use this code as you see fit. It is provided "as is"
  5. //without express or implied warranty of any kind.
  6. //////////////////////////////////////////////////////////////////////
  7. #include "stdafx.h"
  8. #include "md5class.h"
  9. #include "md5.h" //declarations from RFC 1321
  10. #include <string.h>
  11. #include <stdio.h>
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CMD5::CMD5()
  16. {
  17. m_digestValid = false; //we don't have a plaintext string yet
  18. m_digestString[32]=0; //the digest string is always 32 characters, so put a null in position 32
  19. }
  20. CMD5::~CMD5()
  21. {
  22. }
  23. CMD5::CMD5(const char* plainText)
  24. {
  25. m_plainText = const_cast<char*>(plainText); //get a pointer to the plain text. If casting away the const-ness worries you,
  26. //you could make a local copy of the plain text string.
  27. m_digestString[32]=0;
  28. m_digestValid = calcDigest();
  29. }
  30. //////////////////////////////////////////////////////////////////////
  31. // Implementation
  32. //////////////////////////////////////////////////////////////////////
  33. void CMD5::setPlainText(const char* plainText)
  34. {
  35. //set plaintext with a mutator, it's ok to
  36. //to call this multiple times. If casting away the const-ness of plainText
  37. //worries you, you could either make a local copy of the plain
  38. //text string instead of just pointing at the user's string, or
  39. //modify the RFC 1321 code to take 'const' plaintext, see example below.
  40. m_plainText = const_cast<char*>(plainText);
  41. m_digestValid = calcDigest();
  42. }
  43. /* Use a function of this type with your favorite string class
  44. if casting away the const-ness of the user's text buffer violates you
  45. coding standards.
  46. void CMD5::setPlainText(CString& strPlainText)
  47. {
  48. static CString plaintext(strPlainText);
  49. m_plainText = strPlainText.GetBuffer();
  50. m_digestValid = calcDigest();
  51. }
  52. */
  53. const char* CMD5::getMD5Digest()
  54. { //access message digest (aka hash), return 0 if plaintext has not been set
  55. if(m_digestValid)
  56. {
  57. return m_digestString;
  58. } else return 0;
  59. }
  60. bool CMD5::calcDigest()
  61. {
  62. //See RFC 1321 for details on how MD5Init, MD5Update, and MD5Final
  63. //calculate a digest for the plain text
  64. MD5_CTX context;
  65. MD5Init(&context);
  66. //the alternative to these ugly casts is to go into the RFC code and change the declarations
  67. MD5Update(&context, reinterpret_cast<unsigned char *>(m_plainText), ::strlen(m_plainText));
  68. MD5Final(reinterpret_cast <unsigned char *>(m_digest),&context);
  69. //make a string version of the numeric digest value
  70. int p=0;
  71. for (int i = 0; i<16; i++)
  72. {
  73. ::sprintf(&m_digestString[p],"%02x", m_digest[i]);
  74. p+=2;
  75. }
  76. return true;
  77. }