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.

102 lines
3.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. SHA-256 secure hash generator.
  18. Create one of these objects from a block of source data or a stream, and it
  19. calculates the SHA-256 hash of that data.
  20. You can retrieve the hash as a raw 32-byte block, or as a 64-digit hex string.
  21. @see MD5
  22. @tags{Cryptography}
  23. */
  24. class JUCE_API SHA256
  25. {
  26. public:
  27. //==============================================================================
  28. /** Creates an empty SHA256 object.
  29. The default constructor just creates a hash filled with zeros. (This is not
  30. equal to the hash of an empty block of data).
  31. */
  32. SHA256();
  33. /** Destructor. */
  34. ~SHA256();
  35. /** Creates a copy of another SHA256. */
  36. SHA256 (const SHA256&);
  37. /** Copies another SHA256. */
  38. SHA256& operator= (const SHA256&);
  39. //==============================================================================
  40. /** Creates a hash from a block of raw data. */
  41. explicit SHA256 (const MemoryBlock& data);
  42. /** Creates a hash from a block of raw data. */
  43. SHA256 (const void* data, size_t numBytes);
  44. /** Creates a hash from the contents of a stream.
  45. This will read from the stream until the stream is exhausted, or until
  46. maxBytesToRead bytes have been read. If maxBytesToRead is negative, the entire
  47. stream will be read.
  48. */
  49. SHA256 (InputStream& input, int64 maxBytesToRead = -1);
  50. /** Reads a file and generates the hash of its contents.
  51. If the file can't be opened, the hash will be left uninitialised (i.e. full
  52. of zeros).
  53. */
  54. explicit SHA256 (const File& file);
  55. /** Creates a checksum from a UTF-8 buffer.
  56. E.g.
  57. @code SHA256 checksum (myString.toUTF8());
  58. @endcode
  59. */
  60. explicit SHA256 (CharPointer_UTF8 utf8Text) noexcept;
  61. //==============================================================================
  62. /** Returns the hash as a 32-byte block of data. */
  63. MemoryBlock getRawData() const;
  64. /** Returns the checksum as a 64-digit hex string. */
  65. String toHexString() const;
  66. //==============================================================================
  67. bool operator== (const SHA256&) const noexcept;
  68. bool operator!= (const SHA256&) const noexcept;
  69. private:
  70. //==============================================================================
  71. uint8 result[32] = {};
  72. void process (const void*, size_t);
  73. JUCE_LEAK_DETECTOR (SHA256)
  74. };
  75. } // namespace juce