From 01a62b850a76916dae66f7f52e1d1e515584e7ce Mon Sep 17 00:00:00 2001
From: open-license-manager <rillf@maildrop.cc>
Date: 摹曛, 07 8月 2014 23:27:40 +0800
Subject: [PATCH] added boost

---
 src/bootstrap/win/Main.cpp |   83 +++++++++++++++++++++++++++++++++++++++--
 1 files changed, 79 insertions(+), 4 deletions(-)

diff --git a/src/bootstrap/win/Main.cpp b/src/bootstrap/win/Main.cpp
index 5e24ca7..c910a2f 100644
--- a/src/bootstrap/win/Main.cpp
+++ b/src/bootstrap/win/Main.cpp
@@ -1,13 +1,58 @@
 #include <stdio.h>
 #include "CryptoHelper.h"
+#include <string>
+#include <stdlib.h>
+#include <iostream> 
 
-void main() {
+using namespace std;
+
+void write_pubkey_file(string public_fname, BYTE *pbPublicKey, DWORD dwPublicKeySize){
+	FILE* fp = fopen(public_fname.c_str(), "w");
+	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, "static BYTE PUBLIC_KEY[] = {");
+	for (int i = 0; i < dwPublicKeySize; i++){
+		if (i != 0){
+			fprintf(fp, ",");
+		}
+		if (i % 15 == 0){
+			fprintf(fp, "\n        ");
+		}
+		fprintf(fp, "%d", pbPublicKey[i]);
+	}
+	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(string private_fname, BYTE *privateKey, DWORD dwPrivateKeySize){
+	FILE* fp = fopen(private_fname.c_str(), "w");
+	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, "static BYTE PRIVATE_KEY[] = {");
+	for (int i = 0; i < dwPrivateKeySize; i++){
+		if (i != 0){
+			fprintf(fp, ",");
+		}
+		if (i % 15 == 0){
+			fprintf(fp, "\n        ");
+		}
+		fprintf(fp, "%d", privateKey[i]);
+	}
+	fprintf(fp, "\n};\n\n");
+	fprintf(fp, "#endif\n");
+	fclose(fp);
+}
+
+void generatePk(string private_include, string public_include) {
 	CryptoHelper cryptoHlpr;
 	BYTE *pbPublicKey = NULL, *pbPrivateKey = NULL;
 	DWORD dwPublicKeySize = 0, dwPrivateKeySize = 0;
 	HRESULT hr = S_OK;
 	// Get the key container context.
-	if (FAILED(hr = cryptoHlpr.AcquireContext((L"TestContainer")))) {
+	if (FAILED(hr = cryptoHlpr.AcquireContext(_T("TestContainer")))) {
 // Call FormatMessage to display the error returned in hr.
 		return;
 	}
@@ -20,7 +65,11 @@
 	if (FAILED(
 			hr = cryptoHlpr.ExportPublicKey(&pbPublicKey, dwPublicKeySize))) {
 // Call FormatMessage to display the error returned in hr.
+		cerr << "error exporting pubkey" << endl;
 		return;
+	}
+	else {
+		write_pubkey_file(public_include, pbPublicKey, dwPublicKeySize);
 	}
 	// Print out the public key to console as a
 	// hexadecimal string.
@@ -30,11 +79,17 @@
 	}
 	wprintf(L"\"\n");
 	// Export out the private key blob.
-	cryptoHlpr.ExportPrivateKey(&pbPrivateKey, dwPrivateKeySize);
+	if (FAILED(cryptoHlpr.ExportPrivateKey(&pbPrivateKey, dwPrivateKeySize))){
+		cerr << "Error exporting private key." << endl;
+		return;
+	}
+	else{
+		write_privkey_file(private_include, pbPrivateKey, dwPrivateKeySize);
+	}
 	// Print out the private key to console as a
 	// hexadecimal string.
 	wprintf(L"\n\nPrivateKey = \"");
-	for (i = 0; i < dwPrivateKeySize; i++) {
+	for (DWORD i = 0; i < dwPrivateKeySize; i++) {
 		wprintf(L"%02x", pbPrivateKey[i]);
 	}
 	wprintf(L"\"\n");
@@ -48,3 +103,23 @@
 		delete[] pbPrivateKey;
 	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]);
+
+	generatePk(private_fname, public_fname);
+	return 0;
+}

--
Gitblit v1.9.1