mirror of
https://github.com/liberatedsystems/RNode_Firmware_CE.git
synced 2024-07-02 14:34:13 +02:00
MD5 library
This commit is contained in:
parent
637a44ad70
commit
6866e48867
301
MD5.cpp
Normal file
301
MD5.cpp
Normal file
@ -0,0 +1,301 @@
|
||||
#include "MD5.h"
|
||||
|
||||
MD5::MD5()
|
||||
{
|
||||
//nothing
|
||||
return;
|
||||
}
|
||||
|
||||
char* MD5::make_digest(const unsigned char *digest, int len) /* {{{ */
|
||||
{
|
||||
char * md5str = (char*) malloc(sizeof(char)*(len*2+1));
|
||||
static const char hexits[17] = "0123456789abcdef";
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
md5str[i * 2] = hexits[digest[i] >> 4];
|
||||
md5str[(i * 2) + 1] = hexits[digest[i] & 0x0F];
|
||||
}
|
||||
md5str[len * 2] = '\0';
|
||||
return md5str;
|
||||
}
|
||||
|
||||
/*
|
||||
* The basic MD5 functions.
|
||||
*
|
||||
* E and G are optimized compared to their RFC 1321 definitions for
|
||||
* architectures that lack an AND-NOT instruction, just like in Colin Plumb's
|
||||
* implementation.
|
||||
* E() has been used instead of F() because F() is already defined in the Arduino core
|
||||
*/
|
||||
#define E(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
|
||||
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
|
||||
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
||||
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
|
||||
|
||||
/*
|
||||
* The MD5 transformation for all four rounds.
|
||||
*/
|
||||
#define STEP(f, a, b, c, d, x, t, s) \
|
||||
(a) += f((b), (c), (d)) + (x) + (t); \
|
||||
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
|
||||
(a) += (b);
|
||||
|
||||
/*
|
||||
* SET reads 4 input bytes in little-endian byte order and stores them
|
||||
* in a properly aligned word in host byte order.
|
||||
*
|
||||
* The check for little-endian architectures that tolerate unaligned
|
||||
* memory accesses is just an optimization. Nothing will break if it
|
||||
* doesn't work.
|
||||
*/
|
||||
#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
|
||||
# define SET(n) \
|
||||
(*(MD5_u32plus *)&ptr[(n) * 4])
|
||||
# define GET(n) \
|
||||
SET(n)
|
||||
#else
|
||||
# define SET(n) \
|
||||
(ctx->block[(n)] = \
|
||||
(MD5_u32plus)ptr[(n) * 4] | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
|
||||
((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
|
||||
# define GET(n) \
|
||||
(ctx->block[(n)])
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This processes one or more 64-byte data blocks, but does NOT update
|
||||
* the bit counters. There are no alignment requirements.
|
||||
*/
|
||||
const void *MD5::body(void *ctxBuf, const void *data, size_t size)
|
||||
{
|
||||
MD5_CTX *ctx = (MD5_CTX*)ctxBuf;
|
||||
const unsigned char *ptr;
|
||||
MD5_u32plus a, b, c, d;
|
||||
MD5_u32plus saved_a, saved_b, saved_c, saved_d;
|
||||
|
||||
ptr = (unsigned char*)data;
|
||||
|
||||
a = ctx->a;
|
||||
b = ctx->b;
|
||||
c = ctx->c;
|
||||
d = ctx->d;
|
||||
|
||||
do {
|
||||
saved_a = a;
|
||||
saved_b = b;
|
||||
saved_c = c;
|
||||
saved_d = d;
|
||||
|
||||
/* Round 1
|
||||
* E() has been used instead of F() because F() is already defined in the Arduino core
|
||||
*/
|
||||
STEP(E, a, b, c, d, SET(0), 0xd76aa478, 7)
|
||||
STEP(E, d, a, b, c, SET(1), 0xe8c7b756, 12)
|
||||
STEP(E, c, d, a, b, SET(2), 0x242070db, 17)
|
||||
| < | ||||