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.

109 lines
3.5KB

  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. SHA-256 secure hash generator.
  23. Create one of these objects from a block of source data or a stream, and it
  24. calculates the SHA-256 hash of that data.
  25. You can retrieve the hash as a raw 32-byte block, or as a 64-digit hex string.
  26. @see MD5
  27. @tags{Cryptography}
  28. */
  29. class JUCE_API SHA256
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an empty SHA256 object.
  34. The default constructor just creates a hash filled with zeros. (This is not
  35. equal to the hash of an empty block of data).
  36. */
  37. SHA256();
  38. /** Destructor. */
  39. ~SHA256();
  40. /** Creates a copy of another SHA256. */
  41. SHA256 (const SHA256&);
  42. /** Copies another SHA256. */
  43. SHA256& operator= (const SHA256&);
  44. //==============================================================================
  45. /** Creates a hash from a block of raw data. */
  46. explicit SHA256 (const MemoryBlock& data);
  47. /** Creates a hash from a block of raw data. */
  48. SHA256 (const void* data, size_t numBytes);
  49. /** Creates a hash from the contents of a stream.
  50. This will read from the stream until the stream is exhausted, or until
  51. maxBytesToRead bytes have been read. If maxBytesToRead is negative, the entire
  52. stream will be read.
  53. */
  54. SHA256 (InputStream& input, int64 maxBytesToRead = -1);
  55. /** Reads a file and generates the hash of its contents.
  56. If the file can't be opened, the hash will be left uninitialised (i.e. full
  57. of zeros).
  58. */
  59. explicit SHA256 (const File& file);
  60. /** Creates a checksum from a UTF-8 buffer.
  61. E.g.
  62. @code SHA256 checksum (myString.toUTF8());
  63. @endcode
  64. */
  65. explicit SHA256 (CharPointer_UTF8 utf8Text) noexcept;
  66. //==============================================================================
  67. /** Returns the hash as a 32-byte block of data. */
  68. MemoryBlock getRawData() const;
  69. /** Returns the checksum as a 64-digit hex string. */
  70. String toHexString() const;
  71. //==============================================================================
  72. bool operator== (const SHA256&) const noexcept;
  73. bool operator!= (const SHA256&) const noexcept;
  74. private:
  75. //==============================================================================
  76. uint8 result[32] = {};
  77. void process (const void*, size_t);
  78. JUCE_LEAK_DETECTOR (SHA256)
  79. };
  80. } // namespace juce