~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to doc/examples/dsa_ver.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Grab an DSA public key from the file given as an argument, grab a signature
3
 
from another file, and verify the message (which, suprise, is also in a file).
4
 
 
5
 
The signature format isn't particularly standard, but it's not bad. It's simply
6
 
the IEEE 1363 signature format, encoded into base64 with a trailing newline
7
 
 
8
 
Written by Jack Lloyd (lloyd@randombit.net), August 5, 2002
9
 
   Updated to use X.509 format keys, October 21, 2002
10
 
 
11
 
This file is in the public domain
12
 
*/
13
 
 
14
 
#include <iostream>
15
 
#include <iomanip>
16
 
#include <fstream>
17
 
#include <cstdlib>
18
 
#include <string>
19
 
 
20
 
#include <botan/botan.h>
21
 
#include <botan/look_pk.h>
22
 
#include <botan/dsa.h>
23
 
using namespace Botan;
24
 
 
25
 
SecureVector<byte> b64_decode(const std::string& in)
26
 
   {
27
 
   Pipe pipe(new Base64_Decoder);
28
 
   pipe.process_msg(in);
29
 
   return pipe.read_all();
30
 
   }
31
 
 
32
 
int main(int argc, char* argv[])
33
 
   {
34
 
   if(argc != 4)
35
 
      {
36
 
      std::cout << "Usage: " << argv[0]
37
 
                << " keyfile messagefile sigfile" << std::endl;
38
 
      return 1;
39
 
      }
40
 
 
41
 
   std::ifstream message(argv[2]);
42
 
   if(!message)
43
 
      {
44
 
      std::cout << "Couldn't read the message file." << std::endl;
45
 
      return 1;
46
 
      }
47
 
 
48
 
   std::ifstream sigfile(argv[3]);
49
 
   if(!sigfile)
50
 
      {
51
 
      std::cout << "Couldn't read the signature file." << std::endl;
52
 
      return 1;
53
 
      }
54
 
 
55
 
   try {
56
 
      std::string sigstr;
57
 
      getline(sigfile, sigstr);
58
 
 
59
 
      LibraryInitializer init;
60
 
 
61
 
      std::auto_ptr<X509_PublicKey> key(X509::load_key(argv[1]));
62
 
      DSA_PublicKey* dsakey = dynamic_cast<DSA_PublicKey*>(key.get());
63
 
      if(!dsakey)
64
 
         {
65
 
         std::cout << "The loaded key is not a DSA key!\n";
66
 
         return 1;
67
 
         }
68
 
 
69
 
      SecureVector<byte> sig = b64_decode(sigstr);
70
 
 
71
 
      Pipe pipe(new PK_Verifier_Filter(
72
 
                   get_pk_verifier(*dsakey, "EMSA1(SHA-1)"), sig
73
 
                   )
74
 
         );
75
 
 
76
 
      pipe.start_msg();
77
 
      message >> pipe;
78
 
      pipe.end_msg();
79
 
 
80
 
      byte result = 0;
81
 
      pipe.read(result);
82
 
 
83
 
      if(result)
84
 
         std::cout << "Signature verified\n";
85
 
      else
86
 
         std::cout << "Signature did NOT verify\n";
87
 
   }
88
 
   catch(std::exception& e)
89
 
      {
90
 
      std::cout << "Exception caught: " << e.what() << std::endl;
91
 
      return 1;
92
 
      }
93
 
   return 0;
94
 
   }