Question: Warm - Up 9 . 1 : Hashing Strings Start Assignment Due Oct 2 7 by 1 1 : 5 9 pm Points 0 Submitting

Warm-Up 9.1: Hashing Strings
Start Assignment
Due Oct 27 by 11:59pm
Points 0
Submitting a text entry box or a file upload
File Types c
Available until Dec 1 at 11:59pm
Introduction
For this program you will be generating MD5 hashes of strings. This program will make use of concepts we have been learning:
Command line arguments
Reading and writing files
Linking to libraries
Multi-file compilation
We will be using the MD5 hashing algorithm which, as discussed in the live session, creates 128-bit hashes. By convention, the hashes are represented as a string of 32 hexadecimal digits.
Specification
The program, called makehash, will accept two command line arguments:
The source file name to read plaintext from
The destination file name to write hashes to
If the user doesn't supply these two filenames, an appropriate error message should be displayed and the program exits with status code 1.
Each line in the source file is read in, hashed, and the hash written to the destination file. Each input line should have its newline, if present, trimmed off before hashing.
Using the md5 function
The md5 function is contained in the md5.c file. A corresponding function prototype is in the md5.h header file. To use the md5 function in your program, #include the md5.h header file to make the function prototype available. For most people viewing this page, the contents of the files can be found in the tabs below.
To generate an MD5 hash, call the md5 function, passing in two parameters:
The string to be hashed;
The length of the string.
The function returns to the caller a string containing the 32-character hexadecimal hash. The character array is malloc'd by the md5 function; it is the caller's responsibility to free the memory when it is no longer needed.
Compiling your program
Your program will consist of two source files that need to be compiled together to make an executable. Neither of them, individually, is a complete program. Additionally, the md5 function internally calls additional functions from the libcrypto library. This will need to be linked in to your program.
To compile your program, use this command line:
clang makehash.c md5.c -l crypto
Note: the crypto library is included in your Theia environment. In other environments (for example, Ubuntu Linux or Mac OS X), the libraries you need may be different.
What to Turn In
For this assignment, just submit your makehash.c file. You don't need to submit any of the other files that were needed to compile this program.
Files
Test File 1
Use this short text file a source for strings to hash.
dinosaur pineapple SpongeBob rockstar Burger King May the Force be with you.
When run through yourprogram, the output should appear as below.
03318769a5ee1354f7479acc69755e7c 9dee45a24efffc78483a02cfcfd834335703038ffb5c1a34b391111460c74586 d2feb9b6718bb374dfdd68938067695469489b9a664e43bf088e8ccea3e545869c1bd15cd9811efab188647a1323abb7
Test File 2
I ate a clock yesterday, it was very time-consuming. Did you hear about the monkeys who shared an Amazon account? They were Prime mates. Two guys stole a calendar. They got six months each. What do you call a thieving alligator? A Crookodile.
Here are the resulting hashes:
1074e0e8021422f759204f5b7b35cb3e 69f3d9e428eec55d73d851395413bb9e 80349245c053a9a85956852ffce248dd c7feb25f95ff7630bd528983fd82da5e 32c7620215eeeae8946efae725cbf342
md5.c
#include #include #include #include char *md5(const char *str, int length){ EVP_MD_CTX *mdctx; unsigned int md5_digest_len = EVP_MD_size(EVP_md5()); uint8_t md5_digest[md5_digest_len]; char *hexdigest =(char *) malloc(md5_digest_len *2+1); // MD5_Init mdctx = EVP_MD_CTX_new(); EVP_DigestInit_ex(mdctx, EVP_md5(), NULL); // MD5_Update while (length >0){ if (length >512){ EVP_DigestUpdate(mdctx, str,512); } else { EVP_DigestUpdate(mdctx, str, length); } length -=512; str +=512; }// MD5_Final EVP_DigestFinal_ex(mdctx, md5_digest, &md5_digest_len); EVP_MD_CTX_free(mdctx); for (int n =0; n < md5_digest_len; ++n){ snprintf(hexdigest + n*2,3,"%02x", md5_digest[n]); } hexdigest[md5_digest_len *2]='\0'; return hexdigest; }
PreviousNext
md5.h
/** Create an MD5 hex digest from a string. *33-byte string is malloc'd by this function. The caller must free it.* When compiling, link to: -l crypto */ char *md5(const char *str, int length);
PreviousNext

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!