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.

106 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  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. */
  28. class JUCE_API SHA256
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an empty SHA256 object.
  33. The default constructor just creates a hash filled with zeros. (This is not
  34. equal to the hash of an empty block of data).
  35. */
  36. SHA256() noexcept;
  37. /** Destructor. */
  38. ~SHA256() noexcept;
  39. /** Creates a copy of another SHA256. */
  40. SHA256 (const SHA256& other) noexcept;
  41. /** Copies another SHA256. */
  42. SHA256& operator= (const SHA256& other) noexcept;
  43. //==============================================================================
  44. /** Creates a hash from a block of raw data. */
  45. explicit SHA256 (const MemoryBlock& data);
  46. /** Creates a hash from a block of raw data. */
  47. SHA256 (const void* data, size_t numBytes);
  48. /** Creates a hash from the contents of a stream.
  49. This will read from the stream until the stream is exhausted, or until
  50. maxBytesToRead bytes have been read. If maxBytesToRead is negative, the entire
  51. stream will be read.
  52. */
  53. SHA256 (InputStream& input, int64 maxBytesToRead = -1);
  54. /** Reads a file and generates the hash of its contents.
  55. If the file can't be opened, the hash will be left uninitialised (i.e. full
  56. of zeros).
  57. */
  58. explicit SHA256 (const File& file);
  59. /** Creates a checksum from a UTF-8 buffer.
  60. E.g.
  61. @code SHA256 checksum (myString.toUTF8());
  62. @endcode
  63. */
  64. explicit SHA256 (CharPointer_UTF8 utf8Text) noexcept;
  65. //==============================================================================
  66. /** Returns the hash as a 32-byte block of data. */
  67. MemoryBlock getRawData() const;
  68. /** Returns the checksum as a 64-digit hex string. */
  69. String toHexString() const;
  70. //==============================================================================
  71. bool operator== (const SHA256&) const noexcept;
  72. bool operator!= (const SHA256&) const noexcept;
  73. private:
  74. //==============================================================================
  75. uint8 result [32];
  76. void process (const void*, size_t);
  77. JUCE_LEAK_DETECTOR (SHA256)
  78. };