Advertisement
Konomrd123

Untitled

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