/* * 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); } } }