Created
November 4, 2015 01:33
-
-
Save yudi-matsuzake/7584db5d342aba91fbd3 to your computer and use it in GitHub Desktop.
Hash bobo do exercicício 4 da aula-21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define BUFFER_SIZE 4098 | |
#define ERROR_MSG "Não pôde abrir o arquivo %s.\n" | |
#define USAGE_MSG "USAGE: %s <file> [file ...]\n" | |
unsigned char naive_hash(char* filename){ | |
FILE* file = fopen(filename, "r"); | |
if(!file){ | |
fprintf(stderr, ERROR_MSG, filename); | |
return 0; | |
} | |
char buffer[BUFFER_SIZE]; | |
unsigned int n_data; | |
unsigned char hash = 0; | |
int i; | |
while((n_data = fread(buffer, sizeof(unsigned char), BUFFER_SIZE, file))) | |
for(i=0; i<n_data; i++) | |
hash ^= buffer[i]; | |
fclose(file); | |
return hash; | |
} | |
int main(int argc, char* argv[]){ | |
int i; | |
if(argc <= 1){ | |
printf(USAGE_MSG, argv[0]); | |
return 1; | |
} | |
for(i=1; i<argc; i++){ | |
printf("%x\t|\t%s\n", naive_hash(argv[i]), argv[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment