The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

120 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. MD5 checksum class.
  23. Create one of these with a block of source data or a stream, and it calculates
  24. the MD5 checksum of that data.
  25. You can then retrieve this checksum as a 16-byte block, or as a hex string.
  26. @see SHA256
  27. @tags{Cryptography}
  28. */
  29. class JUCE_API MD5
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a null MD5 object. */
  34. MD5();
  35. /** Creates a copy of another MD5. */
  36. MD5 (const MD5&);
  37. /** Copies another MD5. */
  38. MD5& operator= (const MD5&);
  39. //==============================================================================
  40. /** Creates a checksum for a block of binary data. */
  41. explicit MD5 (const MemoryBlock&) noexcept;
  42. /** Creates a checksum for a block of binary data. */
  43. MD5 (const void* data, size_t numBytes) noexcept;
  44. /** Creates a checksum for the input from a stream.
  45. This will read up to the given number of bytes from the stream, and produce the
  46. checksum of that. If the number of bytes to read is negative, it'll read
  47. until the stream is exhausted.
  48. */
  49. MD5 (InputStream& input, int64 numBytesToRead = -1);
  50. /** Creates a checksum for the contents of a file. */
  51. explicit MD5 (const File&);
  52. /** Creates a checksum of the characters in a UTF-8 buffer.
  53. E.g.
  54. @code MD5 checksum (myString.toUTF8());
  55. @endcode
  56. */
  57. explicit MD5 (CharPointer_UTF8 utf8Text) noexcept;
  58. /** Destructor. */
  59. ~MD5();
  60. //==============================================================================
  61. /** Returns the checksum as a 16-byte block of data. */
  62. MemoryBlock getRawChecksumData() const;
  63. /** Returns a pointer to the 16-byte array of result data. */
  64. const uint8* getChecksumDataArray() const noexcept { return result; }
  65. /** Returns the checksum as a 32-digit hex string. */
  66. String toHexString() const;
  67. /** Creates an MD5 from a little-endian UTF-32 encoded string.
  68. Note that this method is provided for backwards-compatibility with the old
  69. version of this class, which had a constructor that took a string and performed
  70. this operation on it. In new code, you shouldn't use this, and are recommended to
  71. use the constructor that takes a CharPointer_UTF8 instead.
  72. */
  73. static MD5 fromUTF32 (StringRef);
  74. //==============================================================================
  75. bool operator== (const MD5&) const noexcept;
  76. bool operator!= (const MD5&) const noexcept;
  77. private:
  78. //==============================================================================
  79. uint8 result[16] = {};
  80. void processStream (InputStream&, int64);
  81. // This private constructor is declared here to prevent you accidentally passing a
  82. // String and having it unexpectedly call the constructor that takes a File.
  83. explicit MD5 (const String&) = delete;
  84. JUCE_LEAK_DETECTOR (MD5)
  85. };
  86. } // namespace juce