C++, Linux

Calculate the SHA1 hash of your file using C++

We are going to use the boost and crypto++ libraries to get the job done.

Assuming that you have those two libraries already installed:
The code below is licensed under GPLv3.
sha1sum.cpp (sha1sum)

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
26
27
28
#include <iostream>
#include <boost/filesystem.hpp>
#include <cryptopp/sha.h>
#include <cryptopp/hex.h>
#include <cryptopp/files.h>
using namespace std;
using namespace boost::filesystem;
 
string hashFile(const path &file);
 
int main( int argc, char** argv) {
  string hash("");
  path file("plaintext.txt");
  hash = hashFile(file);
  cout<<hash<<endl;
  return 0;
}
 
string hashFile(const path &file)
{
  string result;
  CryptoPP::SHA1 hash;
  CryptoPP::FileSource(file.string().c_str(),true, 
	new CryptoPP::HashFilter(hash, new CryptoPP::HexEncoder(
	new CryptoPP::StringSink(result), true)));
  return result;
 
}

To compile it:
g++ -o sha1sum sha1sum.cpp -lcryptopp -lboost_system -lboost_filesystem

To test it, you need a plaintext.txt file.