Konomrd123

Untitled

Apr 28th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "hash_helper.h"
  5. //funkce na výpoèet crc232 hashe
  6. void crc32b(unsigned char *message, unsigned int *crc, size_t bytes) {
  7.    unsigned int i = 0;
  8.    int j;
  9.    unsigned int byte, mask;
  10.    while (i < bytes) {
  11.       byte = message[i];            // Get next byte.
  12.       *crc = *crc ^ byte;
  13.       for (j = 7; j >= 0; j--) {    // Do eight times.
  14.          mask = -(*crc & 1);
  15.          *crc = (*crc >> 1) ^ (0xEDB88320 & mask);
  16.       }
  17.       i = i + 1;
  18.    }
  19. }
  20. //funkce na výpoèet xor hashe
  21. void xor_hash(unsigned char *message, unsigned int *xor, size_t bytes){
  22.     for(unsigned int i=0;i<bytes;i++){
  23.         *xor = *xor^message[i];
  24.     }
  25. }
  26. int main(int argc, char *argv[])
  27. {
  28.     unsigned int md5=0, c16=0, c32=0, xor=0, hex=0;
  29.     FILE *file = stdin;
  30.     MD5_CTX md5_struct;
  31.     MD5_Init(&md5_struct);
  32.     crc16_context c16_struct;
  33.     crc16_init(&c16_struct);
  34.     unsigned char input[1];
  35.     int counter = 1;
  36.     int lenght = 0;
  37.     unsigned int crc = 0xFFFFFFFF;
  38.     unsigned int xor_result = 0;
  39.     for(int i=0;i<argc;i++){
  40.         //pøepínaè md5
  41.         if(strcmp(argv[i],"-md5") == 0){
  42.             md5 = 1;
  43.         }
  44.         //pøepínaè crc16
  45.         else if(strcmp(argv[i],"-c16") == 0){
  46.             c16 = 1;
  47.         }
  48.         //pøepínaè crc32
  49.         else if(strcmp(argv[i],"-c32") == 0){
  50.             c32 = 1;
  51.         }
  52.         //pøepínaè xor
  53.         else if(strcmp(argv[i],"-xor") == 0){
  54.             xor = 1;
  55.         }
  56.         //pøepínaè hex
  57.         else if(strcmp(argv[i],"-hex") == 0){
  58.             hex = 1;
  59.         }
  60.         //pøepínaè f file
  61.         else if(strcmp(argv[i],"-f") == 0){
  62.             if ((i+1) >= argc){
  63.                 fprintf(stderr,"Nezadali jste soubor!");
  64.                 return 1;
  65.             }
  66.             else {
  67.                 file = fopen(argv[i+1],"rb");
  68.                 i++;
  69.                 if (file == NULL){
  70.                     fprintf(stderr,"Nejde otevrit!");
  71.                     return 1;
  72.                 }
  73.             }
  74.         }
  75.     }
  76.     if (!md5 && !c16 && !c32 && !xor){
  77.             fprintf(stderr,"Nezadali jste zadny hash!");
  78.             return 1;
  79.         }
  80.     while(counter == 1){
  81.         counter = fread(input,1,1,file);
  82.         if(counter == 0) break;
  83.         lenght += counter;
  84.         //volání md5
  85.         if(md5 == 1){
  86.             MD5_Update(&md5_struct,input,counter);
  87.         }
  88.         //volání crc16
  89.         if(c16 == 1){
  90.             crc16_update(&c16_struct,input[0]);
  91.         }
  92.         //volání crc32
  93.         if(c32 == 1){
  94.             crc32b(input,&crc,counter);
  95.         }
  96.         //volání xor
  97.         if(xor == 1){
  98.             xor_hash(input,&xor_result,counter);
  99.         }
  100.     }
  101.     printf("Length: %d bytes\n", lenght);
  102.     //výpis xor
  103.     if(xor == 1){
  104.         printf("XOR: ");
  105.         if(hex == 1){
  106.             printf("0x%x\n",xor_result);
  107.         }
  108.         else printf("%d\n",xor_result);
  109.     }
  110.     //výpis crc16
  111.     if(c16 == 1){
  112.         printf("CRC-16: ");
  113.         if(hex == 1){
  114.             printf("0x%x\n",c16_struct.crc);
  115.         }
  116.         else printf("%d\n",c16_struct.crc);
  117.     }
  118.     //výpis crc32
  119.     if(c32 == 1){
  120.         crc = ~crc;
  121.         if(hex == 1){
  122.             printf("CRC-32: 0x%08x\n",crc);
  123.         }
  124.         else printf("CRC-32: %u\n",crc);
  125.     }
  126.     //výpis md5
  127.     if(md5 == 1){
  128.         unsigned char result[16];
  129.         MD5_Final(result,&md5_struct);
  130.         printf("MD5: ");
  131.         for(int i=0;i<16;i++){
  132.             printf("%02x",result[i]);
  133.         }
  134.         printf("\n");
  135.     }
  136.     fclose(file);
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment