Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include <stdlib.h>
- #include "hash_helper.h"
- char *load_file(char* filename)
- {
- FILE *input = fopen(filename, "rb");
- if (input == NULL)
- return NULL;
- fseek(input, 0, SEEK_END);
- unsigned int lenght = ftell(input) + 1;
- fseek(input, 0, SEEK_SET);
- char *output = malloc(sizeof(int) * lenght);
- if(fgets(output, lenght, input) == NULL) {
- free(output);
- fclose(input);
- return NULL;
- }
- fclose(input);
- return output;
- }
- int main(int argc, char *argv[])
- {
- int was_f = -1, file = -1;
- bool md5 = false, c16 = false, c32 = false, xorr = false;
- for (int i = 1; i < argc; i++) {
- if (strcmp(argv[i], "-md5") == 0)
- md5 = true;
- else if (strcmp(argv[i], "-c16") == 0)
- c16 = true;
- else if (strcmp(argv[i], "-c32") == 0)
- c32 = true;
- else if (strcmp(argv[i], "-xor") == 0)
- xorr = true;
- //else if (argv[i] == "-hex")
- else if (strcmp(argv[i], "-f") == 0)
- was_f = 0;
- else {
- if (was_f == 1) {
- file = i;
- was_f = -1;
- }
- }
- if (was_f != -1)
- was_f++;
- }
- if (file > -1) {
- unsigned char *input = (unsigned char) load_file(argv[file]);
- if (!input) {
- printf("problem");
- return 1;
- }
- if (c16) {
- crc16_context hashc16;
- crc16_init(&hashc16);
- for (size_t i = 0; i < strlen(input); i++) {
- crc16_update(&hashc16, input[i]);
- }
- printf("%04x", hashc16.crc);
- }
- if (md5) {
- MD5_CTX hashmd5;
- unsigned char *result;
- MD5_Init(&hashmd5);
- for (size_t i = 0; i < strlen(input); i++) {
- MD5_Update(&hashmd5, input[i], sizeof(char));
- }
- MD5_Final(result, &hashmd5);
- printf("%s", result);
- }
- } else if (was_f > -1) {
- printf("ERROR");
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement