Browse Source

Drobne poprawki

legacy
Marek 10 years ago
parent
commit
ea340e8f8e
  1. 64
      md5.cpp

64
md5.cpp

@ -2,46 +2,45 @@
#ifndef HAVE_OPENSSL #ifndef HAVE_OPENSSL
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
#define H(x, y, z) ((x) ^ (y) ^ (z)) #define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | ~(z))) #define I(x, y, z) ((y) ^ ((x) | ~(z)))
#define STEP(f, a, b, c, d, x, t, s) \
#define STEP(f, a, b, c, d, x, t, s) \
(a) += f((b), (c), (d)) + (x) + (t); \ (a) += f((b), (c), (d)) + (x) + (t); \
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
(a) += (b); (a) += (b);
#if defined(__i386__) || defined(__x86_64__) || defined(__vax__) #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
#define SET(n) \ #define SET(n) \
(*(MD5_u32 *)&ptr[(n) * 4]) (*(MD5_u32 *)&ptr[(n) * 4])
#define GET(n) \ #define GET(n) \
SET(n) SET(n)
#else #else
#define SET(n) \ #define SET(n) \
(ctx->block[(n)] = \ (ctx->block[(n)] = \
(MD5_u32)ptr[(n) * 4] | \ (MD5_u32)ptr[(n) * 4] | \
((MD5_u32)ptr[(n) * 4 + 1] << 8) | \ ((MD5_u32)ptr[(n) * 4 + 1] << 8) | \
((MD5_u32)ptr[(n) * 4 + 2] << 16) | \ ((MD5_u32)ptr[(n) * 4 + 2] << 16) | \
((MD5_u32)ptr[(n) * 4 + 3] << 24)) ((MD5_u32)ptr[(n) * 4 + 3] << 24))
#define GET(n) \ #define GET(n) \
(ctx->block[(n)]) (ctx->block[(n)])
#endif #endif
typedef unsigned int MD5_u32; typedef unsigned int MD5_u32;
typedef struct { typedef struct {
MD5_u32 lo, hi; MD5_u32 lo, hi;
MD5_u32 a, b, c, d; MD5_u32 a, b, c, d;
unsigned char buffer[64]; unsigned char buffer[64];
MD5_u32 block[16]; MD5_u32 block[16];
} MD5_CTX; } MD5_CTX;
static void MD5_Init(MD5_CTX *ctx); static void MD5_Init(MD5_CTX *ctx);
static void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size); static void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size);
static void MD5_Final(unsigned char *result, MD5_CTX *ctx); static void MD5_Final(unsigned char *result, MD5_CTX *ctx);
static const void *body(MD5_CTX *ctx, const void *data, unsigned long size){ static const void *body(MD5_CTX *ctx, const void *data, unsigned long size){
const unsigned char *ptr; const unsigned char *ptr;
MD5_u32 a, b, c, d; MD5_u32 a, b, c, d;
MD5_u32 saved_a, saved_b, saved_c, saved_d; MD5_u32 saved_a, saved_b, saved_c, saved_d;
@ -75,7 +74,6 @@ static const void *body(MD5_CTX *ctx, const void *data, unsigned long size){
STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
@ -92,7 +90,6 @@ static const void *body(MD5_CTX *ctx, const void *data, unsigned long size){
STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
STEP(H, d, a, b, c, GET(8), 0x8771f681, 11) STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
@ -109,7 +106,6 @@ static const void *body(MD5_CTX *ctx, const void *data, unsigned long size){
STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11) STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23) STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
@ -141,9 +137,9 @@ static const void *body(MD5_CTX *ctx, const void *data, unsigned long size){
ctx->d = d; ctx->d = d;
return ptr; return ptr;
} }
void MD5_Init(MD5_CTX *ctx){ void MD5_Init(MD5_CTX *ctx){
ctx->a = 0x67452301; ctx->a = 0x67452301;
ctx->b = 0xefcdab89; ctx->b = 0xefcdab89;
ctx->c = 0x98badcfe; ctx->c = 0x98badcfe;
@ -151,9 +147,9 @@ void MD5_Init(MD5_CTX *ctx){
ctx->lo = 0; ctx->lo = 0;
ctx->hi = 0; ctx->hi = 0;
} }
void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size){ void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size){
MD5_u32 saved_lo; MD5_u32 saved_lo;
unsigned long used, free; unsigned long used, free;
@ -161,12 +157,10 @@ void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size){
if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
ctx->hi++; ctx->hi++;
ctx->hi += size >> 29; ctx->hi += size >> 29;
used = saved_lo & 0x3f; used = saved_lo & 0x3f;
if (used){ if (used){
free = 64 - used; free = 64 - used;
if (size < free) { if (size < free) {
memcpy(&ctx->buffer[used], data, size); memcpy(&ctx->buffer[used], data, size);
return; return;
@ -184,11 +178,10 @@ void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size){
} }
memcpy(ctx->buffer, data, size); memcpy(ctx->buffer, data, size);
} }
void MD5_Final(unsigned char *result, MD5_CTX *ctx){ void MD5_Final(unsigned char *result, MD5_CTX *ctx){
unsigned long used, free; unsigned long used, free;
used = ctx->lo & 0x3f; used = ctx->lo & 0x3f;
ctx->buffer[used++] = 0x80; ctx->buffer[used++] = 0x80;
free = 64 - used; free = 64 - used;
@ -211,9 +204,7 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx){
ctx->buffer[61] = ctx->hi >> 8; ctx->buffer[61] = ctx->hi >> 8;
ctx->buffer[62] = ctx->hi >> 16; ctx->buffer[62] = ctx->hi >> 16;
ctx->buffer[63] = ctx->hi >> 24; ctx->buffer[63] = ctx->hi >> 24;
body(ctx, ctx->buffer, 64); body(ctx, ctx->buffer, 64);
result[0] = ctx->a; result[0] = ctx->a;
result[1] = ctx->a >> 8; result[1] = ctx->a >> 8;
result[2] = ctx->a >> 16; result[2] = ctx->a >> 16;
@ -230,11 +221,10 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx){
result[13] = ctx->d >> 8; result[13] = ctx->d >> 8;
result[14] = ctx->d >> 16; result[14] = ctx->d >> 16;
result[15] = ctx->d >> 24; result[15] = ctx->d >> 24;
memset(ctx, 0, sizeof(*ctx)); memset(ctx, 0, sizeof(*ctx));
} }
#else #else
#include <openssl/md5.h> #include <openssl/md5.h>
#endif #endif

Loading…
Cancel
Save