aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Richard <[email protected]>2005-09-06 20:08:39 +0000
committerVincent Richard <[email protected]>2005-09-06 20:08:39 +0000
commit3b1fcbe825c90dcb5e358ad7632b45d4cd512f54 (patch)
tree183d183a49b40058a300a7832cfe02c91ec5cab8
parentAdded progression notifications. (diff)
downloadvmime-3b1fcbe825c90dcb5e358ad7632b45d4cd512f54.tar.gz
vmime-3b1fcbe825c90dcb5e358ad7632b45d4cd512f54.zip
New namespace for message digest algorithms.
-rw-r--r--ChangeLog5
-rw-r--r--SConstruct21
-rw-r--r--src/exception.cpp12
-rw-r--r--src/net/authHelper.cpp30
-rw-r--r--src/net/pop3/POP3Store.cpp11
-rw-r--r--src/security/digest/md5/md5MessageDigest.cpp (renamed from src/utility/md5.cpp)103
-rw-r--r--src/security/digest/messageDigest.cpp (renamed from vmime/utility/md5.hpp)51
-rw-r--r--src/security/digest/messageDigestFactory.cpp80
-rw-r--r--src/security/digest/sha1/sha1MessageDigest.cpp262
-rw-r--r--tests/security/digest/md5Test.cpp229
-rw-r--r--tests/security/digest/sha1Test.cpp119
-rw-r--r--tests/testRunner.cpp31
-rw-r--r--tests/testUtils.hpp11
-rw-r--r--tests/utility/md5Test.cpp98
-rw-r--r--vmime/exception.hpp18
-rw-r--r--vmime/object.hpp3
-rw-r--r--vmime/security/digest/md5/md5MessageDigest.hpp76
-rw-r--r--vmime/security/digest/messageDigest.hpp134
-rw-r--r--vmime/security/digest/messageDigestFactory.hpp108
-rw-r--r--vmime/security/digest/sha1/sha1MessageDigest.hpp75
-rw-r--r--vmime/types.hpp2
21 files changed, 1279 insertions, 200 deletions
diff --git a/ChangeLog b/ChangeLog
index 63a61f47..317443b7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,11 @@
VERSION 0.7.2cvs
================
+2005-09-06 Vincent Richard <[email protected]>
+
+ * Created 'vmime::security' and 'vmime::security::digest' namespaces.
+ MD5 has been moved here. Added SHA-1 hash algorithm.
+
2005-09-03 Vincent Richard <[email protected]>
* encoder*, *contentHandler: added progression notifications.
diff --git a/SConstruct b/SConstruct
index b3649ebe..b4ff328d 100644
--- a/SConstruct
+++ b/SConstruct
@@ -79,6 +79,7 @@ packageVersionedName = packageName
##################
libvmime_sources = [
+ # ============================== Parser ==============================
'address.cpp', 'address.hpp',
'addressList.cpp', 'addressList.hpp',
'attachment.hpp',
@@ -147,11 +148,11 @@ libvmime_sources = [
'types.hpp',
'word.cpp', 'word.hpp',
'vmime.hpp',
+ # ============================== Utility =============================
'utility/childProcess.hpp',
'utility/file.hpp',
'utility/datetimeUtils.cpp', 'utility/datetimeUtils.hpp',
'utility/filteredStream.cpp', 'utility/filteredStream.hpp',
- 'utility/md5.cpp', 'utility/md5.hpp',
'utility/path.cpp', 'utility/path.hpp',
'utility/progressionListener.cpp', 'utility/progressionListener.hpp',
'utility/random.cpp', 'utility/random.hpp',
@@ -161,11 +162,18 @@ libvmime_sources = [
'utility/stringUtils.cpp', 'utility/stringUtils.hpp',
'utility/url.cpp', 'utility/url.hpp',
'utility/urlUtils.cpp', 'utility/urlUtils.hpp',
+ # =============================== MDN ================================
'mdn/MDNHelper.cpp', 'mdn/MDNHelper.hpp',
'mdn/MDNInfos.cpp', 'mdn/MDNInfos.hpp',
'mdn/receivedMDNInfos.cpp', 'mdn/receivedMDNInfos.hpp',
'mdn/sendableMDNInfos.cpp', 'mdn/sendableMDNInfos.hpp',
- 'misc/importanceHelper.cpp', 'misc/importanceHelper.hpp'
+ # =============================== Misc ===============================
+ 'misc/importanceHelper.cpp', 'misc/importanceHelper.hpp',
+ # ============================= Security =============================
+ 'security/digest/messageDigest.cpp', 'security/digest/messageDigest.hpp',
+ 'security/digest/messageDigestFactory.cpp', 'security/digest/messageDigestFactory.hpp',
+ 'security/digest/md5/md5MessageDigest.cpp', 'security/digest/md5/md5MessageDigest.hpp',
+ 'security/digest/sha1/sha1MessageDigest.cpp', 'security/digest/sha1/sha1MessageDigest.hpp'
]
libvmime_examples_sources = [
@@ -298,6 +306,7 @@ libvmimetest_common = [
libvmimetest_sources = [
'tests/testRunner.cpp',
+ # ============================== Parser ==============================
'tests/parser/bodyPartTest.cpp',
'tests/parser/datetimeTest.cpp',
'tests/parser/dispositionTest.cpp',
@@ -310,14 +319,18 @@ libvmimetest_sources = [
'tests/parser/pathTest.cpp',
'tests/parser/parameterTest.cpp',
'tests/parser/textTest.cpp',
+ # ============================== Utility =============================
'tests/utility/filteredStreamTest.cpp',
- 'tests/utility/md5Test.cpp',
'tests/utility/stringProxyTest.cpp',
'tests/utility/stringUtilsTest.cpp',
'tests/utility/pathTest.cpp',
'tests/utility/urlTest.cpp',
'tests/utility/smartPtrTest.cpp',
- 'tests/misc/importanceHelperTest.cpp'
+ # =============================== Misc ===============================
+ 'tests/misc/importanceHelperTest.cpp',
+ # ============================= Security =============================
+ 'tests/security/digest/md5Test.cpp',
+ 'tests/security/digest/sha1Test.cpp'
]
libvmime_autotools = [
diff --git a/src/exception.cpp b/src/exception.cpp
index a8479b82..7482482a 100644
--- a/src/exception.cpp
+++ b/src/exception.cpp
@@ -121,6 +121,18 @@ const char* no_encoder_available::name() const throw() { return "no_encoder_avai
//
+// no_digest_algorithm_available
+//
+
+no_digest_algorithm_available::~no_digest_algorithm_available() throw() {}
+no_digest_algorithm_available::no_digest_algorithm_available(const string& name, const exception& other)
+ : exception("No algorithm available: '" + name + "'.", other) {}
+
+exception* no_digest_algorithm_available::clone() const { return new no_digest_algorithm_available(*this); }
+const char* no_digest_algorithm_available::name() const throw() { return "no_digest_algorithm_available"; }
+
+
+//
// no_such_parameter
//
diff --git a/src/net/authHelper.cpp b/src/net/authHelper.cpp
index e10b47ee..a946e6f1 100644
--- a/src/net/authHelper.cpp
+++ b/src/net/authHelper.cpp
@@ -20,7 +20,7 @@
#include "vmime/net/authHelper.hpp"
#include "vmime/config.hpp"
-#include "vmime/utility/md5.hpp"
+#include "vmime/security/digest/messageDigestFactory.hpp"
namespace vmime {
@@ -42,13 +42,17 @@ void hmac_md5(const string& text, const string& key, string& hexDigest)
unsigned char tkey[16];
int tkeyLen;
+ ref <security::digest::messageDigest> md5 =
+ security::digest::messageDigestFactory::getInstance()->create("md5");
+
// If key is longer than 64 bytes reset it to key = MD5(key)
if (key.length() > 64)
{
- utility::md5 keyMD5;
- keyMD5.update(reinterpret_cast <const vmime_uint8*>(key.data()), key.length());
+ md5->reset();
+ md5->update(reinterpret_cast <const vmime_uint8*>(key.data()), key.length());
+ md5->finalize();
- std::copy(keyMD5.hash(), keyMD5.hash() + 16, tkey);
+ std::copy(md5->getDigest(), md5->getDigest() + 16, tkey);
tkeyLen = 16;
}
else
@@ -84,20 +88,22 @@ void hmac_md5(const string& text, const string& key, string& hexDigest)
}
// Perform inner MD5
- utility::md5 innerMD5;
- innerMD5.update(ipad, 64);
- innerMD5.update(text);
+ md5->reset();
+ md5->update(ipad, 64);
+ md5->update(text);
+ md5->finalize();
- std::copy(innerMD5.hash(), innerMD5.hash() + 16, digest);
+ std::copy(md5->getDigest(), md5->getDigest() + 16, digest);
// Perform outer MD5
- utility::md5 outerMD5;
- outerMD5.update(opad, 64);
- outerMD5.update(digest, 16);
+ md5->reset();
+ md5->update(opad, 64);
+ md5->update(digest, 16);
+ md5->finalize();
//std::copy(outerMD5.hash(), outerMD5.hash() + 16, digest);
- hexDigest = outerMD5.hex();
+ hexDigest = md5->getHexDigest();
}
diff --git a/src/net/pop3/POP3Store.cpp b/src/net/pop3/POP3Store.cpp
index 9ccfa7b7..5041d2d3 100644
--- a/src/net/pop3/POP3Store.cpp
+++ b/src/net/pop3/POP3Store.cpp
@@ -23,7 +23,7 @@
#include "vmime/exception.hpp"
#include "vmime/platformDependant.hpp"
#include "vmime/messageId.hpp"
-#include "vmime/utility/md5.hpp"
+#include "vmime/security/digest/messageDigestFactory.hpp"
#include "vmime/utility/filteredStream.hpp"
#include <algorithm>
@@ -145,8 +145,13 @@ void POP3Store::connect()
if (mid.getLeft().length() && mid.getRight().length())
{
// <digest> is the result of MD5 applied to "<message-id>password"
- sendRequest("APOP " + auth.getUsername() + " "
- + utility::md5(mid.generate() + auth.getPassword()).hex());
+ ref <security::digest::messageDigest> md5 =
+ security::digest::messageDigestFactory::getInstance()->create("md5");
+
+ md5->update(mid.generate() + auth.getPassword());
+ md5->finalize();
+
+ sendRequest("APOP " + auth.getUsername() + " " + md5->getHexDigest());
readResponse(response, false);
if (isSuccessResponse(response))
diff --git a/src/utility/md5.cpp b/src/security/digest/md5/md5MessageDigest.cpp
index b5899e36..d5fa036c 100644
--- a/src/utility/md5.cpp
+++ b/src/security/digest/md5/md5MessageDigest.cpp
@@ -44,37 +44,28 @@
// These notices must be retained in any copies of any part of this
// documentation and/or software.
-#include "vmime/utility/md5.hpp"
+#include "vmime/security/digest/md5/md5MessageDigest.hpp"
namespace vmime {
-namespace utility {
+namespace security {
+namespace digest {
+namespace md5 {
-md5::md5()
- : m_finalized(false)
+md5MessageDigest::md5MessageDigest()
{
init();
}
-md5::md5(const vmime_uint8* const in, const unsigned long length)
- : m_finalized(false)
+void md5MessageDigest::reset()
{
init();
- update(in, length);
}
-md5::md5(const string& in)
- : m_finalized(false)
-{
- init();
- update(reinterpret_cast <const vmime_uint8*>(in.c_str()), in.length());
-}
-
-
-void md5::init()
+void md5MessageDigest::init()
{
m_hash[0] = 0x67452301;
m_hash[1] = 0xefcdab89;
@@ -82,6 +73,7 @@ void md5::init()
m_hash[3] = 0x10325476;
m_byteCount = 0;
+ m_finalized = false;
}
@@ -121,18 +113,29 @@ static inline void swapUint32Array(vmime_uint32* buf, unsigned long words)
}
-void md5::update(const string& in)
+void md5MessageDigest::update(const byte b)
{
- update(reinterpret_cast <const vmime_uint8*>(in.c_str()), in.length());
+ update(&b, 1);
}
-void md5::update(const vmime_uint8* data, unsigned long len)
+void md5MessageDigest::update(const string& s)
{
- if (m_finalized)
- return;
+ update(reinterpret_cast <const byte*>(s.data()), s.length());
+}
+
+void md5MessageDigest::update(const byte* data, const unsigned long offset,
+ const unsigned long len)
+{
+ update(data + offset, len);
+}
+
+
+void md5MessageDigest::update(const byte* data, const unsigned long length)
+{
const unsigned long avail = 64 - (m_byteCount & 0x3f);
+ unsigned long len = length;
m_byteCount += len;
@@ -161,7 +164,29 @@ void md5::update(const vmime_uint8* data, unsigned long len)
}
-void md5::finalize()
+void md5MessageDigest::finalize(const string& s)
+{
+ update(s);
+ finalize();
+}
+
+
+void md5MessageDigest::finalize(const byte* buffer, const unsigned long len)
+{
+ update(buffer, len);
+ finalize();
+}
+
+
+void md5MessageDigest::finalize(const byte* buffer,
+ const unsigned long offset, const unsigned long len)
+{
+ update(buffer, offset, len);
+ finalize();
+}
+
+
+void md5MessageDigest::finalize()
{
const long offset = m_byteCount & 0x3f;
@@ -197,7 +222,7 @@ void md5::finalize()
}
-void md5::transformHelper()
+void md5MessageDigest::transformHelper()
{
#if VMIME_BYTE_ORDER_BIG_ENDIAN
swapUint32Array((vmime_uint32*) m_block, 64 / 4);
@@ -206,7 +231,7 @@ void md5::transformHelper()
}
-void md5::transform()
+void md5MessageDigest::transform()
{
const vmime_uint32* const in = reinterpret_cast <vmime_uint32*>(m_block);
@@ -298,34 +323,20 @@ void md5::transform()
}
-const string md5::hex()
+const int md5MessageDigest::getDigestLength() const
{
- if (!m_finalized)
- finalize();
-
- static const unsigned char hex[] = "0123456789abcdef";
-
- std::ostringstream oss;
- const vmime_uint8* const digest = reinterpret_cast <vmime_uint8*>(m_hash);
-
- for (int i = 0 ; i < 16 ; ++i)
- {
- oss << hex[(digest[i] & 0xf0) >> 4];
- oss << hex[(digest[i] & 0x0f)];
- }
-
- return (oss.str());
+ return 16;
}
-const vmime_uint8* md5::hash()
+const byte* md5MessageDigest::getDigest() const
{
- if (!m_finalized)
- finalize();
-
- return (reinterpret_cast <const vmime_uint8*>(m_hash));
+ return reinterpret_cast <const byte*>(m_hash);
}
-} // utility
+} // md5
+} // digest
+} // security
} // vmime
+
diff --git a/vmime/utility/md5.hpp b/src/security/digest/messageDigest.cpp
index eaded4c9..8fb56543 100644
--- a/vmime/utility/md5.hpp
+++ b/src/security/digest/messageDigest.cpp
@@ -17,52 +17,37 @@
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
-#ifndef VMIME_UTILITY_MD5_HPP_INCLUDED
-#define VMIME_UTILITY_MD5_HPP_INCLUDED
+#include "vmime/security/digest/messageDigest.hpp"
-
-#include "vmime/base.hpp"
-#include "vmime/config.hpp"
+#include <sstream>
namespace vmime {
-namespace utility {
+namespace security {
+namespace digest {
-class md5
+const string messageDigest::getHexDigest() const
{
-public:
-
- md5();
- md5(const vmime_uint8* const in, const unsigned long length);
- md5(const string& in);
-
-public:
+ const byte* hash = getDigest();
+ const int len = getDigestLength();
- const string hex();
- const vmime_uint8* hash();
+ static const unsigned char hex[] = "0123456789abcdef";
- void update(const vmime_uint8* data, unsigned long len);
- void update(const string& in);
+ std::ostringstream oss;
-protected:
+ for (int i = 0 ; i < len ; ++i)
+ {
+ oss << hex[(hash[i] & 0xf0) >> 4];
+ oss << hex[(hash[i] & 0x0f)];
+ }
- void init();
- void transformHelper();
- void transform();
- void finalize();
+ return oss.str();
- vmime_uint32 m_hash[4];
+}
- unsigned long m_byteCount;
- vmime_uint8 m_block[64];
- bool m_finalized;
-};
-
-
-} // utility
+} // digest
+} // security
} // vmime
-
-#endif // VMIME_UTILITY_MD5_HPP_INCLUDED
diff --git a/src/security/digest/messageDigestFactory.cpp b/src/security/digest/messageDigestFactory.cpp
new file mode 100644
index 00000000..eccc545c
--- /dev/null
+++ b/src/security/digest/messageDigestFactory.cpp
@@ -0,0 +1,80 @@
+//
+// VMime library (http://www.vmime.org)
+// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+#include "vmime/security/digest/messageDigestFactory.hpp"
+#include "vmime/exception.hpp"
+
+#include "vmime/security/digest/md5/md5MessageDigest.hpp"
+#include "vmime/security/digest/sha1/sha1MessageDigest.hpp"
+
+
+namespace vmime {
+namespace security {
+namespace digest {
+
+
+messageDigestFactory::messageDigestFactory()
+{
+ registerAlgorithm <md5::md5MessageDigest>("md5");
+ registerAlgorithm <sha1::sha1MessageDigest>("sha1");
+}
+
+
+messageDigestFactory::~messageDigestFactory()
+{
+}
+
+
+messageDigestFactory* messageDigestFactory::getInstance()
+{
+ static messageDigestFactory instance;
+ return (&instance);
+}
+
+
+ref <messageDigest> messageDigestFactory::create(const string& name)
+{
+ const MapType::const_iterator it = m_algos.find
+ (utility::stringUtils::toLower(name));
+
+ if (it != m_algos.end())
+ return (*it).second->create();
+
+ throw exceptions::no_digest_algorithm_available(name);
+}
+
+
+const std::vector <string> messageDigestFactory::getSupportedAlgorithms() const
+{
+ std::vector <string> res;
+
+ for (MapType::const_iterator it = m_algos.begin() ;
+ it != m_algos.end() ; ++it)
+ {
+ res.push_back((*it).first);
+ }
+
+ return res;
+}
+
+
+} // digest
+} // security
+} // vmime
+
diff --git a/src/security/digest/sha1/sha1MessageDigest.cpp b/src/security/digest/sha1/sha1MessageDigest.cpp
new file mode 100644
index 00000000..de3f6948
--- /dev/null
+++ b/src/security/digest/sha1/sha1MessageDigest.cpp
@@ -0,0 +1,262 @@
+//
+// VMime library (http://www.vmime.org)
+// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+//
+// This is an implementation by Steve Reid <[email protected]>
+// 100% public domain.
+
+#include "vmime/security/digest/sha1/sha1MessageDigest.hpp"
+
+
+namespace vmime {
+namespace security {
+namespace digest {
+namespace sha1 {
+
+
+#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
+
+// blk0() and blk() perform the initial expand.
+// I got the idea of expanding during the round function from SSLeay
+#if VMIME_BYTE_ORDER_LITTLE_ENDIAN
+ #define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \
+ | (rol(block->l[i], 8) & 0x00FF00FF))
+#else
+ #define blk0(i) block->l[i]
+#endif
+
+#define blk(i) (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] \
+ ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))
+
+// (R0+R1), R2, R3, R4 are the different operations used in SHA1
+#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
+#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
+#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
+
+
+sha1MessageDigest::sha1MessageDigest()
+{
+ init();
+}
+
+
+void sha1MessageDigest::reset()
+{
+ init();
+}
+
+
+void sha1MessageDigest::init()
+{
+ m_state[0] = 0x67452301;
+ m_state[1] = 0xefcdab89;
+ m_state[2] = 0x98badcfe;
+ m_state[3] = 0x10325476;
+ m_state[4] = 0xc3d2e1f0;
+
+ m_count[0] = 0;
+ m_count[1] = 0;
+}
+
+
+void sha1MessageDigest::update(const byte b)
+{
+ update(&b, 1);
+}
+
+
+void sha1MessageDigest::update(const string& s)
+{
+ update(reinterpret_cast <const byte*>(s.data()), s.length());
+}
+
+
+void sha1MessageDigest::update(const byte* buffer, const unsigned long offset,
+ const unsigned long len)
+{
+ update(buffer + offset, len);
+}
+
+
+void sha1MessageDigest::update(const byte* buffer, const unsigned long len)
+{
+ unsigned int i, j;
+
+ j = (m_count[0] >> 3) & 63;
+
+ if ((m_count[0] += len << 3) < (len << 3))
+ m_count[1]++;
+
+ m_count[1] += (len >> 29);
+
+ if ((j + len) > 63)
+ {
+ memcpy(&m_buffer[j], buffer, (i = 64 - j));
+
+ transform(m_state, m_buffer);
+
+ for ( ; i + 63 < len ; i += 64)
+ transform(m_state, &buffer[i]);
+
+ j = 0;
+ }
+ else
+ {
+ i = 0;
+ }
+
+ memcpy(&m_buffer[j], &buffer[i], len - i);
+}
+
+
+void sha1MessageDigest::finalize()
+{
+ unsigned long i, j;
+ unsigned char finalcount[8];
+
+ for (i = 0 ; i < 8 ; i++)
+ {
+ finalcount[i] = static_cast <unsigned char>
+ ((m_count[(i >= 4 ? 0 : 1)]
+ >> ((3-(i & 3)) * 8) ) & 255); // Endian independent
+ }
+
+ update(reinterpret_cast <const byte*>("\200"), 1);
+
+ while ((m_count[0] & 504) != 448)
+ update(reinterpret_cast <const byte*>("\0"), 1);
+
+ update(finalcount, 8); // Should cause a transform()
+
+ for (i = 0 ; i < 20 ; i++)
+ {
+ m_digest[i] = static_cast <unsigned char>
+ ((m_state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
+ }
+
+ // Wipe variables
+ i = j = 0;
+
+ memset(m_buffer, 0, 64);
+ memset(m_state, 0, 20);
+ memset(m_count, 0, 8);
+ memset(&finalcount, 0, 8);
+}
+
+
+void sha1MessageDigest::finalize(const string& s)
+{
+ finalize(reinterpret_cast <const byte*>(s.data()), s.length());
+}
+
+
+void sha1MessageDigest::finalize(const byte* buffer, const unsigned long len)
+{
+ update(buffer, len);
+ finalize();
+}
+
+
+void sha1MessageDigest::finalize(const byte* buffer,
+ const unsigned long offset, const unsigned long len)
+{
+ finalize(buffer + offset, len);
+}
+
+
+/** Hash a single 512-bit block.
+ * This is the core of the algorithm.
+ */
+void sha1MessageDigest::transform
+ (unsigned long state[5], const unsigned char buffer[64])
+{
+ unsigned long a, b, c, d, e;
+
+ typedef union
+ {
+ unsigned char c[64];
+ unsigned long l[16];
+ } CHAR64LONG16;
+
+ CHAR64LONG16* block;
+ static unsigned char workspace[64];
+
+ block = reinterpret_cast <CHAR64LONG16*>(workspace);
+ memcpy(block, buffer, 64);
+
+ // Copy context->state[] to working vars
+ a = state[0];
+ b = state[1];
+ c = state[2];
+ d = state[3];
+ e = state[4];
+
+ // 4 rounds of 20 operations each. Loop unrolled.
+ R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
+ R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
+ R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
+ R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
+ R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
+ R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
+ R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
+ R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
+ R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
+ R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
+ R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
+ R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
+ R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
+ R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
+ R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
+ R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
+ R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
+ R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
+ R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
+ R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
+
+ // Add the working vars back into context.state[]
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+ state[4] += e;
+
+ // Wipe variables
+ a = b = c = d = e = 0;
+}
+
+
+const int sha1MessageDigest::getDigestLength() const
+{
+ return 20;
+}
+
+
+const byte* sha1MessageDigest::getDigest() const
+{
+ return m_digest;
+}
+
+
+} // sha1
+} // digest
+} // security
+} // vmime
+
+
diff --git a/tests/security/digest/md5Test.cpp b/tests/security/digest/md5Test.cpp
new file mode 100644
index 00000000..88c5490d
--- /dev/null
+++ b/tests/security/digest/md5Test.cpp
@@ -0,0 +1,229 @@
+//
+// VMime library (http://www.vmime.org)
+// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+#include "tests/testUtils.hpp"
+
+#include "vmime/security/digest/messageDigestFactory.hpp"
+
+
+#define VMIME_TEST_SUITE md5Test
+#define VMIME_TEST_SUITE_MODULE "Security/Digest"
+
+
+#define INIT_DIGEST(var, algo) \
+ vmime::ref <vmime::security::digest::messageDigest> var = \
+ vmime::security::digest::messageDigestFactory::getInstance()->create(algo)
+
+
+
+VMIME_TEST_SUITE_BEGIN
+
+ VMIME_TEST_LIST_BEGIN
+ VMIME_TEST(testRFC1321_1)
+ VMIME_TEST(testRFC1321_2)
+ VMIME_TEST(testRFC1321_3)
+ VMIME_TEST(testRFC1321_4)
+ VMIME_TEST(testRFC1321_5)
+ VMIME_TEST(testRFC1321_6)
+ VMIME_TEST(testRFC1321_7)
+ VMIME_TEST(testUpdate1)
+ VMIME_TEST(testUpdate2)
+ VMIME_TEST(testUpdate3)
+ VMIME_TEST(testUpdate4)
+ VMIME_TEST(testUpdate5)
+ VMIME_TEST(testUpdate6)
+ VMIME_TEST(testUpdate7)
+ VMIME_TEST_LIST_END
+
+
+ // Test suites from RFC #1321
+
+ void testRFC1321_1()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("");
+ algo->finalize();
+
+ VASSERT_EQ("*", "d41d8cd98f00b204e9800998ecf8427e", algo->getHexDigest());
+ }
+
+ void testRFC1321_2()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("a");
+ algo->finalize();
+
+ VASSERT_EQ("*", "0cc175b9c0f1b6a831c399e269772661", algo->getHexDigest());
+ }
+
+ void testRFC1321_3()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("abc");
+ algo->finalize();
+
+ VASSERT_EQ("*", "900150983cd24fb0d6963f7d28e17f72", algo->getHexDigest());
+ }
+
+ void testRFC1321_4()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("message digest");
+ algo->finalize();
+
+ VASSERT_EQ("*", "f96b697d7cb7938d525a2f31aaf161d0", algo->getHexDigest());
+ }
+
+ void testRFC1321_5()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("abcdefghijklmnopqrstuvwxyz");
+ algo->finalize();
+
+ VASSERT_EQ("*", "c3fcd3d76192e4007dfb496cca67e13b", algo->getHexDigest());
+ }
+
+ void testRFC1321_6()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
+ algo->finalize();
+
+ VASSERT_EQ("*", "d174ab98d277d9f5a5611c2c9f419d9f", algo->getHexDigest());
+ }
+
+ void testRFC1321_7()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
+ algo->finalize();
+
+ VASSERT_EQ("*", "57edf4a22be3c955ac49da2e2107b67a", algo->getHexDigest());
+ }
+
+ void testReset()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("foo");
+ algo->update("bar");
+ algo->finalize();
+
+ algo->reset();
+ algo->finalize();
+
+ VASSERT_EQ("*", "d41d8cd98f00b204e9800998ecf8427e", algo->getHexDigest()); // empty string
+ }
+
+ void testUpdate1()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("");
+ algo->finalize();
+
+ VASSERT_EQ("*", "d41d8cd98f00b204e9800998ecf8427e", algo->getHexDigest());
+ }
+
+ void testUpdate2()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("a");
+ algo->update("");
+ algo->finalize();
+
+ VASSERT_EQ("2", "0cc175b9c0f1b6a831c399e269772661", algo->getHexDigest());
+ }
+
+ void testUpdate3()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("ab");
+ algo->update("c");
+ algo->finalize();
+
+ VASSERT_EQ("3", "900150983cd24fb0d6963f7d28e17f72", algo->getHexDigest());
+ }
+
+ void testUpdate4()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("");
+ algo->update("message");
+ algo->update(" ");
+ algo->update("digest");
+ algo->finalize();
+
+ VASSERT_EQ("4", "f96b697d7cb7938d525a2f31aaf161d0", algo->getHexDigest());
+ }
+
+ void testUpdate5()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("abcd");
+ algo->update("");
+ algo->update("efghijklmnop");
+ algo->update("qrstuvwx");
+ algo->update("yz");
+ algo->finalize();
+
+ VASSERT_EQ("5", "c3fcd3d76192e4007dfb496cca67e13b", algo->getHexDigest());
+ }
+
+ void testUpdate6()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012");
+ algo->update("345");
+ algo->update("6");
+ algo->update("7");
+ algo->update("89");
+ algo->finalize();
+
+ VASSERT_EQ("6", "d174ab98d277d9f5a5611c2c9f419d9f", algo->getHexDigest());
+ }
+
+ void testUpdate7()
+ {
+ INIT_DIGEST(algo, "md5");
+
+ algo->update("12345678901234567890123456789");
+ algo->update("01234567890123456789012345678901");
+ algo->update("234567890123456789");
+ algo->update("");
+ algo->update("0");
+ algo->finalize();
+
+ VASSERT_EQ("7", "57edf4a22be3c955ac49da2e2107b67a", algo->getHexDigest());
+ }
+
+VMIME_TEST_SUITE_END
+
diff --git a/tests/security/digest/sha1Test.cpp b/tests/security/digest/sha1Test.cpp
new file mode 100644
index 00000000..88bd494b
--- /dev/null
+++ b/tests/security/digest/sha1Test.cpp
@@ -0,0 +1,119 @@
+//
+// VMime library (http://www.vmime.org)
+// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+#include "tests/testUtils.hpp"
+
+#include "vmime/security/digest/messageDigestFactory.hpp"
+
+
+#define VMIME_TEST_SUITE sha1Test
+#define VMIME_TEST_SUITE_MODULE "Security/Digest"
+
+
+#define INIT_DIGEST(var, algo) \
+ vmime::ref <vmime::security::digest::messageDigest> var = \
+ vmime::security::digest::messageDigestFactory::getInstance()->create(algo)
+
+
+
+VMIME_TEST_SUITE_BEGIN
+
+ VMIME_TEST_LIST_BEGIN
+ VMIME_TEST(testFIPS180_1)
+ VMIME_TEST(testFIPS180_2)
+ VMIME_TEST(testFIPS180_3)
+ VMIME_TEST(testReset)
+ VMIME_TEST(testUpdate)
+ VMIME_TEST_LIST_END
+
+
+ // Test suites from FIPS PUB 180-1
+ // http://www.itl.nist.gov/fipspubs/fip180-1.htm
+
+ void testFIPS180_1()
+ {
+ INIT_DIGEST(algo, "sha1");
+
+ algo->update("abc");
+ algo->finalize();
+
+ VASSERT_EQ("*", "a9993e364706816aba3e25717850c26c9cd0d89d", algo->getHexDigest());
+ }
+
+ void testFIPS180_2()
+ {
+ INIT_DIGEST(algo, "sha1");
+
+ algo->update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
+ algo->finalize();
+
+ VASSERT_EQ("*", "84983e441c3bd26ebaae4aa1f95129e5e54670f1", algo->getHexDigest());
+ }
+
+ void testFIPS180_3()
+ {
+ INIT_DIGEST(algo, "sha1");
+
+ vmime::byte* buffer = new vmime::byte[1000000];
+
+ for (int i = 0 ; i < 1000000 ; ++i)
+ buffer[i] = 'a';
+
+ algo->update(buffer, 1000000);
+ algo->finalize();
+
+ delete [] buffer;
+
+ VASSERT_EQ("*", "34aa973cd4c4daa4f61eeb2bdbad27316534016f", algo->getHexDigest());
+ }
+
+ void testReset()
+ {
+ INIT_DIGEST(algo, "sha1");
+
+ algo->update("ab");
+ algo->update("c");
+ algo->finalize();
+
+ algo->reset();
+ algo->finalize();
+
+ VASSERT_EQ("*", "da39a3ee5e6b4b0d3255bfef95601890afd80709", algo->getHexDigest()); // empty string
+ }
+
+ void testUpdate()
+ {
+ INIT_DIGEST(algo, "sha1");
+
+ algo->update("a");
+ algo->update("");
+ algo->update("bcdbcdecdefd");
+ algo->update("efgef");
+ algo->update("ghfghighijhijkijkljklmklmnlmnomnopnop");
+ algo->update("");
+ algo->update("q");
+ algo->update("");
+ algo->update("");
+ algo->finalize();
+
+ VASSERT_EQ("*", "84983e441c3bd26ebaae4aa1f95129e5e54670f1", algo->getHexDigest());
+ }
+
+VMIME_TEST_SUITE_END
+
diff --git a/tests/testRunner.cpp b/tests/testRunner.cpp
index 883869a0..2f4e41ec 100644
--- a/tests/testRunner.cpp
+++ b/tests/testRunner.cpp
@@ -21,6 +21,8 @@
#include <time.h>
#include <iostream>
+#include <vector>
+#include <algorithm>
#include <cppunit/XmlOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
@@ -165,6 +167,26 @@ private:
};
+
+// see testUtils.hpp
+
+std::vector <std::string>& getTestModules()
+{
+ static std::vector <std::string> allModules;
+ return allModules;
+}
+
+
+void registerTestModule(const char* name_)
+{
+ std::vector <std::string>& testModules = getTestModules();
+ std::string name(name_);
+
+ if (std::find(testModules.begin(), testModules.end(), name) == testModules.end())
+ testModules.push_back(name);
+}
+
+
int main(int argc, char* argv[])
{
// VMime initialization
@@ -186,9 +208,12 @@ int main(int argc, char* argv[])
{
// Get the test suites from the registry and add them to the list of test to run
CppUnit::TestRunner runner;
- runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("Parser").makeTest());
- runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("Utility").makeTest());
- runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("Misc").makeTest());
+
+ for (unsigned int i = 0 ; i < getTestModules().size() ; ++i)
+ {
+ runner.addTest(CppUnit::TestFactoryRegistry::
+ getRegistry(getTestModules()[i]).makeTest());
+ }
std::auto_ptr <XmlTestListener> xmlListener(new XmlTestListener);
diff --git a/tests/testUtils.hpp b/tests/testUtils.hpp
index cb4549e0..572f4602 100644
--- a/tests/testUtils.hpp
+++ b/tests/testUtils.hpp
@@ -21,6 +21,7 @@
#include <iostream>
#include <sstream>
#include <vector>
+#include <string>
// VMime
@@ -53,7 +54,15 @@
}; \
\
static CppUnit::AutoRegisterSuite <VMIME_TEST_SUITE>(autoRegisterRegistry1); \
- static CppUnit::AutoRegisterSuite <VMIME_TEST_SUITE>(autoRegisterRegistry2)(VMIME_TEST_SUITE_MODULE);
+ static CppUnit::AutoRegisterSuite <VMIME_TEST_SUITE>(autoRegisterRegistry2)(VMIME_TEST_SUITE_MODULE); \
+ extern void registerTestModule(const char* name); \
+ template <typename T> \
+ struct AutoRegisterModule { \
+ AutoRegisterModule() { \
+ registerTestModule(VMIME_TEST_SUITE_MODULE); \
+ } \
+ }; \
+ static AutoRegisterModule <VMIME_TEST_SUITE> autoRegisterModule;
#define VMIME_TEST_LIST_BEGIN CPPUNIT_TEST_SUITE(VMIME_TEST_SUITE);
#define VMIME_TEST_LIST_END CPPUNIT_TEST_SUITE_END();
diff --git a/tests/utility/md5Test.cpp b/tests/utility/md5Test.cpp
deleted file mode 100644
index a55fddf7..00000000
--- a/tests/utility/md5Test.cpp
+++ /dev/null
@@ -1,98 +0,0 @@
-//
-// VMime library (http://www.vmime.org)
-// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License as
-// published by the Free Software Foundation; either version 2 of
-// the License, or (at your option) any later version.
-//
-// This program 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.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-//
-
-#include "tests/testUtils.hpp"
-
-#include "vmime/utility/md5.hpp"
-
-
-#define VMIME_TEST_SUITE md5Test
-#define VMIME_TEST_SUITE_MODULE "Utility"
-
-
-VMIME_TEST_SUITE_BEGIN
-
- VMIME_TEST_LIST_BEGIN
- VMIME_TEST(testString)
- VMIME_TEST(testUpdate)
- VMIME_TEST_LIST_END
-
-
- void testString()
- {
- // Test suites from RFC #1321
- VASSERT_EQ("1", "d41d8cd98f00b204e9800998ecf8427e", vmime::utility::md5("").hex());
- VASSERT_EQ("2", "0cc175b9c0f1b6a831c399e269772661", vmime::utility::md5("a").hex());
- VASSERT_EQ("3", "900150983cd24fb0d6963f7d28e17f72", vmime::utility::md5("abc").hex());
- VASSERT_EQ("4", "f96b697d7cb7938d525a2f31aaf161d0", vmime::utility::md5("message digest").hex());
- VASSERT_EQ("5", "c3fcd3d76192e4007dfb496cca67e13b", vmime::utility::md5("abcdefghijklmnopqrstuvwxyz").hex());
- VASSERT_EQ("6", "d174ab98d277d9f5a5611c2c9f419d9f", vmime::utility::md5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789").hex());
- VASSERT_EQ("7", "57edf4a22be3c955ac49da2e2107b67a", vmime::utility::md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890").hex());
- }
-
- void testUpdate()
- {
- vmime::utility::md5 m1;
- m1.update("");
- VASSERT_EQ("1", "d41d8cd98f00b204e9800998ecf8427e", m1.hex());
-
- vmime::utility::md5 m2;
- m2.update("a");
- m2.update("");
- VASSERT_EQ("2", "0cc175b9c0f1b6a831c399e269772661", m2.hex());
-
- vmime::utility::md5 m3;
- m3.update("ab");
- m3.update("c");
- VASSERT_EQ("3", "900150983cd24fb0d6963f7d28e17f72", m3.hex());
-
- vmime::utility::md5 m4;
- m4.update("");
- m4.update("message");
- m4.update(" ");
- m4.update("digest");
- VASSERT_EQ("4", "f96b697d7cb7938d525a2f31aaf161d0", m4.hex());
-
- vmime::utility::md5 m5;
- m5.update("abcd");
- m5.update("");
- m5.update("efghijklmnop");
- m5.update("qrstuvwx");
- m5.update("yz");
- VASSERT_EQ("5", "c3fcd3d76192e4007dfb496cca67e13b", m5.hex());
-
- vmime::utility::md5 m6;
- m6.update("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012");
- m6.update("345");
- m6.update("6");
- m6.update("7");
- m6.update("89");
- VASSERT_EQ("6", "d174ab98d277d9f5a5611c2c9f419d9f", m6.hex());
-
- vmime::utility::md5 m7;
- m7.update("12345678901234567890123456789");
- m7.update("01234567890123456789012345678901");
- m7.update("234567890123456789");
- m7.update("");
- m7.update("0");
- VASSERT_EQ("7", "57edf4a22be3c955ac49da2e2107b67a", m7.hex());
- }
-
-VMIME_TEST_SUITE_END
-
diff --git a/vmime/exception.hpp b/vmime/exception.hpp
index 55d4c480..d7f6912d 100644
--- a/vmime/exception.hpp
+++ b/vmime/exception.hpp
@@ -112,6 +112,9 @@ public:
};
+/** No encoder has been found for the specified encoding name.
+ */
+
class no_encoder_available : public vmime::exception
{
public:
@@ -124,6 +127,21 @@ public:
};
+/** No algorithm has been found for the specified name.
+ */
+
+class no_digest_algorithm_available : public vmime::exception
+{
+public:
+
+ no_digest_algorithm_available(const string& name, const exception& other = NO_EXCEPTION);
+ ~no_digest_algorithm_available() throw();
+
+ exception* clone() const;
+ const char* name() const throw();
+};
+
+
class no_such_parameter : public vmime::exception
{
public:
diff --git a/vmime/object.hpp b/vmime/object.hpp
index 0ef6e199..8d3015f2 100644
--- a/vmime/object.hpp
+++ b/vmime/object.hpp
@@ -21,6 +21,9 @@
#define VMIME_OBJECT_HPP_INCLUDED
+#include "vmime/types.hpp"
+
+
#include <vector>
diff --git a/vmime/security/digest/md5/md5MessageDigest.hpp b/vmime/security/digest/md5/md5MessageDigest.hpp
new file mode 100644
index 00000000..e6a0224e
--- /dev/null
+++ b/vmime/security/digest/md5/md5MessageDigest.hpp
@@ -0,0 +1,76 @@
+//
+// VMime library (http://www.vmime.org)
+// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+#ifndef VMIME_SECURITY_DIGEST_MD5_MD5MESSAGEDIGEST_HPP_INCLUDED
+#define VMIME_SECURITY_DIGEST_MD5_MD5MESSAGEDIGEST_HPP_INCLUDED
+
+
+#include "vmime/security/digest/messageDigest.hpp"
+
+
+namespace vmime {
+namespace security {
+namespace digest {
+namespace md5 {
+
+
+class md5MessageDigest : public messageDigest
+{
+public:
+
+ md5MessageDigest();
+
+ void update(const byte b);
+ void update(const string& s);
+ void update(const byte* buffer, const unsigned long len);
+ void update(const byte* buffer, const unsigned long offset, const unsigned long len);
+
+ void finalize();
+ void finalize(const string& s);
+ void finalize(const byte* buffer, const unsigned long len);
+ void finalize(const byte* buffer, const unsigned long offset, const unsigned long len);
+
+ const int getDigestLength() const;
+ const byte* getDigest() const;
+
+ void reset();
+
+protected:
+
+ void init();
+ void transformHelper();
+ void transform();
+
+ vmime_uint32 m_hash[4];
+
+ unsigned long m_byteCount;
+ vmime_uint8 m_block[64];
+
+ bool m_finalized;
+};
+
+
+} // md5
+} // digest
+} // security
+} // vmime
+
+
+#endif // VMIME_SECURITY_DIGEST_MD5_MD5MESSAGEDIGEST_HPP_INCLUDED
+
diff --git a/vmime/security/digest/messageDigest.hpp b/vmime/security/digest/messageDigest.hpp
new file mode 100644
index 00000000..250a9992
--- /dev/null
+++ b/vmime/security/digest/messageDigest.hpp
@@ -0,0 +1,134 @@
+//
+// VMime library (http://www.vmime.org)
+// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+#ifndef VMIME_SECURITY_DIGEST_MESSAGEDIGEST_HPP_INCLUDED
+#define VMIME_SECURITY_DIGEST_MESSAGEDIGEST_HPP_INCLUDED
+
+
+#include "vmime/object.hpp"
+#include "vmime/types.hpp"
+
+
+namespace vmime {
+namespace security {
+namespace digest {
+
+
+/** Computes message digests using standard algorithms,
+ * such as MD5 or SHA.
+ */
+
+class messageDigest : public object
+{
+public:
+
+ /** Updates the digest using the specified string.
+ *
+ * @param s the string with which to update the digest.
+ */
+ virtual void update(const string& s) = 0;
+
+ /** Updates the digest using the specified byte.
+ *
+ * @param b the byte with which to update the digest.
+ */
+ virtual void update(const byte b) = 0;
+
+ /** Updates the digest using the specified array of bytes.
+ *
+ * @param buffer array of bytes
+ * @param len number of bytes to use in the buffer
+ */
+ virtual void update(const byte* buffer, const unsigned long len) = 0;
+
+ /** Updates the digest using the specified array of bytes,
+ * starting at the specified offset.
+ *
+ * @param buffer array of bytes
+ * @param offset offset to start from in the array of bytes
+ * @param len number of bytes to use, starting at offset
+ */
+ virtual void update(const byte* buffer,
+ const unsigned long offset,
+ const unsigned long len) = 0;
+
+ /** Completes the hash computation by performing final operations
+ * such as padding.
+ */
+ virtual void finalize() = 0;
+
+ /** Completes the hash computation by performing final operations
+ * such as padding. This is equivalent to calling update() and
+ * then finalize().
+ */
+ virtual void finalize(const string& s) = 0;
+
+ /** Completes the hash computation by performing final operations
+ * such as padding. This is equivalent to calling update() and
+ * then finalize().
+ */
+ virtual void finalize(const byte* buffer,
+ const unsigned long len) = 0;
+
+ /** Completes the hash computation by performing final operations
+ * such as padding. This is equivalent to calling update() and
+ * then finalize().
+ */
+ virtual void finalize(const byte* buffer,
+ const unsigned long offset,
+ const unsigned long len) = 0;
+
+ /** Returns the length of the hash.
+ * This is the length of the array returned by getDigest().
+ *
+ * @return length of computed hash
+ */
+ virtual const int getDigestLength() const = 0;
+
+ /** Returns the hash, as computed by the algorithm.
+ * You must call finalize() before using this function, or the
+ * hash will not be correct.
+ * To get the size of the returned array, call getDigestLength().
+ *
+ * @return computed hash
+ */
+ virtual const byte* getDigest() const = 0;
+
+ /** Returns the hash as an hexadecimal string.
+ * You must call finalize() before using this function, or the
+ * hash will not be correct.
+ *
+ * @return computed hash, in hexadecimal format
+ */
+ virtual const string getHexDigest() const;
+
+ /** Resets the algorithm to its initial state, so that you can
+ * compute a new hash using the same object.
+ */
+ virtual void reset() = 0;
+};
+
+
+} // digest
+} // security
+} // vmime
+
+
+#endif // VMIME_SECURITY_DIGEST_MESSAGEDIGEST_HPP_INCLUDED
+
diff --git a/vmime/security/digest/messageDigestFactory.hpp b/vmime/security/digest/messageDigestFactory.hpp
new file mode 100644
index 00000000..3216a467
--- /dev/null
+++ b/vmime/security/digest/messageDigestFactory.hpp
@@ -0,0 +1,108 @@
+//
+// VMime library (http://www.vmime.org)
+// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+#ifndef VMIME_SECURITY_DIGEST_MESSAGEDIGESTFACTORY_HPP_INCLUDED
+#define VMIME_SECURITY_DIGEST_MESSAGEDIGESTFACTORY_HPP_INCLUDED
+
+
+#include "vmime/types.hpp"
+#include "vmime/security/digest/messageDigest.hpp"
+#include "vmime/utility/stringUtils.hpp"
+
+
+namespace vmime {
+namespace security {
+namespace digest {
+
+
+/** Creates instances of message digest algorithms.
+ */
+
+class messageDigestFactory
+{
+private:
+
+ messageDigestFactory();
+ ~messageDigestFactory();
+
+public:
+
+ static messageDigestFactory* getInstance();
+
+private:
+
+ class digestAlgorithmFactory : public object
+ {
+ public:
+
+ virtual ref <messageDigest> create() const = 0;
+ };
+
+ template <class E>
+ class digestAlgorithmFactoryImpl : public digestAlgorithmFactory
+ {
+ public:
+
+ ref <messageDigest> create() const
+ {
+ return vmime::create <E>();
+ }
+ };
+
+
+ typedef std::map <std::string, ref <digestAlgorithmFactory> > MapType;
+ MapType m_algos;
+
+public:
+
+ /** Register a new digest algorithm by its name.
+ *
+ * @param name algorithm name
+ */
+ template <class E>
+ void registerAlgorithm(const string& name)
+ {
+ m_algos.insert(MapType::value_type(utility::stringUtils::toLower(name),
+ vmime::create <digestAlgorithmFactoryImpl <E> >()));
+ }
+
+ /** Create a new algorithm instance from its name.
+ *
+ * @param name algorithm name (eg. "md5")
+ * @return a new algorithm instance for the specified name
+ * @throw exceptions::no_digest_algorithm_available if no algorithm is
+ * registered with this name
+ */
+ ref <messageDigest> create(const string& name);
+
+ /** Return a list of supported digest algorithms.
+ *
+ * @return list of supported digest algorithms
+ */
+ const std::vector <string> getSupportedAlgorithms() const;
+};
+
+
+} // digest
+} // security
+} // vmime
+
+
+#endif // VMIME_SECURITY_DIGEST_MESSAGEDIGESTFACTORY_HPP_INCLUDED
+
diff --git a/vmime/security/digest/sha1/sha1MessageDigest.hpp b/vmime/security/digest/sha1/sha1MessageDigest.hpp
new file mode 100644
index 00000000..f182ede8
--- /dev/null
+++ b/vmime/security/digest/sha1/sha1MessageDigest.hpp
@@ -0,0 +1,75 @@
+//
+// VMime library (http://www.vmime.org)
+// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program 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.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+//
+
+#ifndef VMIME_SECURITY_DIGEST_SHA1_SHA1MESSAGEDIGEST_HPP_INCLUDED
+#define VMIME_SECURITY_DIGEST_SHA1_SHA1MESSAGEDIGEST_HPP_INCLUDED
+
+
+#include "vmime/security/digest/messageDigest.hpp"
+
+
+namespace vmime {
+namespace security {
+namespace digest {
+namespace sha1 {
+
+
+class sha1MessageDigest : public messageDigest
+{
+public:
+
+ sha1MessageDigest();
+
+ void update(const byte b);
+ void update(const string& s);
+ void update(const byte* buffer, const unsigned long len);
+ void update(const byte* buffer, const unsigned long offset, const unsigned long len);
+
+ void finalize();
+ void finalize(const string& s);
+ void finalize(const byte* buffer, const unsigned long len);
+ void finalize(const byte* buffer, const unsigned long offset, const unsigned long len);
+
+ const int getDigestLength() const;
+ const byte* getDigest() const;
+
+ void reset();
+
+protected:
+
+ void init();
+
+ static void transform(unsigned long state[5], const byte buffer[64]);
+
+ unsigned long m_state[5];
+ unsigned long m_count[2];
+ byte m_buffer[64];
+
+ byte m_digest[20];
+};
+
+
+} // sha1
+} // digest
+} // security
+} // vmime
+
+
+#endif // VMIME_SECURITY_DIGEST_SHA1_SHA1MESSAGEDIGEST_HPP_INCLUDED
+
diff --git a/vmime/types.hpp b/vmime/types.hpp
index 38485f9a..4e2fa923 100644
--- a/vmime/types.hpp
+++ b/vmime/types.hpp
@@ -39,6 +39,8 @@ namespace vmime
typedef int char_t;
+ typedef vmime_uint8 byte;
+
// Some aliases
namespace utils = utility;