aboutsummaryrefslogtreecommitdiffstats
path: root/src/cli/hash.cpp
blob: 81a72ca17caadc9194abcc31a3267a3c76e566ad (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* (C) 2009 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "apps.h"

#if defined(BOTAN_HAS_CODEC_FILTERS)

#include <botan/filters.h>
#include <iostream>
#include <fstream>

using namespace Botan;

namespace {

int hash(const std::vector<std::string> &args)
   {
   if(args.size() < 3)
      {
      std::cout << "Usage: " << args[0] << " digest <filenames>" << std::endl;
      return 1;
      }

   std::string hash = args[1];
   /* a couple of special cases, kind of a crock */
   if(hash == "sha1") hash = "SHA-1";
   if(hash == "md5")  hash = "MD5";

   try {
      Pipe pipe(new Hash_Filter(hash), new Hex_Encoder);

      int skipped = 0;
      for(int j = 2; j < args.size(); j++)
         {
         std::ifstream file(args[j], std::ios::binary);
         if(!file)
            {
            std::cout << "ERROR: could not open " << args[j] << std::endl;
            skipped++;
            continue;
            }
         pipe.start_msg();
         file >> pipe;
         pipe.end_msg();
         pipe.set_default_msg(j-2-skipped);
         std::cout << pipe << "  " << args[j] << std::endl;
         }
   }
   catch(std::exception& e)
      {
      std::cout << "Exception caught: " << e.what() << std::endl;
      }
   return 0;
   }

REGISTER_APP(hash);

}

#endif // BOTAN_HAS_CODEC_FILTERS