diff options
Diffstat (limited to '')
-rw-r--r-- | src/security/digest/messageDigest.cpp (renamed from vmime/utility/md5.hpp) | 51 | ||||
-rw-r--r-- | vmime/exception.hpp | 18 | ||||
-rw-r--r-- | vmime/object.hpp | 3 | ||||
-rw-r--r-- | vmime/security/digest/md5/md5MessageDigest.hpp | 76 | ||||
-rw-r--r-- | vmime/security/digest/messageDigest.hpp | 134 | ||||
-rw-r--r-- | vmime/security/digest/messageDigestFactory.hpp | 108 | ||||
-rw-r--r-- | vmime/security/digest/sha1/sha1MessageDigest.hpp | 75 | ||||
-rw-r--r-- | vmime/types.hpp | 2 |
8 files changed, 434 insertions, 33 deletions
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/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; |