open-license-manager
2014-09-15 345c7b764f0721e9bbc993a3cd7f803fd42cf943
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include "../base_lib/CryptoHelper.h"
#include <string>
#include <stdlib.h>
#include <iostream> 
 
using namespace std;
namespace license {
 
void write_pubkey_file(const string& public_fname, const string& pbPublicKey) {
    FILE* fp = fopen(public_fname.c_str(), "w");
    if (fp == NULL) {
        throw ios_base::failure(string("can't create :") + public_fname);
    }
    fprintf(fp, "//file generated by bootstrap.cpp, do not edit.\n\n");
    fprintf(fp, "#ifndef PUBLIC_KEY_H_\n#define PUBLIC_KEY_H_\n");
    fprintf(fp, "define PUBLIC_KEY { \n");
    fprintf(fp, "%s", pbPublicKey.c_str());
    fprintf(fp, "\n};\n\n");
    int random = rand() % 1000;
    fprintf(fp, "#define SHARED_RANDOM %d;\n", random);
    fprintf(fp, "#endif\n");
    fclose(fp);
}
 
void write_privkey_file(const string& private_fname, const string& privateKey) {
    FILE* fp = fopen(private_fname.c_str(), "w");
    if (fp == NULL) {
        throw ios_base::failure(string("can't create :") + private_fname);
    }
    fprintf(fp, "//file generated by bootstrap.cpp, do not edit.\n\n");
    fprintf(fp, "#ifndef PRIVATE_KEY_H_\n#define PRIVATE_KEY_H_\n");
    fprintf(fp, "define PRIVATE_KEY = {\n");
    fprintf(fp, "%s", privateKey.c_str());
    fprintf(fp, "\n};\n\n");
    fprintf(fp, "#endif\n");
    fclose(fp);
}
 
void generatePk(string private_include, string public_include) {
    unique_ptr<CryptoHelper> cryptoHlpr = CryptoHelper::getInstance();
 
    try {
        cryptoHlpr->generateKeyPair();
    } catch (exception &e) {
        cerr << endl << "Error generating key pair: " << e.what() << endl
                << "aborting" << endl;
        exit(2);
    }
 
    try {
        const string pubKey = cryptoHlpr->exportPublicKey();
        write_pubkey_file(public_include, pubKey);
        // Print out the public key to console as a
        // hexadecimal string.
        cout << endl << "PublicKey" << pubKey.c_str() << endl;
    } catch (exception &e) {
        cerr << endl << "Error exporting public key: " << e.what() << endl
                << "aborting." << endl;
        exit(4);
    }
 
    try {
        const string privKey = cryptoHlpr->exportPrivateKey();
        write_privkey_file(private_include, privKey);
    } catch (exception &e) {
        cerr << endl << "Error exporting private key: " << e.what() << endl
                << "aborting" << endl;
        exit(5);
    }
 
    return;
}
}
 
int main(int argc, char** argv) {
 
    if (argc != 3) {
        //print_usage();
        exit(2);
    } else {
        printf("********************************************\n");
        printf("*  Bootstrap!!!                            *\n");
        printf("********************************************\n");
 
    }
    string private_fname = string(argv[1]);
    string public_fname(argv[2]);
 
    license::generatePk(private_fname, public_fname);
    return 0;
}