md5.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* MD5
  2. Slightly modified version for the purposes of Banshee Engine by Marko Pintera.
  3. converted to C++ class by Frank Thilo ([email protected])
  4. for bzflag (http://www.bzflag.org)
  5. based on:
  6. md5.h and md5.c
  7. reference implementation 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 BZF_MD5_H
  26. #define BZF_MD5_H
  27. #define _CRT_SECURE_NO_WARNINGS
  28. #include <cstring>
  29. #include <iostream>
  30. // a small class for calculating MD5 hashes of strings or byte arrays
  31. // it is not meant to be fast or secure
  32. //
  33. // usage: 1) feed it blocks of uchars with update()
  34. // 2) finalize()
  35. // 3) get hexdigest() string
  36. // or
  37. // MD5(std::string).hexdigest()
  38. //
  39. // assumes that char is 8 bit and int is 32 bit
  40. class MD5
  41. {
  42. public:
  43. typedef unsigned int size_type; // must be 32bit
  44. MD5();
  45. MD5(const std::string& text);
  46. void update(const unsigned char *buf, size_type length);
  47. void update(const char *buf, size_type length);
  48. MD5& finalize();
  49. std::string hexdigest() const;
  50. void decdigest(unsigned char* buf, size_type length);
  51. private:
  52. void init();
  53. typedef unsigned char uint1; // 8bit
  54. typedef unsigned int uint4; // 32bit
  55. enum { blocksize = 64 }; // VC6 won't eat a const static int here
  56. void transform(const uint1 block[blocksize]);
  57. static void decode(uint4 output[], const uint1 input[], size_type len);
  58. static void encode(uint1 output[], const uint4 input[], size_type len);
  59. bool finalized;
  60. uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
  61. uint4 count[2]; // 64bit counter for number of bits (lo, hi)
  62. uint4 state[4]; // digest so far
  63. uint1 digest[16]; // the result
  64. // low level logic operations
  65. static inline uint4 F(uint4 x, uint4 y, uint4 z);
  66. static inline uint4 G(uint4 x, uint4 y, uint4 z);
  67. static inline uint4 H(uint4 x, uint4 y, uint4 z);
  68. static inline uint4 I(uint4 x, uint4 y, uint4 z);
  69. static inline uint4 rotate_left(uint4 x, int n);
  70. static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  71. static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  72. static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  73. static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
  74. };
  75. std::string md5(const std::string str);
  76. #endif