aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/package.cpp
blob: 38a2e166611487aa1fe8ba4f8cc3bc9e3eb3d28e (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
*/

#include <botan/botan.h>
#include <botan/serpent.h>
#include <botan/package.h>

#include <iostream>
#include <fstream>
#include <vector>

using namespace Botan;

namespace {

std::vector<byte> slurp_file(const std::string& filename)
   {
   std::ifstream in(filename.c_str());

   std::vector<byte> out;
   byte buf[4096] = { 0 };

   while(in.good())
      {
      in.read((char*)buf, sizeof(buf));
      ssize_t got = in.gcount();

      out.insert(out.end(), buf, buf+got);
      }

   return out;
   }

}

int main(int argc, char* argv[])
   {
   if(argc != 2)
      {
      std::cout << "Usage: " << argv[0] << " filename\n";
      return 1;
      }

   LibraryInitializer init;

   AutoSeeded_RNG rng;

   BlockCipher* cipher = new Serpent;

   std::vector<byte> input = slurp_file(argv[1]);
   std::vector<byte> output(input.size() + cipher->block_size());

   aont_package(rng, new Serpent,
                &input[0], input.size(),
                &output[0]);

   std::vector<byte> unpackage_output(output.size() - cipher->block_size());

   aont_unpackage(new Serpent,
                  &output[0], output.size(),
                  &unpackage_output[0]);

   if(unpackage_output == input)
      std::cout << "Package/unpackage worked\n";
   else
      std::cout << "Something went wrong :(\n";
   }