llmd5.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file llmd5.h
  3. *
  4. * $LicenseInfo:firstyear=2001&license=viewergpl$
  5. *
  6. * Copyright (c) 2001-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. *
  31. * llMD5.CC - source code for the C++/object oriented translation and
  32. * modification of MD5.
  33. *
  34. * Adapted to Linden Lab by Frank Filipanits, 6/25/2002
  35. * Fixed potential memory leak, James Cook, 6/27/2002
  36. *
  37. * Translation and modification (c) 1995 by Mordechai T. Abzug
  38. *
  39. * This translation/ modification is provided "as is," without express or
  40. * implied warranty of any kind.
  41. *
  42. * The translator/ modifier does not claim (1) that MD5 will do what you think
  43. * it does; (2) that this translation/ modification is accurate; or (3) that
  44. * this software is "merchantible." (Language for this disclaimer partially
  45. * copied from the disclaimer below).
  46. *
  47. * Based on:
  48. *
  49. * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm MDDRIVER.C -
  50. * test driver for MD2, MD4 and MD5
  51. *
  52. * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights
  53. * reserved.
  54. *
  55. * License to copy and use this software is granted provided that it
  56. * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  57. * Algorithm" in all material mentioning or referencing this software
  58. * or this function.
  59. *
  60. * License is also granted to make and use derivative works provided
  61. * that such works are identified as "derived from the RSA Data
  62. * Security, Inc. MD5 Message-Digest Algorithm" in all material
  63. * mentioning or referencing the derived work.
  64. *
  65. * RSA Data Security, Inc. makes no representations concerning either
  66. * the merchantability of this software or the suitability of this
  67. * software for any particular purpose. It is provided "as is"
  68. * without express or implied warranty of any kind.
  69. *
  70. * These notices must be retained in any copies of any part of this
  71. * documentation and/or software.
  72. */
  73. #ifndef LL_LLMD5_H
  74. #define LL_LLMD5_H
  75. #include "llerror.h"
  76. // Use for the raw digest output
  77. #define MD5RAW_BYTES 16
  78. // Use for hex digest output
  79. #define MD5HEX_STR_SIZE 33 // Message system fixed size with nul
  80. #define MD5HEX_STR_BYTES 32 // Message system fixed size
  81. class LLMD5
  82. {
  83. friend std::ostream& operator<<(std::ostream&, LLMD5);
  84. protected:
  85. LOG_CLASS(LLMD5);
  86. public:
  87. LLMD5();
  88. void update(const unsigned char* input, U32 input_length);
  89. void update(std::istream& stream);
  90. void update(FILE* file);
  91. void update(const std::string& str);
  92. void finalize();
  93. // Constructors for special circumstances. All these constructors finalize
  94. // the MD5 context.
  95. LLMD5(const unsigned char* string); // Digest string, finalize
  96. LLMD5(std::istream& stream); // Digest stream, finalize
  97. LLMD5(FILE* file); // Digest file, close, finalize
  98. LLMD5(const unsigned char* string, U32 number);
  99. // Methods to acquire finalized result:
  100. // Needs a 16 bytes array for binary data
  101. void raw_digest(unsigned char* array) const;
  102. // Needs a 33 bytes array for ASCII-hex string
  103. void hex_digest(char* string) const;
  104. private:
  105. void init(); // Called by all constructors
  106. // Does the real update work. Note that length is implied to be 64.
  107. void transform(const unsigned char* buffer);
  108. static void encode(unsigned char* dest, const U32* src, U32 length);
  109. static void decode(U32* dest, const unsigned char* src, U32 length);
  110. private:
  111. U32 mState[4];
  112. U32 mCount[2]; // Number of *bits*, mod 2^64
  113. unsigned char mBuffer[64]; // Input buffer
  114. unsigned char mDigest[MD5RAW_BYTES];
  115. bool mFinalized;
  116. };
  117. bool operator==(const LLMD5& a, const LLMD5& b);
  118. bool operator!=(const LLMD5& a, const LLMD5& b);
  119. #endif // LL_LLMD5_H