Md5 checksum of file that is input by user

closed account (3v0MSL3A)
Hi, can anyone help me? I am new to C++ programming. I need to create a program that calculate the md5 checksum of the file that is input by user. At the moment, I managed to open the file and calculate the md5 checksum.

MD5 md5 ;
cout << "The MD5 checksum for file '" << pathA << "' is" << endl ;
puts( md5.digestFile ("D:\\test.txt") ) ;

My problem is how can I write a command without need to write the path of the file as you can see above. I want to call the path of the file that input by user at the starting of my program.

Also after that, how can I compare the md5 checksum of the file with another textfile that contains virus signature (In this case, the virus signature is already created).

Thank you.
what do you have?
if you have the path and filenames in string variables from user input, that is easy. If it was drag and drop / commandline input, you may have to parse the string for the path piece and toss the filename.

open the file and compare the checksums as strings, probably hex strings.
closed account (3v0MSL3A)
This is my program. Its not complete yet. Can you explain more detail or give example? Im still not clear. Thank you.


#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include "md5.h"

using namespace std;
int main()
{

/*******file that user wish to scan*******/

ifstream fileA ;
string pathA ;
cout << "Write the path of the file that you wish to scan\n" ;
cin >> pathA ;

if(!fileA )
{
cout << "Could not open the file " << pathA << endl ;
return 1 ;
}
else
{

/*******calculating md5 checksum of the file*******/

MD5 md5 ;
cout << "The MD5 checksum for file '" << pathA << "' is" << endl ;
puts( md5.digestFile ("D://test.txt") ) ;

/*******store the checksum in other file for comparing*******/

ofstream checksum ("D:\\md5checksum.txt");
if (checksum.is_open())
{
checksum << md5.digestFile ("D:\\test.txt") << endl ;
checksum.close();
}

/*******comparing checksum with virus database*******/

cout << "Checking in virus dictionary to find matching signature...\n" ;

}
return 0;
}
I would use getline to read the filename since it might contain spaces. Also before you can check your input stream you need to open it or specify a filename when you create it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>
#include <cstdio>
#include "md5.h"

int main()
{
  cout << "Write the path of the file that you wish to scan\n"; 
  string filename;
  getline(cin, filename);
  if (filename.empty())
  {
     cout << "No filename entered.";
     return -1;
  }
  ifstream input(filename);
  if (!ifstream)
  {
     perror("Error opening file");
     return -1;
  }
  // everything ok here so you can proceed with the task
}
you can make a string with the path.
either

string dpath = "D:\\";
or something like that. This could be user input.
string filename = "test.txt"

then you can do
md5.digestFile (dpath+filename);

for an example.

But you need all the pieces. You can ask the user for the paths and filenames to make it more general purpose.

Checksums are not going to match a virus database. The checksum for the whole file, and then the checksum for the file with the virus, I am unaware of any way to convert that into a checksum for the virus to look up. All I can think of is to pull the differences in the file out, checksum only the differences, and run that against a virus database entry. But maybe I am not understanding what you are doing fully.


if you added
int main (int argc, char** argv) you could drag and drop to the program to get the filename etc, which may or may not be useful to your program or needs.
Last edited on
Topic archived. No new replies allowed.