Skip to content

Instantly share code, notes, and snippets.

@Orc
Last active April 25, 2017 18:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Orc/d9d3380777d503c716e9881ce04599e4 to your computer and use it in GitHub Desktop.
Q&D md5sum clone that uses Solar Designer's md5 implementation
/* Q&D md5sum clone that uses Solar Designer's md5 implementation */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "md5.h"
void
md5sum(char *file)
{
int i, fd;
unsigned char block[5120];
int size;
MD5_CTX foo;
if (strcmp(file, "-") == 0 )
fd = dup(0);
else if ( (fd=open(file, O_RDONLY)) == -1 ) {
perror(file);
return;
}
MD5_Init(&foo);
while ( (size=read(fd, block, sizeof block)) > 0 )
MD5_Update(&foo, block, size);
MD5_Final(block, &foo);
for ( i=0; i < 16; i++ )
printf("%02x", block[i]);
printf(" %s\n", file);
close(fd);
}
main(argc, argv)
char **argv;
{
int i;
if ( argc < 2 )
md5sum("-");
else
for ( i=1; i < argc; i++ )
md5sum(argv[i]);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment