suwako / SimPas1-Unix アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
SimPas1-Unix / src / genpass.cc
3.1 KB、108行 生 Blame 履歴
1#include "common.hh"
2#include "genpass.hh"
3
4#include <FL/Fl_Window.H>
5#include <FL/fl_ask.H>
6
7#include <fstream>
8#include <vector>
9
10Genpass gen;
11
12std::string Genpass::exec(int count, bool issecure) {
13 std::string lang = Common::getlang();
14
15 const std::string charset_risky =
16 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
17 const std::string charset_secure =
18 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()=~-^\\|_@`[{]};:+*<>,./?";
19 const std::string &charset = issecure ? charset_secure : charset_risky;
20
21 std::ifstream fp("/dev/random", std::ios_base::binary);
22 if (!fp.is_open()) {
23 std::string err = (lang.compare(0, 2, "en") == 0 ?
24 "Could not open /dev/random" :
25 "/dev/randomを開けられませんでした");
26 fl_alert("%s", err.c_str());
27 return "";
28 }
29
30 std::vector<char> password(count + 1);
31 for (int i = 0; i < count; ++i) {
32 unsigned char key;
33 fp.read(reinterpret_cast<char *>(&key), sizeof(key));
34 if (!fp) {
35 std::string err = (lang.compare(0, 2, "en") == 0 ?
36 "Could not read /dev/random" :
37 "/dev/randomから読み込みに失敗");
38 fl_alert("%s", err.c_str());
39 fp.close();
40 std::exit(EXIT_FAILURE);
41 }
42
43 password[i] = charset[key % charset.size()];
44 }
45
46 password[count] = '\0';
47 fp.close();
48
49 return std::string(password.data());
50}
51
52void Genpass::generate_cb(Fl_Widget *, void *) {
53 int count = std::stoi(gen.counter->value());
54 bool issecure = gen.securechk->value() > 0;
55
56 std::string password = gen.exec(count, issecure);
57 gen.res->value(password.c_str());
58}
59
60void Genpass::dialog_cb(Fl_Widget *w, void *) {
61 (void)w;
62 std::string lang = Common::getlang();
63 Fl_Window *dialog = new Fl_Window(450, 250,
64 (lang.compare(0, 2, "en") == 0 ? "Generate password" : "パスワードの作成"));
65
66 gen.counter = new Fl_Input(120, 20, 100, 30,
67 (lang.compare(0, 2, "en") == 0 ? "Length:" : "長さ:"));
68 gen.counter->type(FL_INT_INPUT);
69 gen.counter->value("64");
70 dialog->add(gen.counter);
71
72 gen.securechk = new Fl_Check_Button(120, 70, 150, 30,
73 (lang.compare(0, 2, "en") == 0 ? "Secure?" : "安全化?"));
74 gen.securechk->value(1);
75 dialog->add(gen.securechk);
76
77 gen.genbtn = new Fl_Button(120, 110, 150, 30,
78 (lang.compare(0, 2, "en") == 0 ? "Generate" : "作成"));
79 gen.genbtn->callback(generate_cb);
80 dialog->add(gen.genbtn);
81
82 gen.res = new Fl_Output(120, 150, 300, 30,
83 (lang.compare(0, 2, "en") == 0 ? "Password:" : "パスワード:"));
84 gen.res->value("");
85 dialog->add(gen.res);
86
87 Fl_Button *okbtn = new Fl_Button(60, 200, 80, 30, "OK");
88 Fl_Button *cancelbtn = new Fl_Button(160, 200, 80, 30,
89 (lang.compare(0, 2, "en") == 0 ? "Cancel" : "キャンセル"));
90
91 okbtn->callback(static_ok_cb, dialog);
92 cancelbtn->callback(static_cancel_cb, dialog);
93
94 dialog->add(okbtn);
95 dialog->add(cancelbtn);
96
97 dialog->end();
98 dialog->set_modal();
99 dialog->show();
100}
101
102void Genpass::static_ok_cb(Fl_Widget *w, void *data) {
103 ((Genpass *)data)->ok_cb(w, data);
104}
105
106void Genpass::static_cancel_cb(Fl_Widget *w, void *data) {
107 ((Genpass *)data)->cancel_cb(w, data);
108}