Skip to content

Commit

Permalink
Move print functions to main.c and change F, G, H, and I to macros
Browse files Browse the repository at this point in the history
Trying to clean up the namespace a little. The print functions aren't used in md5.c; they're just convenient for testing and debugging
  • Loading branch information
Zunawe committed Nov 20, 2021
1 parent 4d97086 commit 1525b0d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 46 deletions.
18 changes: 18 additions & 0 deletions main.c
Expand Up @@ -4,6 +4,24 @@

#include "md5.h"

void print_bytes(void *p, size_t length){
uint8_t *pp = (uint8_t *)p;
for(unsigned int i = 0; i < length; ++i){
if(i && !(i % 16)){
printf("\n");
}
printf("%02X ", pp[i]);
}
printf("\n");
}

void print_hash(uint8_t *p){
for(unsigned int i = 0; i < 16; ++i){
printf("%02x", p[i]);
}
printf("\n");
}

int main(int argc, char *argv[]){
uint8_t *result;
if(argc > 1){
Expand Down
52 changes: 10 additions & 42 deletions md5.c
Expand Up @@ -35,6 +35,14 @@ static uint32_t K[] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};

/*
* Bit-manipulation functions defined by the MD5 algorithm
*/
#define F(X, Y, Z) ((X & Y) | (~X & Z))
#define G(X, Y, Z) ((X & Z) | (Y & ~Z))
#define H(X, Y, Z) (X ^ Y ^ Z)
#define I(X, Y, Z) (Y ^ (X | ~Z))

/*
* Padding used to make the size (in bits) of the input congruent to 448 mod 512
*/
Expand Down Expand Up @@ -165,7 +173,7 @@ void md5Step(uint32_t *buffer, uint32_t *input){
uint32_t temp = DD;
DD = CC;
CC = BB;
BB = BB + rotate_left(AA + E + K[i] + input[j], S[i]);
BB = BB + rotateLeft(AA + E + K[i] + input[j], S[i]);
AA = temp;
}

Expand Down Expand Up @@ -209,49 +217,9 @@ uint8_t* md5File(FILE *file){
return result;
}

/*
* Bit-manipulation functions defined by the MD5 algorithm
*/
uint32_t F(uint32_t X, uint32_t Y, uint32_t Z){
return (X & Y) | (~X & Z);
}

uint32_t G(uint32_t X, uint32_t Y, uint32_t Z){
return (X & Z) | (Y & ~Z);
}

uint32_t H(uint32_t X, uint32_t Y, uint32_t Z){
return X ^ Y ^ Z;
}

uint32_t I(uint32_t X, uint32_t Y, uint32_t Z){
return Y ^ (X | ~Z);
}

/*
* Rotates a 32-bit word left by n bits
*/
uint32_t rotate_left(uint32_t x, uint32_t n){
uint32_t rotateLeft(uint32_t x, uint32_t n){
return (x << n) | (x >> (32 - n));
}

/*
* Printing bytes from buffers or the hash
*/
void print_bytes(void *p, size_t length){
uint8_t *pp = (uint8_t *)p;
for(unsigned int i = 0; i < length; ++i){
if(i && !(i % 16)){
printf("\n");
}
printf("%02X ", pp[i]);
}
printf("\n");
}

void print_hash(uint8_t *p){
for(unsigned int i = 0; i < 16; ++i){
printf("%02x", p[i]);
}
printf("\n");
}
5 changes: 1 addition & 4 deletions md5.h
Expand Up @@ -23,7 +23,4 @@ uint32_t G(uint32_t X, uint32_t Y, uint32_t Z);
uint32_t H(uint32_t X, uint32_t Y, uint32_t Z);
uint32_t I(uint32_t X, uint32_t Y, uint32_t Z);

uint32_t rotate_left(uint32_t x, uint32_t n);

void print_bytes(void *p, size_t length);
void print_hash(uint8_t *p);
uint32_t rotateLeft(uint32_t x, uint32_t n);

0 comments on commit 1525b0d

Please sign in to comment.