summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-11-27 19:06:47 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-11-27 19:06:47 (GMT)
commitaa09a889108c4d0e3c5888ad98958d8f3e12bd3b (patch)
tree3c621ae0a6e2150281be182b37e2837b804adcba /Swiften/StringCodecs/SHA1.cpp
parentc89ef0ffae597ac8c1063732e1d9a2d84703a80c (diff)
downloadswift-aa09a889108c4d0e3c5888ad98958d8f3e12bd3b.zip
swift-aa09a889108c4d0e3c5888ad98958d8f3e12bd3b.tar.bz2
Added MD5 hashing algorithm.
Moved 'hexifying' of hashes into its own class, such that it can be shared between all hashes.
Diffstat (limited to 'Swiften/StringCodecs/SHA1.cpp')
-rw-r--r--Swiften/StringCodecs/SHA1.cpp17
1 files changed, 1 insertions, 16 deletions
diff --git a/Swiften/StringCodecs/SHA1.cpp b/Swiften/StringCodecs/SHA1.cpp
index 70256e9..ef99d9a 100644
--- a/Swiften/StringCodecs/SHA1.cpp
+++ b/Swiften/StringCodecs/SHA1.cpp
@@ -1,52 +1,48 @@
-#include <sstream>
-#include <iomanip>
-#include <boost/numeric/conversion/cast.hpp>
-
#include "Swiften/Base/Platform.h"
#pragma GCC diagnostic ignored "-Wold-style-cast"
/*
SHA-1 in C
By Steve Reid <steve@edmweb.com>
100% Public Domain
Test Vectors (from FIPS PUB 180-1)
"abc"
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
A million repetitions of "a"
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
/* #define LITTLE_ENDIAN * This should be #define'd if true. */
/* #define SHA1HANDSOFF * Copies data before messing with it. */
#include <boost/cstdint.hpp>
#include <stdio.h>
#include <string.h>
typedef struct {
boost::uint32_t state[5];
boost::uint32_t count[2];
boost::uint8_t buffer[64];
} SHA1_CTX;
void SHA1Transform(boost::uint32_t state[5], boost::uint8_t buffer[64]);
void SHA1Init(SHA1_CTX* context);
void SHA1Update(SHA1_CTX* context, boost::uint8_t* data, unsigned int len);
void SHA1Final(boost::uint8_t digest[20], SHA1_CTX* context);
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
/* blk0() and blk() perform the initial expand. */
/* I got the idea of expanding during the round function from SSLeay */
#ifdef SWIFTEN_LITTLE_ENDIAN
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|(rol(block->l[i],8)&0x00FF00FF))
#else
#define blk0(i) block->l[i]
#endif
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
^block->l[(i+2)&15]^block->l[i&15],1))
@@ -142,71 +138,60 @@ unsigned int i, j;
SHA1Transform(context->state, context->buffer);
for ( ; i + 63 < len; i += 64) {
SHA1Transform(context->state, &data[i]);
}
j = 0;
}
else i = 0;
memcpy(&context->buffer[j], &data[i], len - i);
}
/* Add padding and return the message digest. */
void SHA1Final(boost::uint8_t digest[20], SHA1_CTX* context)
{
boost::uint32_t i, j;
boost::uint8_t finalcount[8];
for (i = 0; i < 8; i++) {
finalcount[i] = (boost::uint8_t) ((context->count[(i >= 4 ? 0 : 1)]
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
}
SHA1Update(context, (boost::uint8_t *)("\200"), 1);
while ((context->count[0] & 504) != 448) {
SHA1Update(context, (boost::uint8_t *)("\0"), 1);
}
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
for (i = 0; i < 20; i++) {
digest[i] = (boost::uint8_t)
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
}
/* Wipe variables */
i = j = 0;
memset(context->buffer, 0, 64);
memset(context->state, 0, 20);
memset(context->count, 0, 8);
memset(&finalcount, 0, 8);
#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */
SHA1Transform(context->state, context->buffer);
#endif
}
// -----------------------------------------------------------------------------
#include "Swiften/StringCodecs/SHA1.h"
namespace Swift {
-ByteArray SHA1::getBinaryHash(const ByteArray& input) {
+ByteArray SHA1::getHash(const ByteArray& input) {
ByteArray inputCopy(input);
ByteArray digest;
digest.resize(20);
SHA1_CTX context;
SHA1Init(&context);
SHA1Update(&context, (boost::uint8_t*) inputCopy.getData(), inputCopy.getSize());
SHA1Final((boost::uint8_t*) digest.getData(), &context);
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());
-}
-
}