From 84a5ceeb7db03d75425d72e8a23a0bb0f267bc01 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 24 Aug 2009 03:06:32 +0200 Subject: =?UTF-8?q?Hash-Klasse=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/AuthBackend.h | 2 +- src/Common/AuthManager.cpp | 4 +- src/Common/AuthManager.h | 4 +- src/Common/CMakeLists.txt | 3 +- src/Common/Hash.cpp | 65 +++++++++++++++++++++++++++++++++ src/Common/Hash.h | 91 ++++++++++++++++++++++++++++++++++++++++++++++ src/Common/UserManager.cpp | 20 +++++----- 7 files changed, 173 insertions(+), 16 deletions(-) create mode 100644 src/Common/Hash.cpp create mode 100644 src/Common/Hash.h (limited to 'src/Common') diff --git a/src/Common/AuthBackend.h b/src/Common/AuthBackend.h index e933856..53c7769 100644 --- a/src/Common/AuthBackend.h +++ b/src/Common/AuthBackend.h @@ -40,7 +40,7 @@ class AuthBackend { virtual const std::vector& getMethods() const = 0; virtual boost::shared_ptr authenticate(const std::string& /*method*/, const std::string& /*user*/, - const std::vector& /*challenge*/, std::vector& /*response*/, + const std::vector& /*data*/, std::vector& /*response*/, boost::shared_ptr /*context*/) throw(Core::Exception) = 0; public: diff --git a/src/Common/AuthManager.cpp b/src/Common/AuthManager.cpp index b27c29f..ffb8453 100644 --- a/src/Common/AuthManager.cpp +++ b/src/Common/AuthManager.cpp @@ -45,13 +45,13 @@ std::vector AuthManager::getMethods() { return backend->getMethods(); } -boost::shared_ptr AuthManager::authenticate(const std::string &method, const std::string &user, const std::vector &challenge, +boost::shared_ptr AuthManager::authenticate(const std::string &method, const std::string &user, const std::vector &data, std::vector &response, boost::shared_ptr context) throw(Core::Exception) { boost::lock_guard lock(mutex); response.clear(); - return backend->authenticate(method, user, challenge, response, context); + return backend->authenticate(method, user, data, response, context); } } diff --git a/src/Common/AuthManager.h b/src/Common/AuthManager.h index c773214..1516526 100644 --- a/src/Common/AuthManager.h +++ b/src/Common/AuthManager.h @@ -54,7 +54,7 @@ class MAD_COMMON_EXPORT AuthManager : private boost::noncopyable { } virtual boost::shared_ptr authenticate(const std::string& /*method*/, const std::string& /*user*/, - const std::vector& /*challenge*/, std::vector& /*response*/, + const std::vector& /*data*/, std::vector& /*response*/, boost::shared_ptr /*context*/) throw(Core::Exception) { throw Core::Exception(Core::Exception::NOT_IMPLEMENTED); } @@ -74,7 +74,7 @@ class MAD_COMMON_EXPORT AuthManager : private boost::noncopyable { std::vector getMethods(); - boost::shared_ptr authenticate(const std::string &method, const std::string &user, const std::vector &challenge, + boost::shared_ptr authenticate(const std::string &method, const std::string &user, const std::vector &data, std::vector &response, boost::shared_ptr context = boost::shared_ptr()) throw(Core::Exception); }; diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index 13e1599..129c7de 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -25,6 +25,7 @@ mad_library(Common ClientConnection.cpp ClientConnection.h Connection.cpp Connection.h GroupInfo.h + Hash.cpp Hash.h HostInfo.h Module.h ModuleManager.cpp ModuleManager.h @@ -41,4 +42,4 @@ mad_library(Common UserManager.cpp UserManager.h XmlPacket.cpp XmlPacket.h ) -target_link_libraries(Common Net Core modules ${LIBXML2_LIBRARIES} ${DL_LIBRARY}) +target_link_libraries(Common Net Core modules ${LIBXML2_LIBRARIES} ${MHASH_LIBRARY} ${DL_LIBRARY}) diff --git a/src/Common/Hash.cpp b/src/Common/Hash.cpp new file mode 100644 index 0000000..8194ffb --- /dev/null +++ b/src/Common/Hash.cpp @@ -0,0 +1,65 @@ +/* + * Hash.cpp + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see . + */ + +#include "Hash.h" + +#include + +#include + +namespace Mad { +namespace Common { + +const Hash::Hashes Hash::hashes; + +Hash::Hashes::Hashes() { + addHash("SHA256", MHASH_SHA256); + addHash("Tiger192", MHASH_TIGER192); + addHash("HAVAL192", MHASH_HAVAL192); + addHash("SHA1", MHASH_SHA1); + addHash("Tiger160", MHASH_TIGER160); + addHash("HAVAL160", MHASH_HAVAL160); + addHash("RIPEMD-160", MHASH_RIPEMD160); + addHash("MD5", MHASH_MD5); + addHash("Tiger128", MHASH_TIGER128); + addHash("HAVAL128", MHASH_HAVAL128); +} + +std::vector Hash::hash(const std::vector &in, unsigned int method) throw (Core::Exception) { + MHASH mh; + + mh = mhash_init(static_cast(method)); + if(mh == MHASH_FAILED) + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); + + boost::scoped_array inArray(new boost::uint8_t[in.size()]); + std::copy(in.begin(), in.end(), inArray.get()); + + mhash(mh, inArray.get(), in.size()); + + std::size_t hashLength = mhash_get_block_size(static_cast(method)); + + boost::scoped_array outArray(new boost::uint8_t[hashLength]); + mhash_deinit(mh, outArray.get()); + + return std::vector(outArray.get(), outArray.get()+hashLength); +} + +} +} diff --git a/src/Common/Hash.h b/src/Common/Hash.h new file mode 100644 index 0000000..69b3318 --- /dev/null +++ b/src/Common/Hash.h @@ -0,0 +1,91 @@ +/* + * Hash.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_COMMON_HASH_H_ +#define MAD_COMMON_HASH_H_ + +#include "export.h" + +#include + +#include +#include +#include + +#include + +namespace Mad { +namespace Common { + +class MAD_COMMON_EXPORT Hash { + private: + Hash(); + + class MAD_COMMON_EXPORT Hashes { + private: + std::map map; + std::vector list; + + void addHash(const std::string &name, unsigned id) { + map.insert(std::make_pair(name, id)); + list.push_back(name); + } + + public: + const std::vector& getList() const { + return list; + } + + const std::map& getMap() const { + return map; + } + + Hashes(); + }; + + static const Hashes hashes; + + public: + static const std::vector& getHashList() { + return hashes.getList(); + } + + static std::vector hash(const std::vector &in, unsigned int method) throw (Core::Exception); + + static std::vector hash(const std::vector &in, const std::string &method) throw (Core::Exception) { + std::map::const_iterator methodIt = hashes.getMap().find(method); + if(methodIt == hashes.getMap().end()) + throw(Core::Exception(Core::Exception::NOT_IMPLEMENTED)); + + return hash(in, methodIt->second); + } + + static std::vector hash(const std::string &in, unsigned int method) throw (Core::Exception) { + return hash(std::vector(in.begin(), in.end()), method); + } + + static std::vector hash(const std::string &in, const std::string &method) throw (Core::Exception) { + return hash(std::vector(in.begin(), in.end()), method); + } +}; + +} +} + +#endif /* MAD_COMMON_HASH_H_ */ diff --git a/src/Common/UserManager.cpp b/src/Common/UserManager.cpp index d4467ee..d184044 100644 --- a/src/Common/UserManager.cpp +++ b/src/Common/UserManager.cpp @@ -234,7 +234,7 @@ void UserManager::addUser(const UserInfo &userInfo) throw(Core::Exception) { (*configBackend)->addUser(userInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -258,7 +258,7 @@ void UserManager::updateUser(unsigned long uid, const UserInfo &userInfo) throw( (*configBackend)->updateUser(*oldUserInfo, userInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -280,7 +280,7 @@ void UserManager::deleteUser(unsigned long uid) throw(Core::Exception) { (*configBackend)->deleteUser(*userInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -317,7 +317,7 @@ void UserManager::addGroup(const GroupInfo &groupInfo) throw(Core::Exception) { (*configBackend)->addGroup(groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -341,7 +341,7 @@ void UserManager::updateGroup(unsigned long gid, const GroupInfo &groupInfo) thr (*configBackend)->updateGroup(*oldGroupInfo, groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -363,7 +363,7 @@ void UserManager::deleteGroup(unsigned long gid) throw(Core::Exception) { (*configBackend)->deleteGroup(*groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -388,7 +388,7 @@ void UserManager::addUserToGroup(unsigned long uid, unsigned long gid) throw(Cor (*configBackend)->addUserToGroup(*userInfo, *groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -413,7 +413,7 @@ void UserManager::deleteUserFromGroup(unsigned long uid, unsigned long gid) thro (*configBackend)->deleteUserFromGroup(*userInfo, *groupInfo); } catch(Core::Exception e) { - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } } @@ -439,7 +439,7 @@ void UserManager::setPassword(unsigned long uid, const std::string &password) th exception = e; if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } for(std::set >::iterator configBackend = configBackends.begin(); configBackend != configBackends.end(); ++configBackend) { @@ -449,7 +449,7 @@ void UserManager::setPassword(unsigned long uid, const std::string &password) th } catch(Core::Exception e) { if(e.getErrorCode() != Core::Exception::NOT_IMPLEMENTED) - application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.strerror()); + application->log(Core::LoggerBase::LOG_USER, Core::LoggerBase::LOG_WARNING, e.what()); } } -- cgit v1.2.3