Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "hash_helper.h"
- unsigned int crc32b(unsigned char *message, unsigned int *crc, size_t bytes) {
- int i, j;
- unsigned int byte, mask;
- i = 0;
- 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;
- }
- return *crc;
- }
- void xor(unsigned char *message, unsigned int *xor, size_t bytes){
- for(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);
- char input[1];
- int counter = 1;
- int lenght = 0;
- unsigned int crc = 0xFFFFFFFF;
- for(int i=0;i<argc;i++){
- if(strcmp(argv[i],"-md5") == 0){
- md5 = 1;
- }
- if(strcmp(argv[i],"-c16") == 0){
- c16 = 1;
- }
- if(strcmp(argv[i],"-c32") == 0){
- c32 = 1;
- }
- if(strcmp(argv[i],"-xor") == 0){
- xor = 1;
- }
- if(strcmp(argv[i],"-hex") == 0){
- hex = 1;
- }
- if(strcmp(argv[i],"-f") == 0){
- if ((i+1) >= argc){
- fprintf(stderr,"Chyba!");
- return 1;
- }
- else {
- file = fopen(argv[i+1],"rb");
- i++;
- if (file == NULL){
- fprintf(stderr,"Chyba!");
- return 1;
- }
- }
- }
- }
- while(counter == 1){
- counter = fread(input,1,1,file);
- if(counter == 0) break;
- lenght += counter;
- if(md5 == 1){
- MD5_Update(&md5_struct,input,counter);
- }
- if(c16 == 1){
- crc16_update(&c16_struct,input);
- }
- if(c32 == 1){
- crc32b(input,crc,counter);
- }
- if(xor == 1){
- xor(input,xor,counter)
- }
- }
- printf("Length: %d bytes.", lenght);
- if(md5 == 1){
- unsigned char result[16];
- MD5_Final(result,md5_struct);
- printf("MD5: ");
- for(int i=0;i<16;i++){
- printf("%x",result[i]);
- }
- printf("\n");
- }
- if(c16 == 1){
- printf("CRC-16: ");
- if(hex == 1){
- printf("0x%x\n",crc16_context.crc);
- }
- else printf("%d\n",crc16_context.crc);
- }
- if(c32 == 1){
- c32 = ~c32;
- printf("CRC-32: ");
- if(hex == 1){
- printf("0x%x\n",crc);
- }
- else printf("%d\n",crc);
- }
- if(xor == 1){
- printf("XOR: ");
- if(hex == 1){
- printf("0x%x\n",xor);
- }
- else printf("%d\n",xor);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement