X-Git-Url: https://harishankar.org/repos/?a=blobdiff_plain;f=wordblah.h;h=c641abcd604c40bcd4ee46690b4479641d99cd28;hb=9448369630128d388ee64e3bed682f49d7a7de07;hp=328148d7bfc1049b423265210447525c2c7218fd;hpb=2aac9fd96329b27dffec6534a042cfbe045f3a11;p=wordblah.git diff --git a/wordblah.h b/wordblah.h index 328148d..c641abc 100644 --- a/wordblah.h +++ b/wordblah.h @@ -103,21 +103,13 @@ err: exit (2); } -/* convert the hashed binary password to hexadecimal representation and - free the hashed binary password */ -void to_hexadecimal (char *hex, unsigned char *binary_pwd, unsigned int len) +/* encode the binary data to readable text format using OpenSSL */ +void encode_binary (char *encoded, unsigned char *binary_data, unsigned int len) { - char buf[3]; - /* keep reference to beginning of the hashed password */ - unsigned char *binary_pw_begin = binary_pwd; - for (int i = 0; i < len; i ++) - { - sprintf (buf, "%02x", (*binary_pwd)&0xff); - strcat (hex, buf); - binary_pwd ++; - } - /* free the hashed password */ - OPENSSL_free (binary_pw_begin); + + EVP_EncodeBlock ((unsigned char*)encoded, + (const unsigned char*)binary_data, len); + OPENSSL_free (binary_data); } /* get a number from the user */ @@ -142,7 +134,7 @@ bool verify_solution_password (Puzzle *p, const char* password) digest_message ((const unsigned char *)password, strlen(password), &hashed_sol_password, &len); char hashed_hex_pwd[256] = { (char) NULL }; - to_hexadecimal (hashed_hex_pwd, hashed_sol_password, len); + encode_binary (hashed_hex_pwd, hashed_sol_password, len); if (strcmp (p->hashed_solution_password, hashed_hex_pwd) == 0) return true; @@ -164,7 +156,7 @@ bool verify_master_password (Puzzle *p, const char* password) digest_message ((const unsigned char *)password, strlen(password), &hashed_mas_password, &len); char hashed_hex_pwd[256] = { (char) NULL }; - to_hexadecimal (hashed_hex_pwd, hashed_mas_password, len); + encode_binary (hashed_hex_pwd, hashed_mas_password, len); if (strcmp (p->hashed_master_password, hashed_hex_pwd) == 0) return true; @@ -188,7 +180,7 @@ void set_solution_password (Puzzle *p, const char *password) /* the hashedpwd contains binary data - we will convert it to hexadecimal data and store in file */ - to_hexadecimal (p->hashed_solution_password, hashedpwd, len); + encode_binary (p->hashed_solution_password, hashedpwd, len); } } @@ -208,7 +200,7 @@ void set_master_password (Puzzle *p, const char *password) /* the hashedpwd contains binary data - we will convert it to hexadecimal data and store in file */ - to_hexadecimal (p->hashed_master_password, hashedpwd, len); + encode_binary (p->hashed_master_password, hashedpwd, len); } } @@ -608,19 +600,23 @@ void save_puzzle (Puzzle *puzzle, const char* file) /* read the puzzle from a file */ Puzzle load_puzzle (const char* file) { + Puzzle p; /* First open the GZip file */ gzFile insourcefile = gzopen (file, "rb"); if (insourcefile == NULL) { fprintf (stderr, "%s %s\n", ERROR_READING_FILE, COMPRESSED); - exit (1); + /* return an invalid puzzle */ + init_puzzle (&p, 0); + return p; } /* Open a temporary file to uncompress the contents */ FILE *infile = tmpfile (); if (infile == NULL) { fprintf (stderr, "%s\n", ERROR_READING_FILE); - exit (1); + init_puzzle (&p, 0); + return p; } /* Put the uncompressed content to the temp file */ char buf[128]; @@ -634,7 +630,9 @@ Puzzle load_puzzle (const char* file) fprintf (stderr, "%s\n", ERROR_READING_FILE); fclose (infile); gzclose (insourcefile); - exit (1); + /* return an invalid puzzle */ + init_puzzle (&p, 0); + return p; } num = gzread (insourcefile, buf, 128); } @@ -645,10 +643,17 @@ Puzzle load_puzzle (const char* file) fseek (infile, 0, 0); /* Read the temporary file contents to the structure Puzzle */ - Puzzle p; char line[MAX_CLUE_LENGTH+10]; fgets (line, MAX_CLUE_LENGTH + 10, infile); p.grid_size = atoi (line); + /* if puzzle is invalid or otherwise not proper grid, return an invalid + puzzle object */ + if (p.grid_size == 0) + { + fprintf (stderr, "%s\n", INVALID_PUZZLE); + init_puzzle (&p, 0); + return p; + } fgets (line, MAX_CLUE_LENGTH + 10, infile); p.grid_frozen = atoi (line) == 0 ? false : true ; fgets (line, MAX_CLUE_LENGTH + 10, infile);