diff options
author | Gluzskiy Alexandr <sss123next@list.ru> | 2010-10-27 06:31:24 +0300 |
---|---|---|
committer | Gluzskiy Alexandr <sss123next@list.ru> | 2010-10-27 06:31:24 +0300 |
commit | c7417593521a2a96442e97508354e32f502bd931 (patch) | |
tree | e0041648790fa8aa07215eccc40e5f2714d9e97a | |
parent | fea62665b2057d0be6bdffbb04e986d3b1792da4 (diff) |
random key generation
-rw-r--r-- | commonheaders.h | 4 | ||||
-rw-r--r-- | init.cpp | 2 | ||||
-rw-r--r-- | main.cpp | 112 | ||||
-rw-r--r-- | new_gpg.rc | 15 | ||||
-rw-r--r-- | new_gpg.sln | 6 | ||||
-rw-r--r-- | resource.h | 3 | ||||
-rw-r--r-- | utilities.cpp | 11 | ||||
-rw-r--r-- | utilities.h | 1 |
8 files changed, 136 insertions, 18 deletions
diff --git a/commonheaders.h b/commonheaders.h index 713622f..3670a86 100644 --- a/commonheaders.h +++ b/commonheaders.h @@ -44,6 +44,10 @@ using std::fstream; //boost #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> +#include <boost/nondet_random.hpp>
+#include <boost/random/variate_generator.hpp>
+#include <boost/random/uniform_int.hpp>
+ //utf8cpp #include <utf8.h> @@ -231,7 +231,7 @@ static int OnModulesLoaded(WPARAM wParam,LPARAM lParam) extern list<wstring> transfers; extern "C" int __declspec(dllexport) Unload(void) -{
+{ for (HANDLE hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDFIRST, 0, 0); hContact; hContact = (HANDLE)CallService(MS_DB_CONTACT_FINDNEXT, (WPARAM)hContact, 0)) DBDeleteContactSetting(hContact, szGPGModuleName, "KeyID_Prescense"); if(!transfers.empty()) @@ -218,7 +218,7 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM DBWriteContactSettingByte(NULL, szGPGModuleName, "FirstRun", 0); { wstring keyinfo = _T("Current private key id: "); - keyinfo += (_tcslen(fp) > 0)?fp:_T("not set"); + keyinfo += (fp[0])?fp:_T("not set"); extern HWND hwndCurKey_p; SetWindowText(hwndCurKey_p, keyinfo.c_str()); } @@ -379,9 +379,113 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM DBDeleteContactSetting(NULL, szGPGModuleName, "KeyType"); ListView_DeleteItem(hwndList, itemnum); break; - } - break; - } + case IDC_GENERATE_RANDOM: + { + wstring path; + { //generating key file + TCHAR *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); + path = tmp; + mir_free(tmp); + path.append(_T("\\new_key")); + wfstream f(path.c_str(), std::ios::out); + if(!f.is_open()) + { + MessageBox(0, _T("Failed to open file"), _T("Error"), MB_OK); + break; + } + f<<"Key-Type: RSA"; + f<<"\n"; + f<<"Key-Length: 2048"; + f<<"\n"; + f<<"Subkey-Type: RSA"; + f<<"\n"; + f<<"Name-Real: "; + f<<get_random(6).c_str(); + f<<"\n"; + f<<"Name-Email: "; + f<<get_random(5).c_str(); + f<<"@"; + f<<get_random(5).c_str(); + f<<"."; + f<<get_random(3).c_str(); + f<<"\n"; + f.close(); + } + { //gpg execution + DWORD code; + string out; + wstring cmd; + cmd += _T("--batch --yes --gen-key \""); + cmd += path; + cmd += _T("\""); + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::minutes(10))) + { + gpg_thread.~thread(); + MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + break; + } + if(result == pxNotFound) + { + MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); + break; + } + DeleteFile(path.c_str()); + string::size_type p1 = 0; + if((p1 = out.find("key ")) != string::npos) + path = toUTF16(out.substr(p1+4, 8)); + else + path.clear(); + } + if(!path.empty()) + { + string out; + DWORD code; + wstring cmd = _T("--batch -a --export "); + cmd += path; + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + { + gpg_thread.~thread(); + MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK); + break; + } + if(result == pxNotFound) + { + MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK); + break; + } + string::size_type s = 0; + while((s = out.find("\r", s)) != string::npos) + { + out.erase(s, 1); + } + DBWriteContactSettingString(NULL, szGPGModuleName, "GPGPubKey", out.c_str()); + DBWriteContactSettingTString(NULL, szGPGModuleName, "KeyID", path.c_str()); + extern HWND hwndCurKey_p; + SetWindowText(hwndCurKey_p, path.c_str()); + } + } + DestroyWindow(hwndDlg); + //use new key + break; + } + break; + } case WM_NOTIFY: { @@ -13,13 +13,11 @@ #undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
-// русский resources
+// Russian (Russia) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_RUS)
-#ifdef _WIN32
LANGUAGE LANG_RUSSIAN, SUBLANG_DEFAULT
#pragma code_page(1251)
-#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
@@ -78,6 +76,7 @@ BEGIN RTEXT "Key password:",IDC_STATIC,12,149,76,8
PUSHBUTTON "Load other",IDC_OTHER,73,131,56,14
PUSHBUTTON "Delete key",IDC_DELETE_KEY,132,131,56,14
+ PUSHBUTTON "Generatr and use random key",IDC_GENERATE_RANDOM,99,163,118,14
END
IDD_BIN_PATH DIALOGEX 0, 0, 354, 89
@@ -174,7 +173,7 @@ END //
#ifdef APSTUDIO_INVOKED
-GUIDELINES DESIGNINFO
+GUIDELINES DESIGNINFO
BEGIN
IDD_LOAD_PUBLIC_KEY, DIALOG
BEGIN
@@ -291,18 +290,16 @@ BEGIN END
END
-#endif // русский resources
+#endif // Russian (Russia) resources
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
-// английский (США) resources
+// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
-#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
-#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
//
@@ -357,7 +354,7 @@ BEGIN RTEXT "Close:",IDC_STATIC,127,49,23,8
END
-#endif // английский (США) resources
+#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
diff --git a/new_gpg.sln b/new_gpg.sln index e3091fb..40d016c 100644 --- a/new_gpg.sln +++ b/new_gpg.sln @@ -1,7 +1,7 @@ п»ї
-Microsoft Visual Studio Solution File, Format Version 10.00
-# Visual Studio 2008
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "new_gpg", "new_gpg.vcproj", "{F29D0C8D-141A-43CF-86B2-34A04653F8D4}"
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "new_gpg", "new_gpg.vcxproj", "{F29D0C8D-141A-43CF-86B2-34A04653F8D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -69,6 +69,7 @@ #define IDC_KEYSERVER 1058
#define IDC_FILE_TRANSFERS 1061
#define IDC_REMOVE_FILTERS 1062
+#define IDC_GENERATE_RANDOM 1063
// Next default values for new objects
//
@@ -76,7 +77,7 @@ #ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 114
#define _APS_NEXT_COMMAND_VALUE 40001
-#define _APS_NEXT_CONTROL_VALUE 1063
+#define _APS_NEXT_CONTROL_VALUE 1064
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
diff --git a/utilities.cpp b/utilities.cpp index e857d5d..8b52ce2 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -938,4 +938,15 @@ wstring toUTF16(string str) //convert as much as possible utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr));
utf8::utf8to16(tmpstr.begin(), tmpstr.end(), back_inserter(ustr));
return ustr;
+}
+
+string get_random(int length)
+{
+ string chars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
+ string data;
+ boost::random_device rng;
+ boost::variate_generator<boost::random_device&, boost::uniform_int<>> gen(rng, boost::uniform_int<>(0, chars.length()-1));
+ for(int i = 0; i < length; ++i)
+ data += chars[gen()];
+ return data;
}
\ No newline at end of file diff --git a/utilities.h b/utilities.h index 431c567..0971265 100644 --- a/utilities.h +++ b/utilities.h @@ -31,5 +31,6 @@ bool isContactSecured(HANDLE hContact); const bool StriStr(const char *str, const char *substr); string toUTF8(wstring str); wstring toUTF16(string str); +string get_random(int length); #endif |