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.

283 lines
9.3KB

  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. namespace juce
  20. {
  21. class SHA256Processor
  22. {
  23. public:
  24. SHA256Processor() noexcept
  25. : length (0)
  26. {
  27. state[0] = 0x6a09e667;
  28. state[1] = 0xbb67ae85;
  29. state[2] = 0x3c6ef372;
  30. state[3] = 0xa54ff53a;
  31. state[4] = 0x510e527f;
  32. state[5] = 0x9b05688c;
  33. state[6] = 0x1f83d9ab;
  34. state[7] = 0x5be0cd19;
  35. }
  36. // expects 64 bytes of data
  37. void processFullBlock (const void* const data) noexcept
  38. {
  39. const uint32 constants[] =
  40. {
  41. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  42. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  43. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  44. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  45. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  46. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  47. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  48. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  49. };
  50. uint32 block[16], s[8];
  51. memcpy (s, state, sizeof (s));
  52. for (int i = 0; i < 16; ++i)
  53. block[i] = ByteOrder::bigEndianInt (addBytesToPointer (data, i * 4));
  54. for (uint32 j = 0; j < 64; j += 16)
  55. {
  56. #define JUCE_SHA256(i) \
  57. s[(7 - i) & 7] += S1 (s[(4 - i) & 7]) + ch (s[(4 - i) & 7], s[(5 - i) & 7], s[(6 - i) & 7]) + constants[i + j] \
  58. + (j != 0 ? (block[i & 15] += s1 (block[(i - 2) & 15]) + block[(i - 7) & 15] + s0 (block[(i - 15) & 15])) \
  59. : block[i]); \
  60. s[(3 - i) & 7] += s[(7 - i) & 7]; \
  61. s[(7 - i) & 7] += S0 (s[(0 - i) & 7]) + maj (s[(0 - i) & 7], s[(1 - i) & 7], s[(2 - i) & 7])
  62. JUCE_SHA256(0); JUCE_SHA256(1); JUCE_SHA256(2); JUCE_SHA256(3); JUCE_SHA256(4); JUCE_SHA256(5); JUCE_SHA256(6); JUCE_SHA256(7);
  63. JUCE_SHA256(8); JUCE_SHA256(9); JUCE_SHA256(10); JUCE_SHA256(11); JUCE_SHA256(12); JUCE_SHA256(13); JUCE_SHA256(14); JUCE_SHA256(15);
  64. #undef JUCE_SHA256
  65. }
  66. for (int i = 0; i < 8; ++i)
  67. state[i] += s[i];
  68. length += 64;
  69. }
  70. void processFinalBlock (const void* const data, unsigned int numBytes) noexcept
  71. {
  72. jassert (numBytes < 64);
  73. length += numBytes;
  74. length *= 8; // (the length is stored as a count of bits, not bytes)
  75. uint8 finalBlocks[128];
  76. memcpy (finalBlocks, data, numBytes);
  77. finalBlocks [numBytes++] = 128; // append a '1' bit
  78. while (numBytes != 56 && numBytes < 64 + 56)
  79. finalBlocks [numBytes++] = 0; // pad with zeros..
  80. for (int i = 8; --i >= 0;)
  81. finalBlocks [numBytes++] = (uint8) (length >> (i * 8)); // append the length.
  82. jassert (numBytes == 64 || numBytes == 128);
  83. processFullBlock (finalBlocks);
  84. if (numBytes > 64)
  85. processFullBlock (finalBlocks + 64);
  86. }
  87. void copyResult (uint8* result) const noexcept
  88. {
  89. for (int i = 0; i < 8; ++i)
  90. {
  91. *result++ = (uint8) (state[i] >> 24);
  92. *result++ = (uint8) (state[i] >> 16);
  93. *result++ = (uint8) (state[i] >> 8);
  94. *result++ = (uint8) state[i];
  95. }
  96. }
  97. void processStream (InputStream& input, int64 numBytesToRead, uint8* const result)
  98. {
  99. if (numBytesToRead < 0)
  100. numBytesToRead = std::numeric_limits<int64>::max();
  101. for (;;)
  102. {
  103. uint8 buffer [64];
  104. const int bytesRead = input.read (buffer, (int) jmin (numBytesToRead, (int64) sizeof (buffer)));
  105. if (bytesRead < (int) sizeof (buffer))
  106. {
  107. processFinalBlock (buffer, (unsigned int) bytesRead);
  108. break;
  109. }
  110. numBytesToRead -= sizeof (buffer);
  111. processFullBlock (buffer);
  112. }
  113. copyResult (result);
  114. }
  115. private:
  116. uint32 state[8];
  117. uint64 length;
  118. static inline uint32 rotate (const uint32 x, const uint32 y) noexcept { return (x >> y) | (x << (32 - y)); }
  119. static inline uint32 ch (const uint32 x, const uint32 y, const uint32 z) noexcept { return z ^ ((y ^ z) & x); }
  120. static inline uint32 maj (const uint32 x, const uint32 y, const uint32 z) noexcept { return y ^ ((y ^ z) & (x ^ y)); }
  121. static inline uint32 s0 (const uint32 x) noexcept { return rotate (x, 7) ^ rotate (x, 18) ^ (x >> 3); }
  122. static inline uint32 s1 (const uint32 x) noexcept { return rotate (x, 17) ^ rotate (x, 19) ^ (x >> 10); }
  123. static inline uint32 S0 (const uint32 x) noexcept { return rotate (x, 2) ^ rotate (x, 13) ^ rotate (x, 22); }
  124. static inline uint32 S1 (const uint32 x) noexcept { return rotate (x, 6) ^ rotate (x, 11) ^ rotate (x, 25); }
  125. JUCE_DECLARE_NON_COPYABLE (SHA256Processor)
  126. };
  127. //==============================================================================
  128. SHA256::SHA256() noexcept
  129. {
  130. zerostruct (result);
  131. }
  132. SHA256::~SHA256() noexcept {}
  133. SHA256::SHA256 (const SHA256& other) noexcept
  134. {
  135. memcpy (result, other.result, sizeof (result));
  136. }
  137. SHA256& SHA256::operator= (const SHA256& other) noexcept
  138. {
  139. memcpy (result, other.result, sizeof (result));
  140. return *this;
  141. }
  142. SHA256::SHA256 (const MemoryBlock& data)
  143. {
  144. process (data.getData(), data.getSize());
  145. }
  146. SHA256::SHA256 (const void* const data, const size_t numBytes)
  147. {
  148. process (data, numBytes);
  149. }
  150. SHA256::SHA256 (InputStream& input, const int64 numBytesToRead)
  151. {
  152. SHA256Processor processor;
  153. processor.processStream (input, numBytesToRead, result);
  154. }
  155. SHA256::SHA256 (const File& file)
  156. {
  157. FileInputStream fin (file);
  158. if (fin.getStatus().wasOk())
  159. {
  160. SHA256Processor processor;
  161. processor.processStream (fin, -1, result);
  162. }
  163. else
  164. {
  165. zerostruct (result);
  166. }
  167. }
  168. SHA256::SHA256 (CharPointer_UTF8 utf8) noexcept
  169. {
  170. jassert (utf8.getAddress() != nullptr);
  171. process (utf8.getAddress(), utf8.sizeInBytes() - 1);
  172. }
  173. void SHA256::process (const void* const data, size_t numBytes)
  174. {
  175. MemoryInputStream m (data, numBytes, false);
  176. SHA256Processor processor;
  177. processor.processStream (m, -1, result);
  178. }
  179. MemoryBlock SHA256::getRawData() const
  180. {
  181. return MemoryBlock (result, sizeof (result));
  182. }
  183. String SHA256::toHexString() const
  184. {
  185. return String::toHexString (result, sizeof (result), 0);
  186. }
  187. bool SHA256::operator== (const SHA256& other) const noexcept { return memcmp (result, other.result, sizeof (result)) == 0; }
  188. bool SHA256::operator!= (const SHA256& other) const noexcept { return ! operator== (other); }
  189. //==============================================================================
  190. #if JUCE_UNIT_TESTS
  191. class SHA256Tests : public UnitTest
  192. {
  193. public:
  194. SHA256Tests() : UnitTest ("SHA-256", "Cryptography") {}
  195. void test (const char* input, const char* expected)
  196. {
  197. {
  198. SHA256 hash (input, strlen (input));
  199. expectEquals (hash.toHexString(), String (expected));
  200. }
  201. {
  202. CharPointer_UTF8 utf8 (input);
  203. SHA256 hash (utf8);
  204. expectEquals (hash.toHexString(), String (expected));
  205. }
  206. {
  207. MemoryInputStream m (input, strlen (input), false);
  208. SHA256 hash (m);
  209. expectEquals (hash.toHexString(), String (expected));
  210. }
  211. }
  212. void runTest() override
  213. {
  214. beginTest ("SHA256");
  215. test ("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
  216. test ("The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
  217. test ("The quick brown fox jumps over the lazy dog.", "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
  218. }
  219. };
  220. static SHA256Tests sha256UnitTests;
  221. #endif
  222. } // namespace juce