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


/**
Generate a 1024 bit DSA key and put it into a file. The public key
format is that specified by X.509, while the private key format is
PKCS #8.

The domain parameters are the ones specified as the Java default DSA
parameters. There is nothing special about these, it's just the only
1024-bit DSA parameter set that's included in Botan at the time of
this writing. The application always reads/writes all of the domain
parameters to/from the file, so a new set could be used without any
problems. We could generate a new set for each key, or read a set of
DSA params from a file and use those, but they mostly seem like
needless complications.
*/

#include <iostream>
#include <fstream>
#include <string>
#include <botan/botan.h>
#include <botan/dsa.h>
#include <botan/rng.h>
using namespace Botan;

#include <memory>

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

   Botan::LibraryInitializer init;

   std::ofstream priv("dsapriv.pem");
   std::ofstream pub("dsapub.pem");
   if(!priv || !pub)
      {
      std::cout << "Couldn't write output files" << std::endl;
      return 1;
      }

   try
      {
      AutoSeeded_RNG rng;

      DL_Group group(rng, DL_Group::DSA_Kosherizer, 2048, 256);

      DSA_PrivateKey key(rng, group);

      pub << X509::PEM_encode(key);
      if(argc == 1)
         priv << PKCS8::PEM_encode(key);
      else
         priv << PKCS8::PEM_encode(key, rng, argv[1]);
   }
   catch(std::exception& e)
      {
      std::cout << "Exception caught: " << e.what() << std::endl;
      }
   return 0;
   }