diff --git a/modules/juce_cryptography/hashing/juce_MD5.cpp b/modules/juce_cryptography/hashing/juce_MD5.cpp index 90d64a44ec..3902829e38 100644 --- a/modules/juce_cryptography/hashing/juce_MD5.cpp +++ b/modules/juce_cryptography/hashing/juce_MD5.cpp @@ -297,3 +297,40 @@ String MD5::toHexString() const //============================================================================== bool MD5::operator== (const MD5& other) const noexcept { return memcmp (result, other.result, sizeof (result)) == 0; } bool MD5::operator!= (const MD5& other) const noexcept { return ! operator== (other); } + + +//============================================================================== +#if JUCE_UNIT_TESTS + +class MD5Tests : public UnitTest +{ +public: + MD5Tests() : UnitTest ("MD5") {} + + void test (const char* input, const char* expected) + { + { + MD5 hash (input, strlen (input)); + expectEquals (hash.toHexString(), String (expected)); + } + + { + MemoryInputStream m (input, strlen (input), false); + MD5 hash (m); + expectEquals (hash.toHexString(), String (expected)); + } + } + + void runTest() + { + beginTest ("MD5"); + + test ("", "d41d8cd98f00b204e9800998ecf8427e"); + test ("The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6"); + test ("The quick brown fox jumps over the lazy dog.", "e4d909c290d0fb1ca068ffaddf22cbd0"); + } +}; + +static MD5Tests MD5UnitTests; + +#endif diff --git a/modules/juce_cryptography/hashing/juce_SHA256.cpp b/modules/juce_cryptography/hashing/juce_SHA256.cpp index 0c1f8db34d..15e6840c07 100644 --- a/modules/juce_cryptography/hashing/juce_SHA256.cpp +++ b/modules/juce_cryptography/hashing/juce_SHA256.cpp @@ -240,27 +240,33 @@ class SHA256Tests : public UnitTest public: SHA256Tests() : UnitTest ("SHA-256") {} + void test (const char* input, const char* expected) + { + { + SHA256 hash (input, strlen (input)); + expectEquals (hash.toHexString(), String (expected)); + } + + { + CharPointer_UTF8 utf8 (input); + SHA256 hash (utf8); + expectEquals (hash.toHexString(), String (expected)); + } + + { + MemoryInputStream m (input, strlen (input), false); + SHA256 hash (m); + expectEquals (hash.toHexString(), String (expected)); + } + } + void runTest() { - beginTest ("SHA-256"); + beginTest ("SHA256"); - { - 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")); - } + test ("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); + test ("The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"); + test ("The quick brown fox jumps over the lazy dog.", "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"); } };