aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-06-27 19:29:19 +0000
committerlloyd <[email protected]>2008-06-27 19:29:19 +0000
commitcbcebfaad9ab6dc22186e93d259c6beed7fcff88 (patch)
tree73540a68f94fc397289c034f0babb0c645cecacc /doc/examples
parente2a465b75d8baeac912e3f4d428ebc5e03fd76f1 (diff)
Update some of the examples for the recent API changes
Diffstat (limited to 'doc/examples')
-rw-r--r--doc/examples/ca.cpp7
-rw-r--r--doc/examples/dh.cpp14
-rw-r--r--doc/examples/dsa_kgen.cpp10
-rw-r--r--doc/examples/dsa_sign.cpp4
-rw-r--r--doc/examples/factor.cpp19
-rw-r--r--doc/examples/rsa_kgen.cpp7
-rw-r--r--doc/examples/self_sig.cpp9
7 files changed, 42 insertions, 28 deletions
diff --git a/doc/examples/ca.cpp b/doc/examples/ca.cpp
index 5e465d245..0bcb9a38e 100644
--- a/doc/examples/ca.cpp
+++ b/doc/examples/ca.cpp
@@ -14,6 +14,7 @@
*/
#include <botan/botan.h>
+#include <botan/rng.h>
#include <botan/x509_ca.h>
#include <botan/util.h>
using namespace Botan;
@@ -37,10 +38,12 @@ int main(int argc, char* argv[])
const std::string arg_ca_key = argv[3];
const std::string arg_req_file = argv[4];
+ std::auto_ptr<RandomNumberGenerator> rng(make_rng());
+
X509_Certificate ca_cert(arg_ca_cert);
std::auto_ptr<PKCS8_PrivateKey> privkey(
- PKCS8::load_key(arg_ca_key, arg_passphrase)
+ PKCS8::load_key(arg_ca_key, *rng, arg_passphrase)
);
X509_CA ca(ca_cert, *privkey);
@@ -55,7 +58,7 @@ int main(int argc, char* argv[])
X509_Time start_time(system_time());
X509_Time end_time(system_time() + 365 * 60 * 60 * 24);
- X509_Certificate new_cert = ca.sign_request(req, start_time, end_time);
+ X509_Certificate new_cert = ca.sign_request(req, *rng, start_time, end_time);
// send the new cert back to the requestor
std::cout << new_cert.PEM_encode();
diff --git a/doc/examples/dh.cpp b/doc/examples/dh.cpp
index c8e13dbb4..1957215bc 100644
--- a/doc/examples/dh.cpp
+++ b/doc/examples/dh.cpp
@@ -7,22 +7,24 @@
*/
#include <botan/botan.h>
#include <botan/dh.h>
-#include <botan/libstate.h>
+#include <botan/rng.h>
using namespace Botan;
#include <iostream>
+#include <memory>
int main()
{
- try {
+ try
+ {
+ std::auto_ptr<RandomNumberGenerator> rng(make_rng());
+
// Alice creates a DH key and sends (the public part) to Bob
- DH_PrivateKey private_a(DL_Group("modp/ietf/1024"),
- global_state().prng_reference());
+ DH_PrivateKey private_a(*rng, DL_Group("modp/ietf/1024"));
DH_PublicKey public_a = private_a; // Bob gets this
// Bob creates a key with a matching group
- DH_PrivateKey private_b(public_a.get_domain(),
- global_state().prng_reference());
+ DH_PrivateKey private_b(*rng, public_a.get_domain());
// Bob sends the key back to Alice
DH_PublicKey public_b = private_b; // Alice gets this
diff --git a/doc/examples/dsa_kgen.cpp b/doc/examples/dsa_kgen.cpp
index c078d7fa3..0a380cd9e 100644
--- a/doc/examples/dsa_kgen.cpp
+++ b/doc/examples/dsa_kgen.cpp
@@ -21,9 +21,11 @@ This file is in the public domain
#include <string>
#include <botan/botan.h>
#include <botan/dsa.h>
-#include <botan/libstate.h>
+#include <botan/rng.h>
using namespace Botan;
+#include <memory>
+
int main(int argc, char* argv[])
{
if(argc != 1 && argc != 2)
@@ -42,14 +44,14 @@ int main(int argc, char* argv[])
try
{
- DSA_PrivateKey key(DL_Group("dsa/jce/1024"),
- global_state().prng_reference());
+ std::auto_ptr<RandomNumberGenerator> rng(make_rng());
+ DSA_PrivateKey key(*rng, DL_Group("dsa/jce/1024"));
pub << X509::PEM_encode(key);
if(argc == 1)
priv << PKCS8::PEM_encode(key);
else
- priv << PKCS8::PEM_encode(key, argv[1]);
+ priv << PKCS8::PEM_encode(key, *rng, argv[1]);
}
catch(std::exception& e)
{
diff --git a/doc/examples/dsa_sign.cpp b/doc/examples/dsa_sign.cpp
index 97d8e2918..67800497e 100644
--- a/doc/examples/dsa_sign.cpp
+++ b/doc/examples/dsa_sign.cpp
@@ -48,8 +48,10 @@ int main(int argc, char* argv[])
return 1;
}
+ std::auto_ptr<RandomNumberGenerator> rng(make_rng());
+
std::auto_ptr<PKCS8_PrivateKey> key(
- PKCS8::load_key(argv[1], passphrase)
+ PKCS8::load_key(argv[1], *rng, passphrase)
);
DSA_PrivateKey* dsakey = dynamic_cast<DSA_PrivateKey*>(key.get());
diff --git a/doc/examples/factor.cpp b/doc/examples/factor.cpp
index 2c9d94fa9..63bd1b00f 100644
--- a/doc/examples/factor.cpp
+++ b/doc/examples/factor.cpp
@@ -5,20 +5,20 @@
#include <botan/botan.h>
#include <botan/reducer.h>
#include <botan/numthry.h>
-#include <botan/libstate.h>
using namespace Botan;
#include <algorithm>
#include <iostream>
+#include <memory>
// Pollard's Rho algorithm, as described in the MIT algorithms book
// We use (x^2+x) mod n instead of (x*2-1) mod n as the random function,
// it _seems_ to lead to faster factorization for the values I tried.
-BigInt rho(const BigInt& n)
+BigInt rho(const BigInt& n, RandomNumberGenerator& rng)
{
- BigInt x = random_integer(global_state().prng_reference(), 0, n-1);
+ BigInt x = random_integer(rng, 0, n-1);
BigInt y = x;
BigInt d = 0;
@@ -84,14 +84,15 @@ std::vector<BigInt> remove_small_factors(BigInt& n)
return factors;
}
-std::vector<BigInt> factorize(const BigInt& n_in)
+std::vector<BigInt> factorize(const BigInt& n_in,
+ RandomNumberGenerator& rng)
{
BigInt n = n_in;
std::vector<BigInt> factors = remove_small_factors(n);
while(n != 1)
{
- if(is_prime(n, global_state().prng_reference()))
+ if(is_prime(n, rng))
{
factors.push_back(n);
break;
@@ -99,9 +100,9 @@ std::vector<BigInt> factorize(const BigInt& n_in)
BigInt a_factor = 0;
while(a_factor == 0)
- a_factor = rho(n);
+ a_factor = rho(n, rng);
- std::vector<BigInt> rho_factored = factorize(a_factor);
+ std::vector<BigInt> rho_factored = factorize(a_factor, rng);
for(u32bit j = 0; j != rho_factored.size(); j++)
factors.push_back(rho_factored[j]);
@@ -122,7 +123,9 @@ int main(int argc, char* argv[])
{
BigInt n(argv[1]);
- std::vector<BigInt> factors = factorize(n);
+ std::auto_ptr<RandomNumberGenerator> rng(make_rng());
+
+ std::vector<BigInt> factors = factorize(n, *rng);
std::sort(factors.begin(), factors.end());
std::cout << n << ": ";
diff --git a/doc/examples/rsa_kgen.cpp b/doc/examples/rsa_kgen.cpp
index 13d602d66..7230e7a4a 100644
--- a/doc/examples/rsa_kgen.cpp
+++ b/doc/examples/rsa_kgen.cpp
@@ -13,10 +13,10 @@ This file is in the public domain
#include <fstream>
#include <string>
#include <cstdlib>
+#include <memory>
#include <botan/botan.h>
#include <botan/rsa.h>
-#include <botan/libstate.h>
using namespace Botan;
int main(int argc, char* argv[])
@@ -45,13 +45,14 @@ int main(int argc, char* argv[])
try
{
- RSA_PrivateKey key(bits, global_state().prng_reference());
+ std::auto_ptr<RandomNumberGenerator> rng(make_rng());
+ RSA_PrivateKey key(*rng, bits);
pub << X509::PEM_encode(key);
if(argc == 2)
priv << PKCS8::PEM_encode(key);
else
- priv << PKCS8::PEM_encode(key, argv[2]);
+ priv << PKCS8::PEM_encode(key, *rng, argv[2]);
}
catch(std::exception& e)
{
diff --git a/doc/examples/self_sig.cpp b/doc/examples/self_sig.cpp
index c2118be40..91aa4b2ea 100644
--- a/doc/examples/self_sig.cpp
+++ b/doc/examples/self_sig.cpp
@@ -13,11 +13,11 @@ This file is in the public domain
#include <botan/x509self.h>
#include <botan/rsa.h>
#include <botan/dsa.h>
-#include <botan/libstate.h>
using namespace Botan;
#include <iostream>
#include <fstream>
+#include <memory>
int main(int argc, char* argv[])
{
@@ -42,10 +42,11 @@ int main(int argc, char* argv[])
try
{
- RSA_PrivateKey key(1024, global_state().prng_reference());
+ std::auto_ptr<RandomNumberGenerator> rng(make_rng());
+ RSA_PrivateKey key(*rng, 1024);
std::ofstream priv_key("private.pem");
- priv_key << PKCS8::PEM_encode(key, argv[1]);
+ priv_key << PKCS8::PEM_encode(key, *rng, argv[1]);
X509_Cert_Options opts;
@@ -60,7 +61,7 @@ int main(int argc, char* argv[])
if(do_CA)
opts.CA_key();
- X509_Certificate cert = X509::create_self_signed_cert(opts, key);
+ X509_Certificate cert = X509::create_self_signed_cert(opts, key, *rng);
std::ofstream cert_file("cert.pem");
cert_file << cert.PEM_encode();