You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __MD5_DIGEST_HEADER__
  2. #define __MD5_DIGEST_HEADER__
  3. /////////////////////////////////////////////////////////////////////////
  4. // MD5.cpp
  5. // Implementation file for MD5 class
  6. //
  7. // This C++ Class implementation of the original RSA Data Security, Inc.
  8. // MD5 Message-Digest Algorithm is copyright (c) 2002, Gary McNickle.
  9. // All rights reserved. This software is a derivative of the "RSA Data
  10. // Security, Inc. MD5 Message-Digest Algorithm"
  11. //
  12. // You may use this software free of any charge, but without any
  13. // warranty or implied warranty, provided that you follow the terms
  14. // of the original RSA copyright, listed below.
  15. //
  16. // Original RSA Data Security, Inc. Copyright notice
  17. /////////////////////////////////////////////////////////////////////////
  18. //
  19. // Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  20. // rights reserved.
  21. //
  22. // License to copy and use this software is granted provided that it
  23. // is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  24. // Algorithm" in all material mentioning or referencing this software
  25. // or this function.
  26. // License is also granted to make and use derivative works provided
  27. // that such works are identified as "derived from the RSA Data
  28. // Security, Inc. MD5 Message-Digest Algorithm" in all material
  29. // mentioning or referencing the derived work.
  30. // RSA Data Security, Inc. makes no representations concerning either
  31. // the merchantability of this software or the suitability of this
  32. // software for any particular purpose. It is provided "as is"
  33. // without express or implied warranty of any kind.
  34. // These notices must be retained in any copies of any part of this
  35. // documentation and/or software.
  36. /////////////////////////////////////////////////////////////////////////
  37. #include <cstddef>
  38. typedef unsigned int u_int4;
  39. typedef unsigned short int u_int2;
  40. typedef unsigned char u_char;
  41. char * PrintMD5(u_char md5Digest[16]);
  42. char * MD5String(char * szString);
  43. char * MD5Binary(u_char * bin, size_t len);
  44. char * MD5File(char * szFilename);
  45. class md5
  46. {
  47. // Methods
  48. public:
  49. md5()
  50. {
  51. Init();
  52. }
  53. void Init();
  54. void Update(u_char * chInput, size_t InputLen);
  55. void Finalize();
  56. u_char * Digest()
  57. {
  58. return m_Digest;
  59. }
  60. private:
  61. void Transform(u_char * block);
  62. void Encode(u_char * dest, u_int4 * src, u_int4 nLength);
  63. void Decode(u_int4 * dest, u_char * src, u_int4 nLength);
  64. inline u_int4 rotate_left(u_int4 x, u_int4 n)
  65. {
  66. return ((x << n) | (x >> (32 - n)));
  67. }
  68. inline u_int4 F(u_int4 x, u_int4 y, u_int4 z)
  69. {
  70. return ((x & y) | (~x & z));
  71. }
  72. inline u_int4 G(u_int4 x, u_int4 y, u_int4 z)
  73. {
  74. return ((x & z) | (y & ~z));
  75. }
  76. inline u_int4 H(u_int4 x, u_int4 y, u_int4 z)
  77. {
  78. return (x ^ y ^ z);
  79. }
  80. inline u_int4 I(u_int4 x, u_int4 y, u_int4 z)
  81. {
  82. return (y ^ (x | ~z));
  83. }
  84. inline void FF(u_int4 & a, u_int4 b, u_int4 c, u_int4 d, u_int4 x, u_int4 s, u_int4 ac)
  85. {
  86. a += F(b, c, d) + x + ac;
  87. a = rotate_left(a, s);
  88. a += b;
  89. }
  90. inline void GG(u_int4 & a, u_int4 b, u_int4 c, u_int4 d, u_int4 x, u_int4 s, u_int4 ac)
  91. {
  92. a += G(b, c, d) + x + ac;
  93. a = rotate_left(a, s);
  94. a += b;
  95. }
  96. inline void HH(u_int4 & a, u_int4 b, u_int4 c, u_int4 d, u_int4 x, u_int4 s, u_int4 ac)
  97. {
  98. a += H(b, c, d) + x + ac;
  99. a = rotate_left(a, s);
  100. a += b;
  101. }
  102. inline void II(u_int4 & a, u_int4 b, u_int4 c, u_int4 d, u_int4 x, u_int4 s, u_int4 ac)
  103. {
  104. a += I(b, c, d) + x + ac;
  105. a = rotate_left(a, s);
  106. a += b;
  107. }
  108. // Data
  109. private:
  110. u_int4 m_State[4];
  111. u_int4 m_Count[2];
  112. u_char m_Buffer[64];
  113. u_char m_Digest[16];
  114. u_char m_Finalized;
  115. };
  116. #endif