summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/StringCodecs')
-rw-r--r--Swiften/StringCodecs/Hexify.cpp18
-rw-r--r--Swiften/StringCodecs/Hexify.h1
-rw-r--r--Swiften/StringCodecs/MD5.cpp21
-rw-r--r--Swiften/StringCodecs/MD5.h11
-rw-r--r--Swiften/StringCodecs/UnitTest/HexifyTest.cpp6
-rw-r--r--Swiften/StringCodecs/UnitTest/MD5Test.cpp11
6 files changed, 68 insertions, 0 deletions
diff --git a/Swiften/StringCodecs/Hexify.cpp b/Swiften/StringCodecs/Hexify.cpp
index 367743c..668079b 100644
--- a/Swiften/StringCodecs/Hexify.cpp
+++ b/Swiften/StringCodecs/Hexify.cpp
@@ -31,4 +31,22 @@ std::string Hexify::hexify(const ByteArray& data) {
return std::string(result.str());
}
+ByteArray Hexify::unhexify(const std::string& hexstring) {
+ if (hexstring.size() % 2) {
+ return ByteArray();
+ }
+ ByteArray result = ByteArray(hexstring.size() / 2);
+ for (size_t pos = 0; pos < hexstring.size() - 1; pos += 2) {
+ char c;
+ c = hexstring[pos];
+ int a = (c>='0'&&c<='9') ? c-'0' : (c>='A'&&c<='Z') ? c-'A' + 10 : (c>='a'&&c<='z') ? c-'a' + 10 : -1;
+ c = hexstring[pos+1];
+ int b = (c>='0'&&c<='9') ? c-'0' : (c>='A'&&c<='Z') ? c-'A' + 10 : (c>='a'&&c<='z') ? c-'a' + 10 : -1;
+ if (a == -1 || b == -1) return ByteArray(); // fail
+ result[pos/2] = (a<<4) | b;
+
+ }
+ return result;
+}
+
}
diff --git a/Swiften/StringCodecs/Hexify.h b/Swiften/StringCodecs/Hexify.h
index 9815e21..c016448 100644
--- a/Swiften/StringCodecs/Hexify.h
+++ b/Swiften/StringCodecs/Hexify.h
@@ -13,5 +13,6 @@ namespace Swift {
public:
static std::string hexify(unsigned char byte);
static std::string hexify(const ByteArray& data);
+ static ByteArray unhexify(const std::string& hexstring);
};
}
diff --git a/Swiften/StringCodecs/MD5.cpp b/Swiften/StringCodecs/MD5.cpp
index 0d36254..6871f79 100644
--- a/Swiften/StringCodecs/MD5.cpp
+++ b/Swiften/StringCodecs/MD5.cpp
@@ -366,6 +366,27 @@ namespace {
}
}
+MD5::MD5() {
+ state = new md5_state_t;
+ md5_init(state);
+}
+
+MD5::~MD5() {
+ delete state;
+}
+
+MD5& MD5::update(const std::vector<unsigned char>& input) {
+ md5_append(state, reinterpret_cast<const md5_byte_t*>(vecptr(input)), input.size());
+ return *this;
+}
+
+std::vector<unsigned char> MD5::getHash() {
+ ByteArray digest;
+ digest.resize(16);
+ md5_finish(state, reinterpret_cast<md5_byte_t*>(vecptr(digest)));
+ return digest;
+}
+
ByteArray MD5::getHash(const ByteArray& data) {
return getMD5Hash(data);
}
diff --git a/Swiften/StringCodecs/MD5.h b/Swiften/StringCodecs/MD5.h
index b1d610c..09473c2 100644
--- a/Swiften/StringCodecs/MD5.h
+++ b/Swiften/StringCodecs/MD5.h
@@ -10,9 +10,20 @@
#include <Swiften/Base/SafeByteArray.h>
namespace Swift {
+ struct md5_state_s;
+
class MD5 {
public:
+ MD5();
+ ~MD5();
+
+ MD5& update(const std::vector<unsigned char>& data);
+ std::vector<unsigned char> getHash();
+
static ByteArray getHash(const ByteArray& data);
static ByteArray getHash(const SafeByteArray& data);
+
+ private:
+ md5_state_s* state;
};
}
diff --git a/Swiften/StringCodecs/UnitTest/HexifyTest.cpp b/Swiften/StringCodecs/UnitTest/HexifyTest.cpp
index 9cbd0d4..38233f9 100644
--- a/Swiften/StringCodecs/UnitTest/HexifyTest.cpp
+++ b/Swiften/StringCodecs/UnitTest/HexifyTest.cpp
@@ -17,6 +17,7 @@ class HexifyTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(HexifyTest);
CPPUNIT_TEST(testHexify);
CPPUNIT_TEST(testHexify_Byte);
+ CPPUNIT_TEST(testUnhexify);
CPPUNIT_TEST_SUITE_END();
public:
@@ -27,6 +28,11 @@ class HexifyTest : public CppUnit::TestFixture {
void testHexify_Byte() {
CPPUNIT_ASSERT_EQUAL(std::string("b2"), Hexify::hexify(0xb2));
}
+
+ void testUnhexify() {
+ CPPUNIT_ASSERT_EQUAL(std::string("ffaf02"), Hexify::hexify(Hexify::unhexify("ffaf02")));
+ CPPUNIT_ASSERT(createByteArray("\x01\x23\xf2", 3) == Hexify::unhexify("0123f2"));
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(HexifyTest);
diff --git a/Swiften/StringCodecs/UnitTest/MD5Test.cpp b/Swiften/StringCodecs/UnitTest/MD5Test.cpp
index ce7e422..c62c46a 100644
--- a/Swiften/StringCodecs/UnitTest/MD5Test.cpp
+++ b/Swiften/StringCodecs/UnitTest/MD5Test.cpp
@@ -19,6 +19,7 @@ class MD5Test : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(MD5Test);
CPPUNIT_TEST(testGetHash_Empty);
CPPUNIT_TEST(testGetHash_Alphabet);
+ CPPUNIT_TEST(testIncrementalTest);
CPPUNIT_TEST_SUITE_END();
public:
@@ -33,6 +34,16 @@ class MD5Test : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(createByteArray("\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d\x9f", 16), result);
}
+
+ void testIncrementalTest() {
+ MD5 testling;
+ testling.update(createByteArray("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
+ testling.update(createByteArray("abcdefghijklmnopqrstuvwxyz0123456789"));
+
+ ByteArray result = testling.getHash();
+
+ CPPUNIT_ASSERT_EQUAL(createByteArray("\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d\x9f", 16), result);
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(MD5Test);