diff --git a/Builds/Linux/Makefile b/Builds/Linux/Makefile index a25e638e26..44d6a96386 100644 --- a/Builds/Linux/Makefile +++ b/Builds/Linux/Makefile @@ -114,6 +114,7 @@ OBJECTS := \ $(OBJDIR)/juce_MD5_56896819.o \ $(OBJDIR)/juce_Primes_32e6603.o \ $(OBJDIR)/juce_RSAKey_b60982ae.o \ + $(OBJDIR)/juce_SHA256_cb4da0d6.o \ $(OBJDIR)/juce_ActionBroadcaster_7f997786.o \ $(OBJDIR)/juce_AsyncUpdater_a7e1cb89.o \ $(OBJDIR)/juce_ChangeBroadcaster_3eb8fecc.o \ @@ -762,6 +763,11 @@ $(OBJDIR)/juce_RSAKey_b60982ae.o: ../../src/cryptography/juce_RSAKey.cpp @echo "Compiling juce_RSAKey.cpp" @$(CXX) $(CXXFLAGS) -o "$@" -c "$<" +$(OBJDIR)/juce_SHA256_cb4da0d6.o: ../../src/cryptography/juce_SHA256.cpp + -@mkdir -p $(OBJDIR) + @echo "Compiling juce_SHA256.cpp" + @$(CXX) $(CXXFLAGS) -o "$@" -c "$<" + $(OBJDIR)/juce_ActionBroadcaster_7f997786.o: ../../src/events/juce_ActionBroadcaster.cpp -@mkdir -p $(OBJDIR) @echo "Compiling juce_ActionBroadcaster.cpp" diff --git a/Builds/MacOSX/Juce.xcodeproj/project.pbxproj b/Builds/MacOSX/Juce.xcodeproj/project.pbxproj index 8a9163874f..c585b7f7bc 100644 --- a/Builds/MacOSX/Juce.xcodeproj/project.pbxproj +++ b/Builds/MacOSX/Juce.xcodeproj/project.pbxproj @@ -83,6 +83,7 @@ 708060ED04ADF4E4FB8E2171 = { isa = PBXBuildFile; fileRef = EAFD8FAF365DFB32DC42D0E7; }; 12E3CC31875A202D6B30F778 = { isa = PBXBuildFile; fileRef = E9E692847C14AD33CD5FB40B; }; CF51988743ED2CD823DFE0B5 = { isa = PBXBuildFile; fileRef = 7AE9331938549244E27A5D0E; }; + 4AAFC135B170CF02A40F0D51 = { isa = PBXBuildFile; fileRef = 4CBD301A08E9C3762F861FC3; }; 659D9CD58B6914EB420E6AEC = { isa = PBXBuildFile; fileRef = 31D985CB8646B78460E9D5A7; }; 55737E2F1817DE642AA7DA05 = { isa = PBXBuildFile; fileRef = 1617348BBF5D103619D76911; }; 6D2C50B0A69855A7F8C062E7 = { isa = PBXBuildFile; fileRef = B80F8CD026033ACCCE11A1A4; }; @@ -562,6 +563,8 @@ 22612DBDC6C689B605CC6B48 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_Primes.h"; path = "../../src/cryptography/juce_Primes.h"; sourceTree = "SOURCE_ROOT"; }; 7AE9331938549244E27A5D0E = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = "juce_RSAKey.cpp"; path = "../../src/cryptography/juce_RSAKey.cpp"; sourceTree = "SOURCE_ROOT"; }; 16335266AFB4D19F25410E1D = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_RSAKey.h"; path = "../../src/cryptography/juce_RSAKey.h"; sourceTree = "SOURCE_ROOT"; }; + 4CBD301A08E9C3762F861FC3 = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = "juce_SHA256.cpp"; path = "../../src/cryptography/juce_SHA256.cpp"; sourceTree = "SOURCE_ROOT"; }; + 02CD7BA1750DE36237369F78 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_SHA256.h"; path = "../../src/cryptography/juce_SHA256.h"; sourceTree = "SOURCE_ROOT"; }; 31D985CB8646B78460E9D5A7 = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = "juce_ActionBroadcaster.cpp"; path = "../../src/events/juce_ActionBroadcaster.cpp"; sourceTree = "SOURCE_ROOT"; }; 09F7685D1EFF472ECB1F5EF1 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_ActionBroadcaster.h"; path = "../../src/events/juce_ActionBroadcaster.h"; sourceTree = "SOURCE_ROOT"; }; 4EF8BD4BF46C4BCB39F96609 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_ActionListener.h"; path = "../../src/events/juce_ActionListener.h"; sourceTree = "SOURCE_ROOT"; }; @@ -1344,7 +1347,9 @@ E9E692847C14AD33CD5FB40B, 22612DBDC6C689B605CC6B48, 7AE9331938549244E27A5D0E, - 16335266AFB4D19F25410E1D ); name = cryptography; sourceTree = ""; }; + 16335266AFB4D19F25410E1D, + 4CBD301A08E9C3762F861FC3, + 02CD7BA1750DE36237369F78 ); name = cryptography; sourceTree = ""; }; 89F308FD85F378E278C98884 = { isa = PBXGroup; children = ( 31D985CB8646B78460E9D5A7, 09F7685D1EFF472ECB1F5EF1, @@ -2146,6 +2151,7 @@ 708060ED04ADF4E4FB8E2171, 12E3CC31875A202D6B30F778, CF51988743ED2CD823DFE0B5, + 4AAFC135B170CF02A40F0D51, 659D9CD58B6914EB420E6AEC, 55737E2F1817DE642AA7DA05, 6D2C50B0A69855A7F8C062E7, diff --git a/Builds/VisualStudio2005/Juce.vcproj b/Builds/VisualStudio2005/Juce.vcproj index 1be6dadff2..093a0083bd 100644 --- a/Builds/VisualStudio2005/Juce.vcproj +++ b/Builds/VisualStudio2005/Juce.vcproj @@ -396,6 +396,8 @@ + + diff --git a/Builds/VisualStudio2008/Juce.vcproj b/Builds/VisualStudio2008/Juce.vcproj index 1d0b930d3d..3ee6a8e757 100644 --- a/Builds/VisualStudio2008/Juce.vcproj +++ b/Builds/VisualStudio2008/Juce.vcproj @@ -396,6 +396,8 @@ + + diff --git a/Builds/VisualStudio2008_DLL/Juce.vcproj b/Builds/VisualStudio2008_DLL/Juce.vcproj index 656e6ade54..6e8deec1e7 100644 --- a/Builds/VisualStudio2008_DLL/Juce.vcproj +++ b/Builds/VisualStudio2008_DLL/Juce.vcproj @@ -398,6 +398,8 @@ + + diff --git a/Builds/VisualStudio2010/Juce.vcxproj b/Builds/VisualStudio2010/Juce.vcxproj index 55c454fb45..089c28c60b 100644 --- a/Builds/VisualStudio2010/Juce.vcxproj +++ b/Builds/VisualStudio2010/Juce.vcxproj @@ -197,6 +197,7 @@ + @@ -565,6 +566,7 @@ + diff --git a/Builds/VisualStudio2010/Juce.vcxproj.filters b/Builds/VisualStudio2010/Juce.vcxproj.filters index 5e46f51d30..09630c9a87 100644 --- a/Builds/VisualStudio2010/Juce.vcxproj.filters +++ b/Builds/VisualStudio2010/Juce.vcxproj.filters @@ -448,6 +448,9 @@ Juce\Source\cryptography + + Juce\Source\cryptography + Juce\Source\events @@ -1623,6 +1626,9 @@ Juce\Source\cryptography + + Juce\Source\cryptography + Juce\Source\events diff --git a/Builds/iOS/Juce.xcodeproj/project.pbxproj b/Builds/iOS/Juce.xcodeproj/project.pbxproj index 4579574a97..661c6d2fb3 100644 --- a/Builds/iOS/Juce.xcodeproj/project.pbxproj +++ b/Builds/iOS/Juce.xcodeproj/project.pbxproj @@ -83,6 +83,7 @@ 708060ED04ADF4E4FB8E2171 = { isa = PBXBuildFile; fileRef = EAFD8FAF365DFB32DC42D0E7; }; 12E3CC31875A202D6B30F778 = { isa = PBXBuildFile; fileRef = E9E692847C14AD33CD5FB40B; }; CF51988743ED2CD823DFE0B5 = { isa = PBXBuildFile; fileRef = 7AE9331938549244E27A5D0E; }; + 4AAFC135B170CF02A40F0D51 = { isa = PBXBuildFile; fileRef = 4CBD301A08E9C3762F861FC3; }; 659D9CD58B6914EB420E6AEC = { isa = PBXBuildFile; fileRef = 31D985CB8646B78460E9D5A7; }; 55737E2F1817DE642AA7DA05 = { isa = PBXBuildFile; fileRef = 1617348BBF5D103619D76911; }; 6D2C50B0A69855A7F8C062E7 = { isa = PBXBuildFile; fileRef = B80F8CD026033ACCCE11A1A4; }; @@ -562,6 +563,8 @@ 22612DBDC6C689B605CC6B48 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_Primes.h"; path = "../../src/cryptography/juce_Primes.h"; sourceTree = "SOURCE_ROOT"; }; 7AE9331938549244E27A5D0E = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = "juce_RSAKey.cpp"; path = "../../src/cryptography/juce_RSAKey.cpp"; sourceTree = "SOURCE_ROOT"; }; 16335266AFB4D19F25410E1D = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_RSAKey.h"; path = "../../src/cryptography/juce_RSAKey.h"; sourceTree = "SOURCE_ROOT"; }; + 4CBD301A08E9C3762F861FC3 = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = "juce_SHA256.cpp"; path = "../../src/cryptography/juce_SHA256.cpp"; sourceTree = "SOURCE_ROOT"; }; + 02CD7BA1750DE36237369F78 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_SHA256.h"; path = "../../src/cryptography/juce_SHA256.h"; sourceTree = "SOURCE_ROOT"; }; 31D985CB8646B78460E9D5A7 = { isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = "juce_ActionBroadcaster.cpp"; path = "../../src/events/juce_ActionBroadcaster.cpp"; sourceTree = "SOURCE_ROOT"; }; 09F7685D1EFF472ECB1F5EF1 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_ActionBroadcaster.h"; path = "../../src/events/juce_ActionBroadcaster.h"; sourceTree = "SOURCE_ROOT"; }; 4EF8BD4BF46C4BCB39F96609 = { isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "juce_ActionListener.h"; path = "../../src/events/juce_ActionListener.h"; sourceTree = "SOURCE_ROOT"; }; @@ -1344,7 +1347,9 @@ E9E692847C14AD33CD5FB40B, 22612DBDC6C689B605CC6B48, 7AE9331938549244E27A5D0E, - 16335266AFB4D19F25410E1D ); name = cryptography; sourceTree = ""; }; + 16335266AFB4D19F25410E1D, + 4CBD301A08E9C3762F861FC3, + 02CD7BA1750DE36237369F78 ); name = cryptography; sourceTree = ""; }; 89F308FD85F378E278C98884 = { isa = PBXGroup; children = ( 31D985CB8646B78460E9D5A7, 09F7685D1EFF472ECB1F5EF1, @@ -2150,6 +2155,7 @@ 708060ED04ADF4E4FB8E2171, 12E3CC31875A202D6B30F778, CF51988743ED2CD823DFE0B5, + 4AAFC135B170CF02A40F0D51, 659D9CD58B6914EB420E6AEC, 55737E2F1817DE642AA7DA05, 6D2C50B0A69855A7F8C062E7, diff --git a/Juce.jucer b/Juce.jucer index 0528d22767..916dd54785 100644 --- a/Juce.jucer +++ b/Juce.jucer @@ -456,6 +456,8 @@ file="src/cryptography/juce_RSAKey.cpp"/> + + = 0;) + finalBlocks [numBytes++] = (uint8) (length >> (i * 8)); // append the length. + + jassert (numBytes == 64 || numBytes == 128); + + processFullBlock (finalBlocks); + + if (numBytes > 64) + processFullBlock (finalBlocks + 64); + } + + void copyResult (uint8* result) const noexcept + { + for (int i = 0; i < 8; ++i) + { + *result++ = (uint8) (state[i] >> 24); + *result++ = (uint8) (state[i] >> 16); + *result++ = (uint8) (state[i] >> 8); + *result++ = (uint8) state[i]; + } + } + + void processStream (InputStream& input, int64 numBytesToRead, uint8* const result) + { + if (numBytesToRead < 0) + numBytesToRead = std::numeric_limits::max(); + + for (;;) + { + uint8 buffer [64]; + const int bytesRead = input.read (buffer, (int) jmin (numBytesToRead, (int64) sizeof (buffer))); + + if (bytesRead < sizeof (buffer)) + { + processFinalBlock (buffer, bytesRead); + break; + } + + numBytesToRead -= sizeof (buffer); + processFullBlock (buffer); + } + + copyResult (result); + } + +private: + uint32 state[8]; + uint64 length; + + static inline uint32 rotate (const uint32 x, const uint32 y) noexcept { return (x >> y) | (x << (32 - y)); } + static inline uint32 ch (const uint32 x, const uint32 y, const uint32 z) noexcept { return z ^ ((y ^ z) & x); } + static inline uint32 maj (const uint32 x, const uint32 y, const uint32 z) noexcept { return y ^ ((y ^ z) & (x ^ y)); } + + static inline uint32 s0 (const uint32 x) noexcept { return rotate (x, 7) ^ rotate (x, 18) ^ (x >> 3); } + static inline uint32 s1 (const uint32 x) noexcept { return rotate (x, 17) ^ rotate (x, 19) ^ (x >> 10); } + static inline uint32 S0 (const uint32 x) noexcept { return rotate (x, 2) ^ rotate (x, 13) ^ rotate (x, 22); } + static inline uint32 S1 (const uint32 x) noexcept { return rotate (x, 6) ^ rotate (x, 11) ^ rotate (x, 25); } + + JUCE_DECLARE_NON_COPYABLE (SHA256Processor); +}; + +SHA256::SHA256() +{ + zerostruct (result); +} + +SHA256::~SHA256() {} + +SHA256::SHA256 (const SHA256& other) +{ + memcpy (result, other.result, sizeof (result)); +} + +SHA256& SHA256::operator= (const SHA256& other) +{ + memcpy (result, other.result, sizeof (result)); + return *this; +} + +SHA256::SHA256 (const MemoryBlock& data) +{ + MemoryInputStream m (data, false); + SHA256Processor processor; + processor.processStream (m, -1, result); +} + +SHA256::SHA256 (const void* const data, const size_t numBytes) +{ + MemoryInputStream m (data, numBytes, false); + SHA256Processor processor; + processor.processStream (m, -1, result); +} + +SHA256::SHA256 (InputStream& input, const int64 numBytesToRead) +{ + SHA256Processor processor; + processor.processStream (input, numBytesToRead, result); +} + +SHA256::SHA256 (const File& file) +{ + FileInputStream fin (file); + + if (fin.getStatus().wasOk()) + { + SHA256Processor processor; + processor.processStream (fin, -1, result); + } + else + { + zerostruct (result); + } +} + +MemoryBlock SHA256::getRawData() const +{ + return MemoryBlock (result, sizeof (result)); +} + +String SHA256::toHexString() const +{ + return String::toHexString (result, sizeof (result), 0); +} + +bool SHA256::operator== (const SHA256& other) const noexcept { return memcmp (result, other.result, sizeof (result)) == 0; } +bool SHA256::operator!= (const SHA256& other) const noexcept { return ! operator== (other); } + +#if JUCE_UNIT_TESTS + +class SHA256Tests : public UnitTest +{ +public: + SHA256Tests() : UnitTest ("SHA-256") {} + + void runTest() + { + beginTest ("SHA-256"); + + { + char n[] = ""; + SHA256 sha (n, 0); + expectEquals (sha.toHexString(), String ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); + } + + { + const char n[] = "The quick brown fox jumps over the lazy dog"; + SHA256 sha (n, sizeof (n) - 1); + expectEquals (sha.toHexString(), String ("d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592")); + } + + { + const char n[] = "The quick brown fox jumps over the lazy dog."; + SHA256 sha (n, sizeof (n) - 1); + expectEquals (sha.toHexString(), String ("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")); + } + } +}; + +static SHA256Tests sha256UnitTests; + +#endif + +END_JUCE_NAMESPACE + +/*** End of inlined file: juce_SHA256.cpp ***/ + + /*** Start of inlined file: juce_Primes.cpp ***/ BEGIN_JUCE_NAMESPACE @@ -108423,6 +108661,7 @@ END_JUCE_NAMESPACE /*** Start of inlined file: juce_CoreAudioFormat.cpp ***/ #if JUCE_MAC || JUCE_IOS +#define Point CarbonDummyPointName #include BEGIN_JUCE_NAMESPACE diff --git a/juce_amalgamated.h b/juce_amalgamated.h index 033af52dde..7b2e794354 100644 --- a/juce_amalgamated.h +++ b/juce_amalgamated.h @@ -73,7 +73,7 @@ namespace JuceDummyNamespace {} */ #define JUCE_MAJOR_VERSION 1 #define JUCE_MINOR_VERSION 54 -#define JUCE_BUILDNUMBER 21 +#define JUCE_BUILDNUMBER 22 /** Current Juce version number. @@ -18903,6 +18903,81 @@ private: /*** End of inlined file: juce_RSAKey.h ***/ +#endif +#ifndef __JUCE_SHA256_JUCEHEADER__ + +/*** Start of inlined file: juce_SHA256.h ***/ +#ifndef __JUCE_SHA256_JUCEHEADER__ +#define __JUCE_SHA256_JUCEHEADER__ + +/** + SHA-256 secure hash generator. + + Create one of these objects from a block of source data or a string, and it + represents the SHA-256 hash of that data. + + You can retrieve the hash as a raw 32-byte block, or as a 64-digit hex string. +*/ +class JUCE_API SHA256 +{ +public: + + /** Creates an empty SHA256 object. + The default constructor just creates a hash filled with zeros. (This is not + equal to the hash of an empty block of data). + */ + SHA256(); + + /** Destructor. */ + ~SHA256(); + + /** Creates a copy of another SHA256. */ + SHA256 (const SHA256& other); + + /** Copies another SHA256. */ + SHA256& operator= (const SHA256& other); + + /** Creates a hash from a block of raw data. */ + explicit SHA256 (const MemoryBlock& data); + + /** Creates a hash from a block of raw data. */ + SHA256 (const void* data, size_t numBytes); + + /** Creates a hash from the contents of a stream. + + This will read from the stream until the stream is exhausted, or until + maxBytesToRead bytes have been read. If maxBytesToRead is negative, the entire + stream will be read. + */ + SHA256 (InputStream& input, int64 maxBytesToRead = -1); + + /** Reads a file and generates the hash of its contents. + If the file can't be opened, the hash will be left uninitialised (i.e. full + of zeros). + */ + explicit SHA256 (const File& file); + + /** Returns the hash as a 32-byte block of data. */ + MemoryBlock getRawData() const; + + /** Returns the checksum as a 64-digit hex string. */ + String toHexString() const; + + bool operator== (const SHA256& other) const noexcept; + bool operator!= (const SHA256& other) const noexcept; + +private: + + uint8 result [32]; + + JUCE_LEAK_DETECTOR (SHA256); +}; + +#endif // __JUCE_SHA256_JUCEHEADER__ + +/*** End of inlined file: juce_SHA256.h ***/ + + #endif #ifndef __JUCE_DIRECTORYITERATOR_JUCEHEADER__ diff --git a/src/audio/audio_file_formats/juce_CoreAudioFormat.cpp b/src/audio/audio_file_formats/juce_CoreAudioFormat.cpp index af5070ce4e..ff43999ff9 100644 --- a/src/audio/audio_file_formats/juce_CoreAudioFormat.cpp +++ b/src/audio/audio_file_formats/juce_CoreAudioFormat.cpp @@ -30,6 +30,7 @@ #include "../../core/juce_StandardHeader.h" +#define Point CarbonDummyPointName #include BEGIN_JUCE_NAMESPACE diff --git a/src/core/juce_StandardHeader.h b/src/core/juce_StandardHeader.h index af191b54ca..34797abb02 100644 --- a/src/core/juce_StandardHeader.h +++ b/src/core/juce_StandardHeader.h @@ -33,7 +33,7 @@ */ #define JUCE_MAJOR_VERSION 1 #define JUCE_MINOR_VERSION 54 -#define JUCE_BUILDNUMBER 21 +#define JUCE_BUILDNUMBER 22 /** Current Juce version number. diff --git a/src/cryptography/juce_SHA256.cpp b/src/cryptography/juce_SHA256.cpp new file mode 100644 index 0000000000..efff857ffb --- /dev/null +++ b/src/cryptography/juce_SHA256.cpp @@ -0,0 +1,271 @@ +/* + ============================================================================== + + This file is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-11 by Raw Material Software Ltd. + + ------------------------------------------------------------------------------ + + JUCE can be redistributed and/or modified under the terms of the GNU General + Public License (Version 2), as published by the Free Software Foundation. + A copy of the license is included in the JUCE distribution, or can be found + online at www.gnu.org/licenses. + + JUCE is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + ------------------------------------------------------------------------------ + + To release a closed-source product which uses JUCE, commercial licenses are + available: visit www.rawmaterialsoftware.com/juce for more information. + + ============================================================================== +*/ + +#include "../core/juce_StandardHeader.h" + +BEGIN_JUCE_NAMESPACE + +#include "juce_SHA256.h" +#include "../io/files/juce_FileInputStream.h" +#include "../io/streams/juce_MemoryInputStream.h" + + +//============================================================================== +class SHA256Processor +{ +public: + SHA256Processor() noexcept + : length (0) + { + state[0] = 0x6a09e667; + state[1] = 0xbb67ae85; + state[2] = 0x3c6ef372; + state[3] = 0xa54ff53a; + state[4] = 0x510e527f; + state[5] = 0x9b05688c; + state[6] = 0x1f83d9ab; + state[7] = 0x5be0cd19; + } + + // expects 64 bytes of data + void processFullBlock (const void* const data) noexcept + { + const uint32 constants[] = + { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + }; + + uint32 block[16], s[8]; + memcpy (s, state, sizeof (s)); + + for (uint32 j = 0; j < 64; j += 16) + { + #define JUCE_SHA256(i) 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] \ + + (j != 0 ? (block[i & 15] += s1 (block[(i - 2) & 15]) + block[(i - 7) & 15] + s0 (block[(i - 15) & 15])) \ + : (block[i] = ByteOrder::bigEndianInt (addBytesToPointer (data, i * 4)))); \ + s[(3 - i) & 7] += s[(7 - i) & 7]; \ + s[(7 - i) & 7] += S0 (s[(0 - i) & 7]) + maj (s[(0 - i) & 7], s[(1 - i) & 7], s[(2 - i) & 7]) + + JUCE_SHA256(0); JUCE_SHA256(1); JUCE_SHA256(2); JUCE_SHA256(3); JUCE_SHA256(4); JUCE_SHA256(5); JUCE_SHA256(6); JUCE_SHA256(7); + JUCE_SHA256(8); JUCE_SHA256(9); JUCE_SHA256(10); JUCE_SHA256(11); JUCE_SHA256(12); JUCE_SHA256(13); JUCE_SHA256(14); JUCE_SHA256(15); + #undef JUCE_SHA256 + } + + for (int i = 0; i < 8; ++i) + state[i] += s[i]; + + length += 64; + } + + void processFinalBlock (const void* const data, int numBytes) noexcept + { + jassert (numBytes < 64); + + length += numBytes; + length *= 8; // (the length is stored as a count of bits, not bytes) + + uint8 finalBlocks[128]; + + memcpy (finalBlocks, data, numBytes); + finalBlocks [numBytes++] = 128; // append a '1' bit + + while (numBytes != 56 && numBytes < 64 + 56) + finalBlocks [numBytes++] = 0; // pad with zeros.. + + for (int i = 8; --i >= 0;) + finalBlocks [numBytes++] = (uint8) (length >> (i * 8)); // append the length. + + jassert (numBytes == 64 || numBytes == 128); + + processFullBlock (finalBlocks); + + if (numBytes > 64) + processFullBlock (finalBlocks + 64); + } + + void copyResult (uint8* result) const noexcept + { + for (int i = 0; i < 8; ++i) + { + *result++ = (uint8) (state[i] >> 24); + *result++ = (uint8) (state[i] >> 16); + *result++ = (uint8) (state[i] >> 8); + *result++ = (uint8) state[i]; + } + } + + void processStream (InputStream& input, int64 numBytesToRead, uint8* const result) + { + if (numBytesToRead < 0) + numBytesToRead = std::numeric_limits::max(); + + for (;;) + { + uint8 buffer [64]; + const int bytesRead = input.read (buffer, (int) jmin (numBytesToRead, (int64) sizeof (buffer))); + + if (bytesRead < sizeof (buffer)) + { + processFinalBlock (buffer, bytesRead); + break; + } + + numBytesToRead -= sizeof (buffer); + processFullBlock (buffer); + } + + copyResult (result); + } + +private: + uint32 state[8]; + uint64 length; + + static inline uint32 rotate (const uint32 x, const uint32 y) noexcept { return (x >> y) | (x << (32 - y)); } + static inline uint32 ch (const uint32 x, const uint32 y, const uint32 z) noexcept { return z ^ ((y ^ z) & x); } + static inline uint32 maj (const uint32 x, const uint32 y, const uint32 z) noexcept { return y ^ ((y ^ z) & (x ^ y)); } + + static inline uint32 s0 (const uint32 x) noexcept { return rotate (x, 7) ^ rotate (x, 18) ^ (x >> 3); } + static inline uint32 s1 (const uint32 x) noexcept { return rotate (x, 17) ^ rotate (x, 19) ^ (x >> 10); } + static inline uint32 S0 (const uint32 x) noexcept { return rotate (x, 2) ^ rotate (x, 13) ^ rotate (x, 22); } + static inline uint32 S1 (const uint32 x) noexcept { return rotate (x, 6) ^ rotate (x, 11) ^ rotate (x, 25); } + + JUCE_DECLARE_NON_COPYABLE (SHA256Processor); +}; + +//============================================================================== +SHA256::SHA256() +{ + zerostruct (result); +} + +SHA256::~SHA256() {} + +SHA256::SHA256 (const SHA256& other) +{ + memcpy (result, other.result, sizeof (result)); +} + +SHA256& SHA256::operator= (const SHA256& other) +{ + memcpy (result, other.result, sizeof (result)); + return *this; +} + +SHA256::SHA256 (const MemoryBlock& data) +{ + MemoryInputStream m (data, false); + SHA256Processor processor; + processor.processStream (m, -1, result); +} + +SHA256::SHA256 (const void* const data, const size_t numBytes) +{ + MemoryInputStream m (data, numBytes, false); + SHA256Processor processor; + processor.processStream (m, -1, result); +} + +SHA256::SHA256 (InputStream& input, const int64 numBytesToRead) +{ + SHA256Processor processor; + processor.processStream (input, numBytesToRead, result); +} + +SHA256::SHA256 (const File& file) +{ + FileInputStream fin (file); + + if (fin.getStatus().wasOk()) + { + SHA256Processor processor; + processor.processStream (fin, -1, result); + } + else + { + zerostruct (result); + } +} + +MemoryBlock SHA256::getRawData() const +{ + return MemoryBlock (result, sizeof (result)); +} + +String SHA256::toHexString() const +{ + return String::toHexString (result, sizeof (result), 0); +} + +bool SHA256::operator== (const SHA256& other) const noexcept { return memcmp (result, other.result, sizeof (result)) == 0; } +bool SHA256::operator!= (const SHA256& other) const noexcept { return ! operator== (other); } + + +//============================================================================== +#if JUCE_UNIT_TESTS + +#include "../utilities/juce_UnitTest.h" + +class SHA256Tests : public UnitTest +{ +public: + SHA256Tests() : UnitTest ("SHA-256") {} + + void runTest() + { + beginTest ("SHA-256"); + + { + char n[] = ""; + SHA256 sha (n, 0); + expectEquals (sha.toHexString(), String ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); + } + + { + const char n[] = "The quick brown fox jumps over the lazy dog"; + SHA256 sha (n, sizeof (n) - 1); + expectEquals (sha.toHexString(), String ("d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592")); + } + + { + const char n[] = "The quick brown fox jumps over the lazy dog."; + SHA256 sha (n, sizeof (n) - 1); + expectEquals (sha.toHexString(), String ("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")); + } + } +}; + +static SHA256Tests sha256UnitTests; + +#endif + +END_JUCE_NAMESPACE diff --git a/src/cryptography/juce_SHA256.h b/src/cryptography/juce_SHA256.h new file mode 100644 index 0000000000..8d5486f230 --- /dev/null +++ b/src/cryptography/juce_SHA256.h @@ -0,0 +1,103 @@ +/* + ============================================================================== + + This file is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-11 by Raw Material Software Ltd. + + ------------------------------------------------------------------------------ + + JUCE can be redistributed and/or modified under the terms of the GNU General + Public License (Version 2), as published by the Free Software Foundation. + A copy of the license is included in the JUCE distribution, or can be found + online at www.gnu.org/licenses. + + JUCE is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + ------------------------------------------------------------------------------ + + To release a closed-source product which uses JUCE, commercial licenses are + available: visit www.rawmaterialsoftware.com/juce for more information. + + ============================================================================== +*/ + +#ifndef __JUCE_SHA256_JUCEHEADER__ +#define __JUCE_SHA256_JUCEHEADER__ + +#include "../memory/juce_MemoryBlock.h" +#include "../io/streams/juce_InputStream.h" +#include "../io/files/juce_File.h" + + +//============================================================================== +/** + SHA-256 secure hash generator. + + Create one of these objects from a block of source data or a string, and it + represents the SHA-256 hash of that data. + + You can retrieve the hash as a raw 32-byte block, or as a 64-digit hex string. +*/ +class JUCE_API SHA256 +{ +public: + //============================================================================== + /** Creates an empty SHA256 object. + The default constructor just creates a hash filled with zeros. (This is not + equal to the hash of an empty block of data). + */ + SHA256(); + + /** Destructor. */ + ~SHA256(); + + /** Creates a copy of another SHA256. */ + SHA256 (const SHA256& other); + + /** Copies another SHA256. */ + SHA256& operator= (const SHA256& other); + + //============================================================================== + /** Creates a hash from a block of raw data. */ + explicit SHA256 (const MemoryBlock& data); + + /** Creates a hash from a block of raw data. */ + SHA256 (const void* data, size_t numBytes); + + /** Creates a hash from the contents of a stream. + + This will read from the stream until the stream is exhausted, or until + maxBytesToRead bytes have been read. If maxBytesToRead is negative, the entire + stream will be read. + */ + SHA256 (InputStream& input, int64 maxBytesToRead = -1); + + /** Reads a file and generates the hash of its contents. + If the file can't be opened, the hash will be left uninitialised (i.e. full + of zeros). + */ + explicit SHA256 (const File& file); + + //============================================================================== + /** Returns the hash as a 32-byte block of data. */ + MemoryBlock getRawData() const; + + /** Returns the checksum as a 64-digit hex string. */ + String toHexString() const; + + //============================================================================== + bool operator== (const SHA256& other) const noexcept; + bool operator!= (const SHA256& other) const noexcept; + + +private: + //============================================================================== + uint8 result [32]; + + JUCE_LEAK_DETECTOR (SHA256); +}; + + +#endif // __JUCE_SHA256_JUCEHEADER__ diff --git a/src/juce_core_includes.h b/src/juce_core_includes.h index 2599b13c27..040e3f7908 100644 --- a/src/juce_core_includes.h +++ b/src/juce_core_includes.h @@ -128,6 +128,9 @@ #ifndef __JUCE_RSAKEY_JUCEHEADER__ #include "cryptography/juce_RSAKey.h" #endif +#ifndef __JUCE_SHA256_JUCEHEADER__ + #include "cryptography/juce_SHA256.h" +#endif #ifndef __JUCE_DIRECTORYITERATOR_JUCEHEADER__ #include "io/files/juce_DirectoryIterator.h" #endif