1 | /* |
2 | * This code implements the MD5 message-digest algorithm. |
3 | * The algorithm is due to Ron Rivest. This code was |
4 | * written by Colin Plumb in 1993, no copyright is claimed. |
5 | * This code is in the public domain; do with it what you wish. |
6 | * |
7 | * Equivalent code is available from RSA Data Security, Inc. |
8 | * This code has been tested against that, and is equivalent, |
9 | * except that you don't need to include two pages of legalese |
10 | * with every copy. |
11 | */ |
12 | |
13 | #include "config.h" |
14 | #include <wtf/MD5.h> |
15 | #include <wtf/StringExtras.h> |
16 | #include <wtf/text/CString.h> |
17 | |
18 | namespace TestWebKitAPI { |
19 | |
20 | static void expectMD5(CString input, CString expected) |
21 | { |
22 | MD5 md5; |
23 | md5.addBytes(reinterpret_cast<const uint8_t*>(input.data()), input.length()); |
24 | MD5::Digest digest; |
25 | md5.checksum(digest); |
26 | char* buf = 0; |
27 | CString actual = CString::newUninitialized(32, buf); |
28 | for (size_t i = 0; i < MD5::hashSize; i++, buf += 2) |
29 | snprintf(buf, 3, "%02x" , digest[i]); |
30 | |
31 | ASSERT_EQ(expected.length(), actual.length()); |
32 | ASSERT_STREQ(expected.data(), actual.data()); |
33 | } |
34 | |
35 | TEST(WTF_MD5, Computation) |
36 | { |
37 | // MD5 Test suite from http://www.ietf.org/rfc/rfc1321.txt. |
38 | expectMD5("" , "d41d8cd98f00b204e9800998ecf8427e" ); |
39 | expectMD5("a" , "0cc175b9c0f1b6a831c399e269772661" ); |
40 | expectMD5("abc" , "900150983cd24fb0d6963f7d28e17f72" ); |
41 | expectMD5("message digest" , "f96b697d7cb7938d525a2f31aaf161d0" ); |
42 | expectMD5("abcdefghijklmnopqrstuvwxyz" , "c3fcd3d76192e4007dfb496cca67e13b" ); |
43 | expectMD5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" , "d174ab98d277d9f5a5611c2c9f419d9f" ); |
44 | expectMD5("12345678901234567890123456789012345678901234567890123456789012345678901234567890" , "57edf4a22be3c955ac49da2e2107b67a" ); |
45 | } |
46 | |
47 | } // namespace TestWebKitAPI |
48 | |