MD5.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //MD5.cpp
  2. /* MD5
  3. converted to C++ class by Frank Thilo (thilo@unix-ag.org)
  4. for bzflag (http://www.bzflag.org)
  5. based on:
  6. md5.h and md5.c
  7. reference implemantion of RFC 1321
  8. Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  9. rights reserved.
  10. License to copy and use this software is granted provided that it
  11. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  12. Algorithm" in all material mentioning or referencing this software
  13. or this function.
  14. License is also granted to make and use derivative works provided
  15. that such works are identified as "derived from the RSA Data
  16. Security, Inc. MD5 Message-Digest Algorithm" in all material
  17. mentioning or referencing the derived work.
  18. RSA Data Security, Inc. makes no representations concerning either
  19. the merchantability of this software or the suitability of this
  20. software for any particular purpose. It is provided "as is"
  21. without express or implied warranty of any kind.
  22. These notices must be retained in any copies of any part of this
  23. documentation and/or software.
  24. */
  25. #ifndef SRC_UTIL_MD5_H_
  26. #define SRC_UTIL_MD5_H_
  27. #include <string>
  28. #include <iostream>
  29. using namespace std;
  30. namespace toolkit {
  31. // a small class for calculating MD5 hashes of strings or byte arrays
  32. // it is not meant to be fast or secure
  33. //
  34. // usage: 1) feed it blocks of uchars with update()
  35. // 2) finalize()
  36. // 3) get hexdigest() string
  37. // or
  38. // MD5(std::string).hexdigest()
  39. //
  40. // assumes that char is 8 bit and int is 32 bit
  41. class MD5
  42. {
  43. public:
  44. typedef unsigned int size_type; // must be 32bit
  45. MD5();
  46. MD5(const std::string& text);
  47. void update(const unsigned char *buf, size_type length);
  48. void update(const char *buf, size_type length);
  49. MD5& finalize();
  50. std::string hexdigest() const;
  51. std::string rawdigest() const;
  52. friend std::ostream& operator<<(std::ostream&, MD5 md5);
  53. private:
  54. void init();
  55. typedef uint8_t uint1; // 8bit
  56. typedef uint32_t uint4; // 32bit
  57. enum {blocksize = 64}; // VC6 won't eat a const static int here
  58. void transform(const uint1 block[blocksize]);
  59. static void decode(uint4 output[], const uint1 input[], size_type len);
  60. static void encode(uint1 output[], const uint4 input[], size_type len);
  61. bool finalized;
  62. uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
  63. uint4 count[2]; // 64bit counter for number of bits (lo, hi)
  64. uint4 state[4]; // digest so far
  65. uint1 digest[16]; // the result
  66. // low level logic operations
  67. static inline uint4 F(uint4 x, uint4 y, uint4 z);
  68. static inline uint4 G(uint4 x, uint4 y, uint4 z);
  69. static inline uint4 H(uint4 x, uint4 y, uint4 z);
  70. static inline uint4 I(uint4 x, uint4 y, uint4 z);
  71. static inline uint4 rotate_left(uint4 x, int n);
  72. static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  73. static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  74. static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  75. static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  76. };
  77. } /* namespace toolkit */
  78. #endif /* SRC_UTIL_MD5_H_ */