File: driver.cc

package info (click to toggle)
bobcat 6.11.00-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,292 kB
  • sloc: cpp: 21,370; fortran: 6,507; makefile: 2,787; sh: 724; perl: 401; ansic: 26
file content (74 lines) | stat: -rw-r--r-- 2,007 bytes parent folder | download | duplicates (2)
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
71
72
73
74

#include <fstream>
#include <iostream>

#include <bobcat/exception>
#include <bobcat/diffiehellman>

using namespace FBB;
using namespace std;

int main(int argc, char **argv)
try
{

    if (argc == 1)
    {
        cout << "1: create prime and generator, write to 'params'\n"
                "2: create secret and public parts, arg 2: 0 or 1,\n"
                "   write secret and public parts to <arg 2>.sec and "
                                                     "<arg 2>.pub\n"
                "3: create common key arg 2: 0 or 1,\n"
                "   0: write common0 using 0.pub, 0.sec and 1.pub\n"
                "   1: write common1 using 1.pub, 1.sec and 0.pub\n"
                ;
        return 0;
    }


    switch (*argv[1])                       // using generator == 5
    {
        case '1':
        {
            ofstream out = Exception::factory<ofstream>("params");
            out << hex << DiffieHellman::prime(1024, true, true) << '\n';
        }
        break;

        case '2':
        {
            char *nr = argv[2];

            if (nr == 0 || "01"s.find(*nr) == string::npos)
                throw Exception{} << "mode '2' needs 0 or 1 as 2nd argument";

            ifstream in = Exception::factory<ifstream>("params");
            BigInt prime;
            in >> hex >> prime;

            DiffieHellman dh{ prime };
            dh.save(nr);
        }
        break;

        case '3':
        {
            char *nr = argv[2];

            if (nr == 0 || "01"s.find(*nr) == string::npos)
                throw Exception{} << "mode '3' needs 0 or 1 as 2nd argument";

            DiffieHellman dh{ nr + ".pub"s, nr + ".sec"s };
            cout << "common key computed by " << nr << ":\n" <<
                hex << dh.key((nr[0] == '0' ? '1' : '0') + ".pub"s) << '\n';
        }
        break;

        default:
        throw Exception{} << "undefined action `" << *argv[1] <<'\'';
    }
}
catch (std::exception const &exc)
{
    std::cout << exc.what() << '\n';
}