diff options
author | lloyd <[email protected]> | 2009-11-03 23:41:56 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2009-11-03 23:41:56 +0000 |
commit | 30f6169ebf9164a6fdb35519030975440a5b07d7 (patch) | |
tree | 8435e826dd692066c6c3fc56d02f8f4479188c02 /doc/examples | |
parent | cb0d4a18f2fc77a40f6055fedb43b78606068b7b (diff) | |
parent | 226d96ee4e64994beb9ec9436a29ac6656d61924 (diff) |
propagate from branch 'net.randombit.botan.1_8' (head 6e8c18515725a70923b34118951252723dd4c29a)
to branch 'net.randombit.botan' (head 77ba4ea5a4be36d6d029bcc852b2271edff0d679)
Diffstat (limited to 'doc/examples')
-rw-r--r-- | doc/examples/GNUmakefile | 4 | ||||
-rw-r--r-- | doc/examples/bench.cpp | 57 | ||||
-rw-r--r-- | doc/examples/cms_dec.cpp | 2 | ||||
-rw-r--r-- | doc/examples/cpuid.cpp | 17 | ||||
-rw-r--r-- | doc/examples/package.cpp | 61 | ||||
-rw-r--r-- | doc/examples/tss.cpp | 38 |
6 files changed, 148 insertions, 31 deletions
diff --git a/doc/examples/GNUmakefile b/doc/examples/GNUmakefile index d854c81d8..44fcfeea5 100644 --- a/doc/examples/GNUmakefile +++ b/doc/examples/GNUmakefile @@ -2,8 +2,8 @@ BOTAN_CONFIG = botan-config CXX = g++ -CFLAGS = -O2 -ansi -W -Wall $(shell $(BOTAN_CONFIG) --cflags) -LIBS = $(shell $(BOTAN_CONFIG) --libs) +CFLAGS = -O2 -ansi -W -Wall -I../../build/include +LIBS = -L../.. -lbotan SRCS=$(wildcard *.cpp) diff --git a/doc/examples/bench.cpp b/doc/examples/bench.cpp index 37ef1104d..cc43fade0 100644 --- a/doc/examples/bench.cpp +++ b/doc/examples/bench.cpp @@ -7,26 +7,6 @@ using namespace Botan; #include <iostream> -double best_speed(const std::string& algorithm, - u32bit milliseconds, - RandomNumberGenerator& rng, - Timer& timer) - { - std::map<std::string, double> speeds = - algorithm_benchmark(algorithm, milliseconds, - timer, rng, - global_state().algorithm_factory()); - - double best_time = 0; - - for(std::map<std::string, double>::const_iterator i = speeds.begin(); - i != speeds.end(); ++i) - if(i->second > best_time) - best_time = i->second; - - return best_time; - } - const std::string algos[] = { "AES-128", "AES-192", @@ -62,7 +42,6 @@ const std::string algos[] = { "FORK-256", "GOST-34.11", "HAS-160", - "HAS-V", "MD2", "MD4", "MD5", @@ -81,18 +60,40 @@ const std::string algos[] = { "", }; -int main() +void benchmark_algo(const std::string& algo, + RandomNumberGenerator& rng) + { + u32bit milliseconds = 3000; + Default_Benchmark_Timer timer; + Algorithm_Factory& af = global_state().algorithm_factory(); + + std::map<std::string, double> speeds = + algorithm_benchmark(algo, milliseconds, timer, rng, af); + + std::cout << algo << ":"; + + for(std::map<std::string, double>::const_iterator i = speeds.begin(); + i != speeds.end(); ++i) + { + std::cout << " " << i->second << " [" << i->first << "]"; + } + std::cout << "\n"; + } + +int main(int argc, char* argv[]) { LibraryInitializer init; - u32bit milliseconds = 1000; AutoSeeded_RNG rng; - Default_Benchmark_Timer timer; - for(u32bit i = 0; algos[i] != ""; ++i) + if(argc == 1) // no args, benchmark everything + { + for(u32bit i = 0; algos[i] != ""; ++i) + benchmark_algo(algos[i], rng); + } + else { - std::string algo = algos[i]; - std::cout << algo << ' ' - << best_speed(algo, milliseconds, rng, timer) << "\n"; + for(int i = 1; argv[i]; ++i) + benchmark_algo(argv[i], rng); } } diff --git a/doc/examples/cms_dec.cpp b/doc/examples/cms_dec.cpp index 3fa285395..de4d732f0 100644 --- a/doc/examples/cms_dec.cpp +++ b/doc/examples/cms_dec.cpp @@ -10,7 +10,7 @@ int main(int argc, char* argv[]) { if(argc != 2) { - printf("Usage: %s <filename>\n", argv[0]); + std::cout << "Usage: " << argv[0] << " <filename>\n"; return 1; } diff --git a/doc/examples/cpuid.cpp b/doc/examples/cpuid.cpp new file mode 100644 index 000000000..1bdee787c --- /dev/null +++ b/doc/examples/cpuid.cpp @@ -0,0 +1,17 @@ +#include <stdio.h> + +#include <botan/cpuid.h> + +using namespace Botan; + +int main() + { + printf("Cache line size: %d\n", CPUID::cache_line_size()); + printf("RDTSC: %d\n", CPUID::has_rdtsc()); + printf("SSE2 %d\n", CPUID::has_sse2()); + printf("SSSE3 %d\n", CPUID::has_ssse3()); + printf("SSE41 %d\n", CPUID::has_sse41()); + printf("SSE42 %d\n", CPUID::has_sse42()); + + printf("AltiVec %d\n", CPUID::has_altivec()); + } diff --git a/doc/examples/package.cpp b/doc/examples/package.cpp new file mode 100644 index 000000000..866dd7e96 --- /dev/null +++ b/doc/examples/package.cpp @@ -0,0 +1,61 @@ + +#include <botan/botan.h> +#include <botan/serpent.h> +#include <botan/package.h> + +#include <iostream> +#include <fstream> +#include <vector> + +using namespace Botan; + +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); + + AllOrNothingTransform::package(rng, new Serpent, + &input[0], input.size(), + &output[0]); + + std::vector<byte> 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"; + } diff --git a/doc/examples/tss.cpp b/doc/examples/tss.cpp new file mode 100644 index 000000000..1881ffe24 --- /dev/null +++ b/doc/examples/tss.cpp @@ -0,0 +1,38 @@ +#include <botan/botan.h> +#include <botan/tss.h> +#include <iostream> +#include <stdio.h> + +namespace { + +void print(const Botan::SecureVector<Botan::byte>& r) + { + for(Botan::u32bit i = 0; i != r.size(); ++i) + printf("%02X", r[i]); + printf("\n"); + } + +} + +int main() + { + using namespace Botan; + + LibraryInitializer init; + + AutoSeeded_RNG rng; + + byte id[16]; + for(int i = 0; i != 16; ++i) + id[i] = i; + + const byte S2[] = { 0xDE, 0xAD, 0xCA, 0xFE, 0xBA, 0xBE, 0xBE, 0xEF }; + + std::vector<RTSS_Share> shares = + RTSS_Share::split(4, 6, S2, sizeof(S2), id, rng); + + for(size_t i = 0; i != shares.size(); ++i) + std::cout << i << " = " << shares[i].to_string() << "\n"; + + print(RTSS_Share::reconstruct(shares)); + } |