Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small changes to improve compatibility. #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions md5.c
Original file line number Diff line number Diff line change
@@ -5,6 +5,9 @@

#include "md5.h"

#include <stdlib.h>
#include <string.h>

/*
* Constants defined by the MD5 algorithm
*/
@@ -81,7 +84,7 @@ void md5Init(MD5Context *ctx){
* If the input fills out a block of 512 bits, apply the algorithm (md5Step)
* and save the result in the buffer. Also updates the overall size.
*/
void md5Update(MD5Context *ctx, uint8_t *input_buffer, size_t input_len){
void md5Update(MD5Context *ctx, const uint8_t *input_buffer, size_t input_len){
uint32_t input[16];
unsigned int offset = ctx->size % 64;
ctx->size += (uint64_t)input_len;
@@ -148,7 +151,7 @@ void md5Finalize(MD5Context *ctx){
/*
* Step on 512 bits of input with the main MD5 algorithm.
*/
void md5Step(uint32_t *buffer, uint32_t *input){
void md5Step(uint32_t *buffer, const uint32_t *input){
uint32_t AA = buffer[0];
uint32_t BB = buffer[1];
uint32_t CC = buffer[2];
@@ -195,10 +198,10 @@ void md5Step(uint32_t *buffer, uint32_t *input){
* Functions that run the algorithm on the provided input and put the digest into result.
* result should be able to store 16 bytes.
*/
void md5String(char *input, uint8_t *result){
void md5String(const char *input, uint8_t *result){
MD5Context ctx;
md5Init(&ctx);
md5Update(&ctx, (uint8_t *)input, strlen(input));
md5Update(&ctx, (const uint8_t *)input, strlen(input));
md5Finalize(&ctx);

memcpy(result, ctx.digest, 16);
16 changes: 11 additions & 5 deletions md5.h
Original file line number Diff line number Diff line change
@@ -3,8 +3,6 @@

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

typedef struct{
uint64_t size; // Size of input in bytes
@@ -13,12 +11,20 @@ typedef struct{
uint8_t digest[16]; // Result of algorithm
}MD5Context;

#ifdef __cplusplus
extern "C" {
#endif

void md5Init(MD5Context *ctx);
void md5Update(MD5Context *ctx, uint8_t *input, size_t input_len);
void md5Update(MD5Context *ctx, const uint8_t *input, size_t input_len);
void md5Finalize(MD5Context *ctx);
void md5Step(uint32_t *buffer, uint32_t *input);
void md5Step(uint32_t *buffer, const uint32_t *input);

void md5String(char *input, uint8_t *result);
void md5String(const char *input, uint8_t *result);
void md5File(FILE *file, uint8_t *result);

#ifdef __cplusplus
}
#endif

#endif