add generate thing

This commit is contained in:
zxkmm 2024-09-28 22:40:08 +08:00
parent e73c227860
commit c06f171544
2 changed files with 161 additions and 3 deletions

View File

@ -59,7 +59,18 @@ RandomView::RandomView(NavigationView& nav)
&check_log,
&text_debug,
&button_modem_setup,
&console});
&console,
&labels,
&text_generated_passwd,
&text_char_type_hints,
&check_digits,
&check_latin_lower,
&check_latin_upper,
&check_punctuation,
&button_refresh,
&button_show_qr,
&field_digits,
&check_allow_confusable_chars});
// Auto-configure modem for LCR RX (TODO remove)
field_frequency.set_value(467225500);
@ -83,6 +94,44 @@ RandomView::RandomView(NavigationView& nav)
nav.push<ModemSetupView>();
};
check_digits.on_select = [this](Checkbox&, bool) {
this->new_password();
};
check_latin_lower.on_select = [this](Checkbox&, bool) {
this->new_password();
};
check_latin_upper.on_select = [this](Checkbox&, bool) {
this->new_password();
};
check_punctuation.on_select = [this](Checkbox&, bool) {
this->new_password();
};
check_allow_confusable_chars.on_select = [this](Checkbox&, bool) {
this->new_password();
};
button_refresh.on_select = [this](Button&) {
this->new_password();
};
button_show_qr.on_select = [this](Button&) {
// TODO
};
field_digits.on_change = [this](int32_t) {
this->new_password();
};
/// v init setting
check_digits.set_value(true);
check_latin_lower.set_value(true);
field_digits.set_value(8);
///^ init setting
logger = std::make_unique<RandomLogger>();
if (logger)
logger->append(logs_dir / u"AFSK.TXT");
@ -94,6 +143,7 @@ RandomView::RandomView(NavigationView& nav)
audio::output::start();
receiver_model.enable();
new_password();
}
void RandomView::on_data(uint32_t value, bool is_data) {
@ -101,6 +151,7 @@ void RandomView::on_data(uint32_t value, bool is_data) {
std::string str_byte = "";
if (is_data) {
seed = static_cast<unsigned int>(value);
// Colorize differently after message splits
str_console += (char)((console_color & 3) + 9);
@ -133,6 +184,59 @@ void RandomView::on_freqchg(int64_t freq) {
field_frequency.set_value(freq);
}
void RandomView::new_password() {
std::string charset;
std::string password;
std::string char_type_hints;
if (check_digits.value())
charset += "0123456789";
if (check_latin_lower.value())
charset += "abcdefghijklmnopqrstuvwxyz";
if (check_latin_upper.value())
charset += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (check_punctuation.value())
charset += ".,-!?";
if (!check_allow_confusable_chars.value()) {
charset.erase(std::remove_if(charset.begin(), charset.end(),
[](char c) { return c == '0' || c == 'O' || c == 'o' || c == '1' || c == 'l'; }),
charset.end());
}
if (charset.empty()) {
text_generated_passwd.set("generate failed,");
text_char_type_hints.set("select at least 1 type");
return;
}
if (seed == 0) {
text_generated_passwd.set("generate failed,");
text_char_type_hints.set("random seed exception");
}else {
std::srand(seed); // extern void srand (unsigned int __seed) __THROW;
}
int password_length = field_digits.value();
for (int i = 0; i < password_length; i++) {
char c = charset[std::rand() % charset.length()];
password += c;
if (std::isdigit(c))
char_type_hints += "1";
else if (std::islower(c))
char_type_hints += "a";
else if (std::isupper(c))
char_type_hints += "A";
else
char_type_hints += ",";
}
text_generated_passwd.set(password);
text_char_type_hints.set(char_type_hints);
}
RandomView::~RandomView() {
audio::output::stop();
receiver_model.disable();

View File

@ -57,10 +57,13 @@ class RandomView : public View {
void focus() override;
std::string title() const override { return "AFSK RX"; };
std::string title() const override { return "random"; };
private:
unsigned int seed = 0; // extern void srand (unsigned int __seed) __THROW;
void on_data(uint32_t value, bool is_data);
void new_password();
NavigationView& nav_;
RxRadioState radio_state_{};
@ -105,7 +108,58 @@ class RandomView : public View {
LanguageHelper::currentMessages[LANG_MODEM_SETUP]};
Console console{
{0, 4 * 16, 240, screen_width}};
{0, 3 * 16, 240, 16}};
Labels labels{
{{5 * 8, 9 * 16}, "digits:", Theme::getInstance()->fg_light->foreground}};
Text text_generated_passwd{
{0, 5 * 16, screen_width, 28},
"000000000000000000000000000000"};
Text text_char_type_hints{
{0, 6 * 16, screen_width, 28},
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"};
Checkbox check_digits{
{3 * 8, 13 * 16},
3,
"123"};
Checkbox check_punctuation{
{20 * 8, 13 * 16},
6,
".,-!?"};
Checkbox check_latin_lower{
{3 * 8, 15 * 16},
3,
"abc"};
Checkbox check_latin_upper{
{20 * 8, 15 * 16},
3,
"ABC"};
Checkbox check_allow_confusable_chars{
{3 * 8, 11 * 16},
20,
"Include 0 O o 1 l"};
Button button_refresh{
{0 * 8, 17 * 16 + 10, screen_width / 2, 24},
"refresh"};
Button button_show_qr{
{screen_width / 2, 17 * 16 + 10, screen_width / 2, 24},
"show QR"};
NumberField field_digits{
{24 * 8, 9 * 16},
2,
{1, 30},
1,
' '};
void on_data_afsk(const AFSKDataMessage& message);