md5.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* MD5
  2. converted to C++ class by Frank Thilo (thilo@unix-ag.org)
  3. for bzflag (http://www.bzflag.org)
  4. based on:
  5. md5.h and md5.c
  6. reference implementation of RFC 1321
  7. Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  8. rights reserved.
  9. License to copy and use this software is granted provided that it
  10. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  11. Algorithm" in all material mentioning or referencing this software
  12. or this function.
  13. License is also granted to make and use derivative works provided
  14. that such works are identified as "derived from the RSA Data
  15. Security, Inc. MD5 Message-Digest Algorithm" in all material
  16. mentioning or referencing the derived work.
  17. RSA Data Security, Inc. makes no representations concerning either
  18. the merchantability of this software or the suitability of this
  19. software for any particular purpose. It is provided "as is"
  20. without express or implied warranty of any kind.
  21. These notices must be retained in any copies of any part of this
  22. documentation and/or software.
  23. */
  24. #ifndef BZF_MD5_H
  25. #define BZF_MD5_H
  26. #include <cstring>
  27. #include <iostream>
  28. #include "../fastrtps_dll.h"
  29. /**
  30. * Class MD5, 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. * @ingroup UTILITIES_MODULE
  41. */
  42. class RTPS_DllAPI MD5
  43. {
  44. public:
  45. typedef unsigned char uint1; // 8bit
  46. typedef unsigned int size_type; // must be 32bit
  47. MD5();
  48. MD5(const std::string& text);
  49. void update(const unsigned char *buf, size_type length);
  50. void update(const char *buf, size_type length);
  51. MD5& finalize();
  52. std::string hexdigest() const;
  53. friend std::ostream& operator<<(std::ostream&, MD5& md5);
  54. uint1 digest[16]; // the result
  55. void init();
  56. private:
  57. typedef unsigned int uint4; // 32bit
  58. enum {blocksize = 64}; // VC6 won't eat a const static int here
  59. void transform(const uint1 block[blocksize]);
  60. static void decode(uint4 output[], const uint1 input[], size_type len);
  61. static void encode(uint1 output[], const uint4 input[], size_type len);
  62. bool finalized;
  63. uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
  64. uint4 count[2]; // 64bit counter for number of bits (lo, hi)
  65. uint4 state[4]; // digest so far
  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. std::string md5(const std::string str);
  78. #endif