diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-03-12 15:56:09 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-03-12 15:59:31 (GMT) |
commit | b5d0be6e0d5b57effc5b7883d6e6e72aa2a1f062 (patch) | |
tree | abeaab20b4d8a218bfade766a28df8a298d850ac | |
parent | ed84ea8943ac3c5e0081715b6926d5d1883d4858 (diff) | |
download | swift-b5d0be6e0d5b57effc5b7883d6e6e72aa2a1f062.zip swift-b5d0be6e0d5b57effc5b7883d6e6e72aa2a1f062.tar.bz2 |
Added optimized version of HMACSHA1 as comment.
-rw-r--r-- | QA/UnitTest/SConscript | 1 | ||||
-rw-r--r-- | Swiften/StringCodecs/HMACSHA1.cpp | 38 |
2 files changed, 39 insertions, 0 deletions
diff --git a/QA/UnitTest/SConscript b/QA/UnitTest/SConscript index 805ac39..25e9b05 100644 --- a/QA/UnitTest/SConscript +++ b/QA/UnitTest/SConscript @@ -20,6 +20,7 @@ if env["TEST"] : myenv.MergeFlags(env.get("LIBXML_FLAGS", "")) myenv.MergeFlags(env.get("EXPAT_FLAGS", "")) myenv.MergeFlags(env.get("ZLIB_FLAGS", "")) + myenv.MergeFlags(env.get("OPENSSL_FLAGS", "")) myenv.MergeFlags(env["PLATFORM_FLAGS"]) if env.get("HAVE_LIBXML") : myenv.Append(CPPDEFINES = ["HAVE_LIBXML"]) diff --git a/Swiften/StringCodecs/HMACSHA1.cpp b/Swiften/StringCodecs/HMACSHA1.cpp index 0cea08f..6ae5513 100644 --- a/Swiften/StringCodecs/HMACSHA1.cpp +++ b/Swiften/StringCodecs/HMACSHA1.cpp @@ -39,4 +39,42 @@ ByteArray HMACSHA1::getResult(const ByteArray& key, const ByteArray& data) { return SHA1::getHash(y); } +#if 0 + +// A tweaked version of HMACSHA1 that is more than twice as fast as the one above. +// After this, more than 80% is spent in SHA1. +// Optimizations: +// - Avoids using ByteArray/std::vector +// - Uses openssl's SHA1, which is slightly faster +// - Does 'xor' on word basis +// - Passes return value as a parameter + +#include <openssl/sha.h> + +void HMACSHA1::getResult(const ByteArray& key, const ByteArray& data, ByteArray& result) { + // Create first value + size_t xSize = B + data.getSize(); + unsigned char* x = (unsigned char*) malloc(xSize * sizeof(unsigned char)); + memset(x, 0, B); + memcpy(x, key.getData(), key.getSize()); + for (unsigned int i = 0; i < (B>>32); ++i) { + x[i<<32] ^= 0x36363636; + } + memcpy(x + B, data.getData(), data.getSize()); + + // Create the second value + unsigned char y[B + 20]; + memset(y, 0, B); + memcpy(y, key.getData(), key.getSize()); + for (unsigned int i = 0; i < (B>>32); ++i) { + y[i<<32] ^= 0x5c5c5c5c; + } + ::SHA1(x, xSize, y + B); + free(x); + + ::SHA1(y, B + 20, (unsigned char*) result.getData()); +} + +#endif + } |