Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "hash_helper.h"
- //funkce na výpoèet crc232 hashe
- void crc32b(unsigned char *message, unsigned int *crc, size_t bytes) {
- unsigned int i = 0;
- int j;
- unsigned int byte, mask;
- while (i < bytes) {
- byte = message[i]; // Get next byte.
- *crc = *crc ^ byte;
- for (j = 7; j >= 0; j--) { // Do eight times.
- mask = -(*crc & 1);
- *crc = (*crc >> 1) ^ (0xEDB88320 & mask);
- }
- i = i + 1;
- }
- }
- //funkce na výpoèet xor hashe
- void xor_hash(unsigned char *message, unsigned int *xor, size_t bytes){
- for(unsigned int i=0;i<bytes;i++){
- *xor = *xor^message[i];
- }
- }
- int main(int argc, char *argv[])
- {
- unsigned int md5=0, c16=0, c32=0, xor=0, hex=0;
- FILE *file = stdin;
- MD5_CTX md5_struct;
- MD5_Init(&md5_struct);
- crc16_context c16_struct;
- crc16_init(&c16_struct);
- unsigned char input[1];
- int counter = 1;
- int lenght = 0;
- unsigned int crc = 0xFFFFFFFF;
- unsigned int xor_result = 0;
- for(int i=0;i<argc;i++){
- //pøepínaè md5
- if(strcmp(argv[i],"-md5") == 0){
- md5 = 1;
- }
- //pøepínaè crc16
- else if(strcmp(argv[i],"-c16") == 0){
- c16 = 1;
- }
- //pøepínaè crc32
- else if(strcmp(argv[i],"-c32") == 0){
- c32 = 1;
- }
- //pøepínaè xor
- else if(strcmp(argv[i],"-xor") == 0){
- xor = 1;
- }
- //pøepínaè hex
- else if(strcmp(argv[i],"-hex") == 0){
- hex = 1;
- }
- //pøepínaè f file
- else if(strcmp(argv[i],"-f") == 0){
- if ((i+1) >= argc){
- fprintf(stderr,"Nezadali jste soubor!");
- return 1;
- }
- else {
- file = fopen(argv[i+1],"rb");
- i++;
- if (file == NULL){
- fprintf(stderr,"Nejde otevrit!");
- return 1;
- }
- }
- }
- }
- if (!md5 && !c16 && !c32 && !xor){
- fprintf(stderr,"Nezadali jste zadny hash!");
- return 1;
- }
- while(counter == 1){
- counter = fread(input,1,1,file);
- if(counter == 0) break;
- lenght += counter;
- //volání md5
- if(md5 == 1){
- MD5_Update(&md5_struct,input,counter);
- }
- //volání crc16
- if(c16 == 1){
- crc16_update(&c16_struct,input[0]);
- }
- //volání crc32
- if(c32 == 1){
- crc32b(input,&crc,counter);
- }
- //volání xor
- if(xor == 1){
- xor_hash(input,&xor_result,counter);
- }
- }
- printf("Length: %d bytes\n", lenght);
- //výpis xor
- if(xor == 1){
- printf("XOR: ");
- if(hex == 1){
- printf("0x%x\n",xor_result);
- }
- else printf("%d\n",xor_result);
- }
- //výpis crc16
- if(c16 == 1){
- printf("CRC-16: ");
- if(hex == 1){
- printf("0x%x\n",c16_struct.crc);
- }
- else printf("%d\n",c16_struct.crc);
- }
- //výpis crc32
- if(c32 == 1){
- crc = ~crc;
- if(hex == 1){
- printf("CRC-32: 0x%08x\n",crc);
- }
- else printf("CRC-32: %u\n",crc);
- }
- //výpis md5
- if(md5 == 1){
- unsigned char result[16];
- MD5_Final(result,&md5_struct);
- printf("MD5: ");
- for(int i=0;i<16;i++){
- printf("%02x",result[i]);
- }
- printf("\n");
- }
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment