diff options
-rw-r--r-- | Swiften/StringCodecs/SHA1.cpp | 15 | ||||
-rw-r--r-- | Swiften/StringCodecs/SHA1.h | 1 | ||||
-rw-r--r-- | Swiften/StringCodecs/UnitTest/SHA1Test.cpp | 10 |
3 files changed, 24 insertions, 2 deletions
diff --git a/Swiften/StringCodecs/SHA1.cpp b/Swiften/StringCodecs/SHA1.cpp index 367f3c9..18fde71 100644 --- a/Swiften/StringCodecs/SHA1.cpp +++ b/Swiften/StringCodecs/SHA1.cpp @@ -1,3 +1,7 @@ +#include <sstream> +#include <iomanip> +#include <boost/numeric/conversion/cast.hpp> + #include "Swiften/Base/Platform.h" #pragma GCC diagnostic ignored "-Wold-style-cast" @@ -193,4 +197,15 @@ ByteArray SHA1::getBinaryHash(const ByteArray& input) { return digest; } +String SHA1::getHexHash(const ByteArray& input) { + ByteArray digest = getBinaryHash(input); + std::ostringstream result; + result << std::hex; + + for (unsigned int i = 0; i < digest.getSize(); ++i) { + result << std::setw(2) << std::setfill('0') << boost::numeric_cast<unsigned int>(static_cast<unsigned char>(digest[i])); + } + return String(result.str()); +} + } diff --git a/Swiften/StringCodecs/SHA1.h b/Swiften/StringCodecs/SHA1.h index 7d432ac..0b0f434 100644 --- a/Swiften/StringCodecs/SHA1.h +++ b/Swiften/StringCodecs/SHA1.h @@ -7,6 +7,7 @@ namespace Swift { class SHA1 { public: static ByteArray getBinaryHash(const ByteArray& data); + static String getHexHash(const ByteArray& data); }; } diff --git a/Swiften/StringCodecs/UnitTest/SHA1Test.cpp b/Swiften/StringCodecs/UnitTest/SHA1Test.cpp index 16e96ad..a6c7a72 100644 --- a/Swiften/StringCodecs/UnitTest/SHA1Test.cpp +++ b/Swiften/StringCodecs/UnitTest/SHA1Test.cpp @@ -8,16 +8,22 @@ using namespace Swift; class SHA1Test : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(SHA1Test); - CPPUNIT_TEST(testEncode); + CPPUNIT_TEST(testGetBinaryHash); + CPPUNIT_TEST(testGetHexHash); CPPUNIT_TEST_SUITE_END(); public: SHA1Test() {} - void testEncode() { + void testGetBinaryHash() { ByteArray result(SHA1::getBinaryHash("client/pc//Exodus 0.9.1<http://jabber.org/protocol/caps<http://jabber.org/protocol/disco#info<http://jabber.org/protocol/disco#items<http://jabber.org/protocol/muc<")); CPPUNIT_ASSERT_EQUAL(ByteArray("\x42\x06\xb2\x3c\xa6\xb0\xa6\x43\xd2\x0d\x89\xb0\x4f\xf5\x8c\xf7\x8b\x80\x96\xed"), result); } + + void testGetHexHash() { + String result(SHA1::getHexHash("client/pc//Exodus 0.9.1<http://jabber.org/protocol/caps<http://jabber.org/protocol/disco#info<http://jabber.org/protocol/disco#items<http://jabber.org/protocol/muc<")); + CPPUNIT_ASSERT_EQUAL(String("4206b23ca6b0a643d20d89b04ff58cf78b8096ed"), result); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(SHA1Test); |