From 72fce1ebf137e1a33bd39d221b79da1234d62cb9 Mon Sep 17 00:00:00 2001 From: lloyd Date: Sun, 7 Oct 2007 23:55:19 +0000 Subject: If the LibraryInitializer is created with just default arguments, don't bother creating it, just let it be initialized lazily when needed. Reindents in encypt.cpp and hash_fd.cpp (indenting the brace after a try statement). --- doc/examples/asn1.cpp | 2 -- doc/examples/base64.cpp | 2 -- doc/examples/bzip.cpp | 1 - doc/examples/ca.cpp | 2 -- doc/examples/decrypt.cpp | 3 -- doc/examples/dh.cpp | 2 -- doc/examples/dsa_kgen.cpp | 2 -- doc/examples/dsa_sign.cpp | 2 -- doc/examples/dsa_ver.cpp | 2 -- doc/examples/encrypt.cpp | 76 +++++++++++++++++++++++------------------------ doc/examples/factor.cpp | 2 -- doc/examples/hash.cpp | 2 -- doc/examples/hash_fd.cpp | 39 ++++++++++++------------ doc/examples/hasher.cpp | 2 -- doc/examples/hasher2.cpp | 2 -- doc/examples/passhash.cpp | 2 -- doc/examples/pkcs10.cpp | 2 -- doc/examples/rsa_dec.cpp | 3 -- doc/examples/rsa_enc.cpp | 3 -- doc/examples/rsa_kgen.cpp | 1 - doc/examples/self_sig.cpp | 2 -- doc/examples/stack.cpp | 2 -- doc/examples/x509info.cpp | 2 -- doc/examples/xor_ciph.cpp | 2 -- 24 files changed, 56 insertions(+), 104 deletions(-) (limited to 'doc/examples') diff --git a/doc/examples/asn1.cpp b/doc/examples/asn1.cpp index 84fb6b276..72b576653 100644 --- a/doc/examples/asn1.cpp +++ b/doc/examples/asn1.cpp @@ -58,8 +58,6 @@ int main(int argc, char* argv[]) } try { - LibraryInitializer init; - DataSource_Stream in(argv[1]); if(!PEM_Code::matches(in)) diff --git a/doc/examples/base64.cpp b/doc/examples/base64.cpp index c1260b8f4..8f9a06648 100644 --- a/doc/examples/base64.cpp +++ b/doc/examples/base64.cpp @@ -26,8 +26,6 @@ int main(int argc, char* argv[]) return 1; } - Botan::LibraryInitializer init; - int column = 78; bool wrap = false; bool encoding = true; diff --git a/doc/examples/bzip.cpp b/doc/examples/bzip.cpp index 46ac8abce..82bc3100a 100644 --- a/doc/examples/bzip.cpp +++ b/doc/examples/bzip.cpp @@ -29,7 +29,6 @@ int main(int argc, char* argv[]) return 1; } - Botan::LibraryInitializer init; std::vector files; bool decompress = false, small = false; diff --git a/doc/examples/ca.cpp b/doc/examples/ca.cpp index a28227357..62564df1a 100644 --- a/doc/examples/ca.cpp +++ b/doc/examples/ca.cpp @@ -28,8 +28,6 @@ int main(int argc, char* argv[]) } try { - LibraryInitializer init; - // set up our CA X509_Certificate ca_cert("cacert.pem"); std::auto_ptr privkey( diff --git a/doc/examples/decrypt.cpp b/doc/examples/decrypt.cpp index 84490cb1b..c5daeece5 100644 --- a/doc/examples/decrypt.cpp +++ b/doc/examples/decrypt.cpp @@ -80,9 +80,6 @@ int main(int argc, char* argv[]) std::string algo; try { - - LibraryInitializer init; - std::string header, salt_str, mac_str; std::getline(in, header); std::getline(in, algo); diff --git a/doc/examples/dh.cpp b/doc/examples/dh.cpp index 8dcc93f88..17dd29f56 100644 --- a/doc/examples/dh.cpp +++ b/doc/examples/dh.cpp @@ -14,8 +14,6 @@ using namespace Botan; int main() { try { - LibraryInitializer init; - // Alice creates a DH key and sends (the public part) to Bob DH_PrivateKey private_a(DL_Group("modp/ietf/1024")); DH_PublicKey public_a = private_a; // Bob gets this diff --git a/doc/examples/dsa_kgen.cpp b/doc/examples/dsa_kgen.cpp index 2e42c06b7..3dc55a443 100644 --- a/doc/examples/dsa_kgen.cpp +++ b/doc/examples/dsa_kgen.cpp @@ -40,8 +40,6 @@ int main(int argc, char* argv[]) } try { - LibraryInitializer init; - DSA_PrivateKey key(DL_Group("dsa/jce/1024")); pub << X509::PEM_encode(key); diff --git a/doc/examples/dsa_sign.cpp b/doc/examples/dsa_sign.cpp index 26f9e9dac..97d8e2918 100644 --- a/doc/examples/dsa_sign.cpp +++ b/doc/examples/dsa_sign.cpp @@ -48,8 +48,6 @@ int main(int argc, char* argv[]) return 1; } - LibraryInitializer init; - std::auto_ptr key( PKCS8::load_key(argv[1], passphrase) ); diff --git a/doc/examples/dsa_ver.cpp b/doc/examples/dsa_ver.cpp index fb6eb7079..8cc01c6dc 100644 --- a/doc/examples/dsa_ver.cpp +++ b/doc/examples/dsa_ver.cpp @@ -56,8 +56,6 @@ int main(int argc, char* argv[]) std::string sigstr; getline(sigfile, sigstr); - LibraryInitializer init; - std::auto_ptr key(X509::load_key(argv[1])); DSA_PublicKey* dsakey = dynamic_cast(key.get()); if(!dsakey) diff --git a/doc/examples/encrypt.cpp b/doc/examples/encrypt.cpp index c2cf2c5ba..7d64949f6 100644 --- a/doc/examples/encrypt.cpp +++ b/doc/examples/encrypt.cpp @@ -109,51 +109,49 @@ int main(int argc, char* argv[]) return 1; } - try { - - LibraryInitializer init; - - if(!have_block_cipher(algo)) + try { - std::cout << "Don't know about the block cipher \"" << algo << "\"\n"; - return 1; - } + if(!have_block_cipher(algo)) + { + std::cout << "Don't know about the block cipher \"" << algo << "\"\n"; + return 1; + } - const u32bit key_len = max_keylength_of(algo); - const u32bit iv_len = block_size_of(algo); - - std::auto_ptr s2k(get_s2k("PBKDF2(SHA-1)")); - s2k->set_iterations(8192); - s2k->new_random_salt(8); - - SymmetricKey bc_key = s2k->derive_key(key_len, "BLK" + passphrase); - InitializationVector iv = s2k->derive_key(iv_len, "IVL" + passphrase); - SymmetricKey mac_key = s2k->derive_key(16, "MAC" + passphrase); - - // Just to be all fancy we even write a (simple) header. - out << "-------- ENCRYPTED FILE --------" << std::endl; - out << algo << std::endl; - out << b64_encode(s2k->current_salt()) << std::endl; - - Pipe pipe(new Fork( - new Chain(new MAC_Filter("HMAC(SHA-1)", mac_key), - new Base64_Encoder - ), - new Chain(new Zlib_Compression, - get_cipher(algo + "/CBC", bc_key, iv, ENCRYPTION), - new Base64_Encoder(true) + const u32bit key_len = max_keylength_of(algo); + const u32bit iv_len = block_size_of(algo); + + std::auto_ptr s2k(get_s2k("PBKDF2(SHA-1)")); + s2k->set_iterations(8192); + s2k->new_random_salt(8); + + SymmetricKey bc_key = s2k->derive_key(key_len, "BLK" + passphrase); + InitializationVector iv = s2k->derive_key(iv_len, "IVL" + passphrase); + SymmetricKey mac_key = s2k->derive_key(16, "MAC" + passphrase); + + // Just to be all fancy we even write a (simple) header. + out << "-------- ENCRYPTED FILE --------" << std::endl; + out << algo << std::endl; + out << b64_encode(s2k->current_salt()) << std::endl; + + Pipe pipe(new Fork( + new Chain(new MAC_Filter("HMAC(SHA-1)", mac_key), + new Base64_Encoder + ), + new Chain(new Zlib_Compression, + get_cipher(algo + "/CBC", bc_key, iv, ENCRYPTION), + new Base64_Encoder(true) + ) ) - ) - ); + ); - pipe.start_msg(); - in >> pipe; - pipe.end_msg(); + pipe.start_msg(); + in >> pipe; + pipe.end_msg(); - out << pipe.read_all_as_string(0) << std::endl; - out << pipe.read_all_as_string(1); + out << pipe.read_all_as_string(0) << std::endl; + out << pipe.read_all_as_string(1); - } + } catch(Algorithm_Not_Found) { std::cout << "Don't know about the block cipher \"" << algo << "\"\n"; diff --git a/doc/examples/factor.cpp b/doc/examples/factor.cpp index bdd6319b7..6972ec108 100644 --- a/doc/examples/factor.cpp +++ b/doc/examples/factor.cpp @@ -119,8 +119,6 @@ int main(int argc, char* argv[]) try { - LibraryInitializer init; - BigInt n(argv[1]); std::vector factors = factorize(n); diff --git a/doc/examples/hash.cpp b/doc/examples/hash.cpp index a97cd6082..7c9500160 100644 --- a/doc/examples/hash.cpp +++ b/doc/examples/hash.cpp @@ -22,8 +22,6 @@ int main(int argc, char* argv[]) return 1; } - Botan::LibraryInitializer init; - std::string hash = argv[1]; /* a couple of special cases, kind of a crock */ if(hash == "sha1") hash = "SHA-1"; diff --git a/doc/examples/hash_fd.cpp b/doc/examples/hash_fd.cpp index 19d744287..d6487095a 100644 --- a/doc/examples/hash_fd.cpp +++ b/doc/examples/hash_fd.cpp @@ -33,29 +33,28 @@ int main(int argc, char* argv[]) 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++) + try { - int file = open(argv[j], O_RDONLY); - if(file == -1) + Botan::Pipe pipe(new Botan::Hash_Filter(argv[1]), + new Botan::Hex_Encoder); + + int skipped = 0; + for(int j = 2; argv[j] != 0; j++) { - std::cout << "ERROR: could not open " << argv[j] << std::endl; - skipped++; - continue; + 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; } - 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) { diff --git a/doc/examples/hasher.cpp b/doc/examples/hasher.cpp index 5ba982fc0..55a2ecd4b 100644 --- a/doc/examples/hasher.cpp +++ b/doc/examples/hasher.cpp @@ -20,8 +20,6 @@ int main(int argc, char* argv[]) return 1; } - Botan::LibraryInitializer init; - const int COUNT = 3; std::string name[COUNT] = { "MD5", "SHA-1", "RIPEMD-160" }; diff --git a/doc/examples/hasher2.cpp b/doc/examples/hasher2.cpp index 12d3c853d..e71ebf63e 100644 --- a/doc/examples/hasher2.cpp +++ b/doc/examples/hasher2.cpp @@ -23,8 +23,6 @@ int main(int argc, char* argv[]) return 1; } - Botan::LibraryInitializer init; - const int COUNT = 3; std::string name[COUNT] = { "MD5", "SHA-1", "RIPEMD-160" }; diff --git a/doc/examples/passhash.cpp b/doc/examples/passhash.cpp index 19b4abc40..03c1fdd24 100644 --- a/doc/examples/passhash.cpp +++ b/doc/examples/passhash.cpp @@ -18,8 +18,6 @@ int main(int argc, char* argv[]) try { - LibraryInitializer init; - if(argc == 2) std::cout << "H('" << argv[1] << "') = " << password_hash(argv[1]) << '\n'; else diff --git a/doc/examples/pkcs10.cpp b/doc/examples/pkcs10.cpp index 4639353df..a4f6efb04 100644 --- a/doc/examples/pkcs10.cpp +++ b/doc/examples/pkcs10.cpp @@ -26,8 +26,6 @@ int main(int argc, char* argv[]) } try { - LibraryInitializer init; - RSA_PrivateKey priv_key(1024); // If you want a DSA key instead of RSA, comment out the above line and // uncomment this one: diff --git a/doc/examples/rsa_dec.cpp b/doc/examples/rsa_dec.cpp index d50f6781a..77b5e5dd4 100644 --- a/doc/examples/rsa_dec.cpp +++ b/doc/examples/rsa_dec.cpp @@ -32,9 +32,6 @@ int main(int argc, char* argv[]) } try { - - LibraryInitializer init; - std::auto_ptr key(PKCS8::load_key(argv[1], argv[3])); RSA_PrivateKey* rsakey = dynamic_cast(key.get()); if(!rsakey) diff --git a/doc/examples/rsa_enc.cpp b/doc/examples/rsa_enc.cpp index 49b62989c..b24074038 100644 --- a/doc/examples/rsa_enc.cpp +++ b/doc/examples/rsa_enc.cpp @@ -65,9 +65,6 @@ int main(int argc, char* argv[]) } try { - - LibraryInitializer init; - std::auto_ptr key(X509::load_key(argv[1])); RSA_PublicKey* rsakey = dynamic_cast(key.get()); if(!rsakey) diff --git a/doc/examples/rsa_kgen.cpp b/doc/examples/rsa_kgen.cpp index 210eca52c..e57f60c06 100644 --- a/doc/examples/rsa_kgen.cpp +++ b/doc/examples/rsa_kgen.cpp @@ -42,7 +42,6 @@ int main(int argc, char* argv[]) try { - LibraryInitializer init; RSA_PrivateKey key(bits); pub << X509::PEM_encode(key); diff --git a/doc/examples/self_sig.cpp b/doc/examples/self_sig.cpp index c117ff3aa..d00bcb3b4 100644 --- a/doc/examples/self_sig.cpp +++ b/doc/examples/self_sig.cpp @@ -28,8 +28,6 @@ int main(int argc, char* argv[]) return 1; } - LibraryInitializer init; - std::string CA_flag = argv[2]; bool do_CA = false; diff --git a/doc/examples/stack.cpp b/doc/examples/stack.cpp index 1522b05f5..389b12e7e 100644 --- a/doc/examples/stack.cpp +++ b/doc/examples/stack.cpp @@ -20,8 +20,6 @@ int main(int argc, char* argv[]) return 1; } - Botan::LibraryInitializer init; - // this is a pretty vacuous example, but it's useful as a test Botan::Pipe pipe; diff --git a/doc/examples/x509info.cpp b/doc/examples/x509info.cpp index 343628922..6694ab5e1 100644 --- a/doc/examples/x509info.cpp +++ b/doc/examples/x509info.cpp @@ -57,8 +57,6 @@ int main(int argc, char* argv[]) } try { - LibraryInitializer init; - X509_Certificate cert(argv[1]); std::cout << "Version: " << cert.x509_version() << std::endl; diff --git a/doc/examples/xor_ciph.cpp b/doc/examples/xor_ciph.cpp index 92902a483..b9e46d91c 100644 --- a/doc/examples/xor_ciph.cpp +++ b/doc/examples/xor_ciph.cpp @@ -55,8 +55,6 @@ void XOR_Cipher::key(const byte key[], u32bit length) int main() { - LibraryInitializer init; - add_algorithm(new XOR_Cipher); // make it available to use global_config().add_alias("Vernam", "XOR"); // make Vernam an alias for XOR -- cgit v1.2.3