1

Trying to build a program that scans a directory and generates MD5 hash for each file. Using boost and crypto++ in C++14

Struggling to get my head around the path vectors and passing the individual items to the crypto++ FileSource function.

See examples below with a fixed path. If anyone could let me know how to convert the directory elements to a type that is accepted by the FileSource function in the for loop that would be great.

if (exists(p)) {                                             
  if (is_regular_file(p)) {     
    cout << "Please enter base path of directory only." << endl;
  }
  else if (is_directory(p)){           
    cout << "Scanning " << p << endl;

    typedef vector<path> vec;
    vec v;

    copy(directory_iterator(p), directory_iterator(), back_inserter(v));
    sort(v.begin(), v.end());

    for (vec::const_iterator it (v.begin()); it != v.end(); ++it)
      {
        string result;
        Weak::MD5 hash;
        FileSource("/home/howard/Documents/projects/copy_project/MultiCopy/main.cpp", true, new HashFilter(hash, new HexEncoder(new StringSink(result), false)));
        cout << "   " << *it << " : "  << result << endl;
      }

This works as expected, prints out the following:

Scanning "/home/howard/Documents/projects/copy_project/"
   "/home/howard/Documents/projects/copy_project/MultiCopy" : e4e336e768990b515a573dc5f60e5b8b
   "/home/howard/Documents/projects/copy_project/cryptopp_example" : e4e336e768990b515a573dc5f60e5b8b
   "/home/howard/Documents/projects/copy_project/md5_example" : e4e336e768990b515a573dc5f60e5b8b
   "/home/howard/Documents/projects/copy_project/md5_from_file" : e4e336e768990b515a573dc5f60e5b8b
   "/home/howard/Documents/projects/copy_project/scan_dir" : e4e336e768990b515a573dc5f60e5b8b

Tried below, but this does not work.

FileSource(*it, true, new HashFilter(hash, new HexEncoder(new StringSink(result), false)));
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Howard
  • 9
  • 3
  • 1
    This is from the documentation: FileSource (BufferedTransformation *attachment=NULL) FileSource (std::istream &in, bool pumpAll, BufferedTransformation *attachment=NULL) FileSource (const char *filename, bool pumpAll, BufferedTransformation *attachment=NULL, bool binary=true) – Howard May 02 '19 at 21:02
  • there is a string() function i have tried *i.string() also v[i].string and had no luck. More details here https://theboostcpplibraries.com/boost.filesystem-paths – Howard May 02 '19 at 21:10
  • fixed as per this post https://stackoverflow.com/questions/6826310/casting-a-boost-filesystem3-iterator-to-a-const-char – Howard May 02 '19 at 21:20
  • A heads up about `FileSource` and `const char *str = s.c_str(); //this is what you want`. Be careful about using `str`. The storage for `c_str()` may go out of scope at the end of that expression. My gut tells me ir may be better to use something like `FileSource((*it).string().c_str(), true, new HashFilter(...))`. That should ensure the `const char*` is available during the lifetime of the expression. – jww May 03 '19 at 01:33

0 Answers0