Browse Source

Improved the SHA256 unit test, and added test for MD5.

tags/2021-05-28
jules 12 years ago
parent
commit
e31ebbfbc9
2 changed files with 55 additions and 12 deletions
  1. +37
    -0
      modules/juce_cryptography/hashing/juce_MD5.cpp
  2. +18
    -12
      modules/juce_cryptography/hashing/juce_SHA256.cpp

+ 37
- 0
modules/juce_cryptography/hashing/juce_MD5.cpp View File

@@ -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

+ 18
- 12
modules/juce_cryptography/hashing/juce_SHA256.cpp View File

@@ -240,28 +240,34 @@ class SHA256Tests : public UnitTest
public:
SHA256Tests() : UnitTest ("SHA-256") {}
void runTest()
void test (const char* input, const char* expected)
{
beginTest ("SHA-256");
{
char n[] = "";
SHA256 sha (n, 0);
expectEquals (sha.toHexString(), String ("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
SHA256 hash (input, strlen (input));
expectEquals (hash.toHexString(), String (expected));
}
{
const char n[] = "The quick brown fox jumps over the lazy dog";
SHA256 sha (n, sizeof (n) - 1);
expectEquals (sha.toHexString(), String ("d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"));
CharPointer_UTF8 utf8 (input);
SHA256 hash (utf8);
expectEquals (hash.toHexString(), String (expected));
}
{
const char n[] = "The quick brown fox jumps over the lazy dog.";
SHA256 sha (n, sizeof (n) - 1);
expectEquals (sha.toHexString(), String ("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"));
MemoryInputStream m (input, strlen (input), false);
SHA256 hash (m);
expectEquals (hash.toHexString(), String (expected));
}
}
void runTest()
{
beginTest ("SHA256");
test ("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
test ("The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");
test ("The quick brown fox jumps over the lazy dog.", "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c");
}
};
static SHA256Tests sha256UnitTests;


Loading…
Cancel
Save