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.

108 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_SHA256_H_INCLUDED
  18. #define JUCE_SHA256_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. SHA-256 secure hash generator.
  22. Create one of these objects from a block of source data or a stream, and it
  23. calculates the SHA-256 hash of that data.
  24. You can retrieve the hash as a raw 32-byte block, or as a 64-digit hex string.
  25. @see MD5
  26. */
  27. class JUCE_API SHA256
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an empty SHA256 object.
  32. The default constructor just creates a hash filled with zeros. (This is not
  33. equal to the hash of an empty block of data).
  34. */
  35. SHA256() noexcept;
  36. /** Destructor. */
  37. ~SHA256() noexcept;
  38. /** Creates a copy of another SHA256. */
  39. SHA256 (const SHA256& other) noexcept;
  40. /** Copies another SHA256. */
  41. SHA256& operator= (const SHA256& other) noexcept;
  42. //==============================================================================
  43. /** Creates a hash from a block of raw data. */
  44. explicit SHA256 (const MemoryBlock& data);
  45. /** Creates a hash from a block of raw data. */
  46. SHA256 (const void* data, size_t numBytes);
  47. /** Creates a hash from the contents of a stream.
  48. This will read from the stream until the stream is exhausted, or until
  49. maxBytesToRead bytes have been read. If maxBytesToRead is negative, the entire
  50. stream will be read.
  51. */
  52. SHA256 (InputStream& input, int64 maxBytesToRead = -1);
  53. /** Reads a file and generates the hash of its contents.
  54. If the file can't be opened, the hash will be left uninitialised (i.e. full
  55. of zeros).
  56. */
  57. explicit SHA256 (const File& file);
  58. /** Creates a checksum from a UTF-8 buffer.
  59. E.g.
  60. @code SHA256 checksum (myString.toUTF8());
  61. @endcode
  62. */
  63. explicit SHA256 (CharPointer_UTF8 utf8Text) noexcept;
  64. //==============================================================================
  65. /** Returns the hash as a 32-byte block of data. */
  66. MemoryBlock getRawData() const;
  67. /** Returns the checksum as a 64-digit hex string. */
  68. String toHexString() const;
  69. //==============================================================================
  70. bool operator== (const SHA256&) const noexcept;
  71. bool operator!= (const SHA256&) const noexcept;
  72. private:
  73. //==============================================================================
  74. uint8 result [32];
  75. void process (const void*, size_t);
  76. JUCE_LEAK_DETECTOR (SHA256)
  77. };
  78. #endif // JUCE_SHA256_H_INCLUDED