diff options
Diffstat (limited to 'src/Common/Hash.cpp')
-rw-r--r-- | src/Common/Hash.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
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 <matthias@gamezock.de> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include "Hash.h" + +#include <boost/scoped_array.hpp> + +#include <mhash.h> + +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<boost::uint8_t> Hash::hash(const std::vector<boost::uint8_t> &in, unsigned int method) throw (Core::Exception) { + MHASH mh; + + mh = mhash_init(static_cast<hashid>(method)); + if(mh == MHASH_FAILED) + throw(Core::Exception(Core::Exception::NOT_AVAILABLE)); + + boost::scoped_array<boost::uint8_t> 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<hashid>(method)); + + boost::scoped_array<boost::uint8_t> outArray(new boost::uint8_t[hashLength]); + mhash_deinit(mh, outArray.get()); + + return std::vector<boost::uint8_t>(outArray.get(), outArray.get()+hashLength); +} + +} +} |