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.

100 lines
3.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. typedef unsigned int u_int4;
  38. typedef unsigned short int u_int2;
  39. typedef unsigned char u_char;
  40. char* PrintMD5(u_char md5Digest[16]);
  41. char* MD5String(char* szString);
  42. char* MD5File(char* szFilename);
  43. class md5{
  44. // Methods
  45. public:
  46. md5() { Init(); }
  47. void Init();
  48. void Update(u_char* chInput, u_int4 nInputLen);
  49. void Finalize();
  50. u_char* Digest() { return m_Digest; }
  51. private:
  52. void Transform(u_char* block);
  53. void Encode(u_char* dest, u_int4* src, u_int4 nLength);
  54. void Decode(u_int4* dest, u_char* src, u_int4 nLength);
  55. inline u_int4 rotate_left(u_int4 x, u_int4 n)
  56. { return ((x << n) | (x >> (32-n))); }
  57. inline u_int4 F(u_int4 x, u_int4 y, u_int4 z)
  58. { return ((x & y) | (~x & z)); }
  59. inline u_int4 G(u_int4 x, u_int4 y, u_int4 z)
  60. { return ((x & z) | (y & ~z)); }
  61. inline u_int4 H(u_int4 x, u_int4 y, u_int4 z)
  62. { return (x ^ y ^ z); }
  63. inline u_int4 I(u_int4 x, u_int4 y, u_int4 z)
  64. { return (y ^ (x | ~z)); }
  65. inline void FF(u_int4& a, u_int4 b, u_int4 c, u_int4 d, u_int4 x, u_int4 s, u_int4 ac)
  66. { a += F(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
  67. inline void GG(u_int4& a, u_int4 b, u_int4 c, u_int4 d, u_int4 x, u_int4 s, u_int4 ac)
  68. { a += G(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
  69. inline void HH(u_int4& a, u_int4 b, u_int4 c, u_int4 d, u_int4 x, u_int4 s, u_int4 ac)
  70. { a += H(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
  71. inline void II(u_int4& a, u_int4 b, u_int4 c, u_int4 d, u_int4 x, u_int4 s, u_int4 ac)
  72. { a += I(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
  73. // Data
  74. private:
  75. u_int4 m_State[4];
  76. u_int4 m_Count[2];
  77. u_char m_Buffer[64];
  78. u_char m_Digest[16];
  79. u_char m_Finalized;
  80. };
  81. #endif