aboutsummaryrefslogtreecommitdiffstats
path: root/examples/hash_fd.cpp
blob: 32acdbec3d51619cfabeef53e520a4e27abd7a93 (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
64
65
66
67
68
69
70
/*
* (C) 2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

/*
This is just like the normal hash application, but uses the Unix I/O
system calls instead of C++ iostreams. Previously, this version was
much faster and smaller, but GCC 3.1's libstdc++ seems to have been
improved enough that the difference is now fairly minimal.

Nicely enough, doing the change required changing only about 3 lines
of code.
*/

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

#if !defined(BOTAN_HAS_PIPE_UNIXFD_IO)
  #error "You didn't compile the pipe_unixfd module into Botan"
#endif

#include <fcntl.h>
#include <unistd.h>

int main(int argc, char* argv[])
   {
   if(argc < 3)
      {
      std::cout << "Usage: " << argv[0] << " digest <filenames>" << std::endl;
      return 1;
      }

   Botan::LibraryInitializer init;

   try
      {
      Botan::Pipe pipe(new Botan::Hash_Filter(argv[1]),
                       new Botan::Hex_Encoder);

      int skipped = 0;
      for(int j = 2; argv[j] != 0; j++)
         {
         int file = open(argv[j], O_RDONLY);
         if(file == -1)
            {
            std::cout << "ERROR: could not open " << argv[j] << std::endl;
            skipped++;
            continue;
            }
         pipe.start_msg();
         file >> pipe;
         pipe.end_msg();
         close(file);
         pipe.set_default_msg(j-2-skipped);
         std::cout << pipe << "  " << argv[j] << std::endl;
         }
   }
   catch(Botan::Algorithm_Not_Found)
      {
      std::cout << "Don't know about the hash function \"" << argv[1] << "\""
                << std::endl;
      }
   catch(std::exception& e)
      {
      std::cout << "Exception caught: " << e.what() << std::endl;
      }
   return 0;
   }