Advertisement
Guest User

:* <3

a guest
Apr 21st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "hash_helper.h"
  6.  
  7. char *load_file(char* filename)
  8. {
  9.     FILE *input = fopen(filename, "rb");
  10.     if (input == NULL)
  11.         return NULL;
  12.     fseek(input, 0, SEEK_END);
  13.     unsigned int lenght = ftell(input) + 1;
  14.     fseek(input, 0, SEEK_SET);
  15.     char *output = malloc(sizeof(int) * lenght);
  16.     if(fgets(output, lenght, input) == NULL) {
  17.         free(output);
  18.         fclose(input);
  19.         return NULL;
  20.     }
  21.     fclose(input);
  22.     return output;
  23. }
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.     int was_f = -1, file = -1;
  28.     bool md5 = false, c16 = false, c32 = false, xorr = false;
  29.     for (int i = 1; i < argc; i++) {
  30.         if (strcmp(argv[i], "-md5") == 0)
  31.             md5 = true;
  32.         else if (strcmp(argv[i], "-c16") == 0)
  33.             c16 = true;
  34.         else if (strcmp(argv[i], "-c32") == 0)
  35.             c32 = true;
  36.         else if (strcmp(argv[i], "-xor") == 0)
  37.             xorr = true;
  38.         //else if (argv[i] == "-hex")
  39.         else if (strcmp(argv[i], "-f") == 0)
  40.             was_f = 0;
  41.         else {
  42.             if (was_f == 1) {
  43.                 file = i;
  44.                 was_f = -1;
  45.             }
  46.         }
  47.         if (was_f != -1)
  48.             was_f++;
  49.     }
  50.     if (file > -1) {
  51.         unsigned char *input = (unsigned char) load_file(argv[file]);
  52.         if (!input) {
  53.             printf("problem");
  54.             return 1;
  55.         }
  56.         if (c16) {
  57.             crc16_context hashc16;
  58.             crc16_init(&hashc16);
  59.             for (size_t i = 0; i < strlen(input); i++) {
  60.                 crc16_update(&hashc16, input[i]);
  61.             }
  62.             printf("%04x", hashc16.crc);
  63.         }
  64.         if (md5) {
  65.             MD5_CTX hashmd5;
  66.             unsigned char *result;
  67.             MD5_Init(&hashmd5);
  68.             for (size_t i = 0; i < strlen(input); i++) {
  69.                 MD5_Update(&hashmd5, input[i], sizeof(char));
  70.             }
  71.             MD5_Final(result, &hashmd5);
  72.             printf("%s", result);
  73.         }
  74.     } else if (was_f > -1) {
  75.         printf("ERROR");
  76.         return 1;
  77.     }
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement