From 74c8b2300a6a63b5bca2332e8ff42565cd1601dc Mon Sep 17 00:00:00 2001 From: lloyd Date: Thu, 10 Sep 2009 00:44:36 +0000 Subject: Add example of the package transform --- doc/examples/package.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 doc/examples/package.cpp (limited to 'doc/examples') diff --git a/doc/examples/package.cpp b/doc/examples/package.cpp new file mode 100644 index 000000000..981abaa31 --- /dev/null +++ b/doc/examples/package.cpp @@ -0,0 +1,61 @@ + +#include +#include +#include + +#include +#include +#include + +using namespace Botan; + +std::vector slurp_file(const std::string& filename) + { + std::ifstream in(filename.c_str()); + + std::vector 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 input = slurp_file(argv[1]); + std::vector output(input.size() + cipher->BLOCK_SIZE); + + AllOrNothingTransform::package(rng, new Serpent, + &input[0], input.size(), + &output[0]); + + std::vector unpackage_output(output.size() - cipher->BLOCK_SIZE); + + AllOrNothingTransform::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"; + } -- cgit v1.2.3