diff options
Diffstat (limited to 'Swiften/StringCodecs/HMACSHA1.cpp')
| -rw-r--r-- | Swiften/StringCodecs/HMACSHA1.cpp | 67 | 
1 files changed, 41 insertions, 26 deletions
| diff --git a/Swiften/StringCodecs/HMACSHA1.cpp b/Swiften/StringCodecs/HMACSHA1.cpp index 6ae5513..fd951ae 100644 --- a/Swiften/StringCodecs/HMACSHA1.cpp +++ b/Swiften/StringCodecs/HMACSHA1.cpp @@ -4,41 +4,56 @@   * See Documentation/Licenses/GPLv3.txt for more information.   */ -#include "Swiften/StringCodecs/HMACSHA1.h" +#include <Swiften/StringCodecs/HMACSHA1.h>  #include <cassert> -#include "Swiften/StringCodecs/SHA1.h" -#include "Swiften/Base/ByteArray.h" +#include <Swiften/StringCodecs/SHA1.h> +#include <Swiften/Base/Algorithm.h> -namespace Swift { +using namespace Swift; -static const unsigned int B = 64; +namespace { +	static const unsigned int B = 64; -ByteArray HMACSHA1::getResult(const ByteArray& key, const ByteArray& data) { -	assert(key.getSize() <= B); +	template<typename SourceType> +	ByteArray getHMACSHA1(const SourceType& key, const ByteArray& data) { +		assert(key.size() <= B); -	// Create the padded key -	ByteArray paddedKey(key); -	paddedKey.resize(B, 0x0); +		// Create the padded key +		SourceType paddedKey(key); +		paddedKey.resize(B, 0x0); -	// Create the first value -	ByteArray x(paddedKey); -	for (unsigned int i = 0; i < x.getSize(); ++i) { -		x[i] ^= 0x36; -	} -	x += data; +		// Create the first value +		SourceType x(paddedKey); +		for (unsigned int i = 0; i < x.size(); ++i) { +			x[i] ^= 0x36; +		} +		append(x, data); -	// Create the second value -	ByteArray y(paddedKey); -	for (unsigned int i = 0; i < y.getSize(); ++i) { -		y[i] ^= 0x5c; +		// Create the second value +		SourceType y(paddedKey); +		for (unsigned int i = 0; i < y.size(); ++i) { +			y[i] ^= 0x5c; +		} +		append(y, SHA1::getHash(x)); + +		return SHA1::getHash(y);  	} -	y += SHA1::getHash(x); +} -	return SHA1::getHash(y); +namespace Swift { + +ByteArray HMACSHA1::getResult(const SafeByteArray& key, const ByteArray& data) { +	return getHMACSHA1(key, data); +} + +ByteArray HMACSHA1::getResult(const ByteArray& key, const ByteArray& data) { +	return getHMACSHA1(key, data);  } + +  #if 0  // A tweaked version of HMACSHA1 that is more than twice as fast as the one above. @@ -53,19 +68,19 @@ ByteArray HMACSHA1::getResult(const ByteArray& key, const ByteArray& data) {  void HMACSHA1::getResult(const ByteArray& key, const ByteArray& data, ByteArray& result) {  	// Create first value -	size_t xSize = B + data.getSize(); +	size_t xSize = B + data.size();  	unsigned char* x = (unsigned char*) malloc(xSize * sizeof(unsigned char));  	memset(x, 0, B); -	memcpy(x, key.getData(), key.getSize()); +	memcpy(x, key.getData(), key.size());  	for (unsigned int i = 0; i < (B>>32); ++i) {  		x[i<<32] ^= 0x36363636;  	} -	memcpy(x + B, data.getData(), data.getSize()); +	memcpy(x + B, data.getData(), data.size());  	// Create the second value  	unsigned char y[B + 20];  	memset(y, 0, B); -	memcpy(y, key.getData(), key.getSize()); +	memcpy(y, key.getData(), key.size());  	for (unsigned int i = 0; i < (B>>32); ++i) {  		y[i<<32] ^= 0x5c5c5c5c;  	} | 
 Swift
 Swift