summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Swiften/StringCodecs/PBKDF2.cpp20
-rw-r--r--Swiften/StringCodecs/PBKDF2.h10
-rw-r--r--Swiften/StringCodecs/UnitTest/PBKDF2Test.cpp36
3 files changed, 66 insertions, 0 deletions
diff --git a/Swiften/StringCodecs/PBKDF2.cpp b/Swiften/StringCodecs/PBKDF2.cpp
new file mode 100644
index 0000000..c09c10e
--- /dev/null
+++ b/Swiften/StringCodecs/PBKDF2.cpp
@@ -0,0 +1,20 @@
+#include "Swiften/StringCodecs/PBKDF2.h"
+#include "Swiften/StringCodecs/HMACSHA1.h"
+
+namespace Swift {
+
+ByteArray PBKDF2::encode(const ByteArray& password, const ByteArray& salt, int iterations) {
+ ByteArray u = HMACSHA1::getResult(password, salt + ByteArray("\0\0\0\1", 4));
+ ByteArray result = u;
+ int i = 1;
+ while (i < iterations) {
+ u = HMACSHA1::getResult(password, u);
+ for (unsigned int j = 0; j < u.getSize(); ++j) {
+ result[j] ^= u[j];
+ }
+ ++i;
+ }
+ return result;
+}
+
+}
diff --git a/Swiften/StringCodecs/PBKDF2.h b/Swiften/StringCodecs/PBKDF2.h
new file mode 100644
index 0000000..e87380d
--- /dev/null
+++ b/Swiften/StringCodecs/PBKDF2.h
@@ -0,0 +1,10 @@
+#pragma once
+
+#include "Swiften/Base/ByteArray.h"
+
+namespace Swift {
+ class PBKDF2 {
+ public:
+ static ByteArray encode(const ByteArray& password, const ByteArray& salt, int iterations);
+ };
+}
diff --git a/Swiften/StringCodecs/UnitTest/PBKDF2Test.cpp b/Swiften/StringCodecs/UnitTest/PBKDF2Test.cpp
new file mode 100644
index 0000000..92f4fe9
--- /dev/null
+++ b/Swiften/StringCodecs/UnitTest/PBKDF2Test.cpp
@@ -0,0 +1,36 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swiften/Base/ByteArray.h"
+#include "Swiften/StringCodecs/PBKDF2.h"
+
+using namespace Swift;
+
+class PBKDF2Test : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(PBKDF2Test);
+ CPPUNIT_TEST(testGetResult_I1);
+ CPPUNIT_TEST(testGetResult_I2);
+ CPPUNIT_TEST(testGetResult_I4096);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void testGetResult_I1() {
+ ByteArray result(PBKDF2::encode("password", "salt", 1));
+
+ CPPUNIT_ASSERT_EQUAL(ByteArray("\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6"), result);
+ }
+
+ void testGetResult_I2() {
+ ByteArray result(PBKDF2::encode("password", "salt", 2));
+
+ CPPUNIT_ASSERT_EQUAL(ByteArray("\xea\x6c\x1\x4d\xc7\x2d\x6f\x8c\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57"), result);
+ }
+
+ void testGetResult_I4096() {
+ ByteArray result(PBKDF2::encode("password", "salt", 4096));
+
+ CPPUNIT_ASSERT_EQUAL(ByteArray("\x4b\x00\x79\x1\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1", 20), result);
+ }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(PBKDF2Test);