Skip to content

Commit

Permalink
Random app imp (#2276)
Browse files Browse the repository at this point in the history
  • Loading branch information
htotoo authored Sep 30, 2024
1 parent 105742a commit a398ed1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion firmware/application/external/random_password/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ __attribute__((section(".external_app.app_random_password.application_informatio
/*.header_version = */ CURRENT_HEADER_VERSION,
/*.app_version = */ VERSION_MD5,

/*.app_name = */ "random passwd",
/*.app_name = */ "Random passwd",
/*.bitmap_data = */ {
0xC0,
0x03,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
* copyleft zxkmm
* Copyright (C) 2024 HToToo
* Copyright (C) 2024 zxkmm
* Copyright (C) 2024 HTotoo
*
* This file is part of PortaPack.
*
Expand Down Expand Up @@ -86,7 +86,7 @@ RandomPasswordView::RandomPasswordView(NavigationView& nav)
serial_format.bit_order = LSB_FIRST;
persistent_memory::set_serial_format(serial_format);

progressbar.set_max(30);
progressbar.set_max(MAX_DIGITS * 2);

check_log.set_value(logging);
check_log.on_select = [this](Checkbox&, bool v) {
Expand Down Expand Up @@ -151,16 +151,17 @@ RandomPasswordView::RandomPasswordView(NavigationView& nav)
button_flood.on_select = [this](Button&) {
if (flooding) {
flooding = false;
button_flood.set_text("flood");
button_flood.set_text(LanguageHelper::currentMessages[LANG_FLOOD]);
} else {
flooding = true;
button_flood.set_text("stop");
button_flood.set_text(LanguageHelper::currentMessages[LANG_STOP]);
}
};
button_send.on_select = [this, &nav](Button&) {
async_prev_val = portapack::async_tx_enabled;
portapack::async_tx_enabled = true;
UsbSerialAsyncmsg::asyncmsg(password);
portapack::async_tx_enabled = false;
portapack::async_tx_enabled = async_prev_val;
};

field_digits.on_change = [this](int32_t) {
Expand Down Expand Up @@ -197,15 +198,15 @@ void RandomPasswordView::on_data(uint32_t value, bool is_data) {

/// v feed deque
seeds_deque.push_back(value);
if (seeds_deque.size() > MAX_DIGITS) {
if (seeds_deque.size() > MAX_DIGITS * 2) {
seeds_deque.pop_front();
}

///^ feed deque

progressbar.set_value(seeds_deque.size());

if (flooding && seeds_deque.size() >= MAX_DIGITS) {
if (flooding && seeds_deque.size() >= MAX_DIGITS * 2) {
new_password();
}

Expand All @@ -217,7 +218,6 @@ void RandomPasswordView::on_data(uint32_t value, bool is_data) {

void RandomPasswordView::clean_buffer() {
seeds_deque = {};
char_deque = {""};
}

void RandomPasswordView::on_freqchg(int64_t freq) {
Expand All @@ -234,7 +234,7 @@ void RandomPasswordView::set_random_freq() {
}

void RandomPasswordView::new_password() {
if (seeds_deque.size() < MAX_DIGITS) {
if (seeds_deque.size() < MAX_DIGITS * 2) {
seeds_buffer_not_full = true;
text_generated_passwd.set("wait seeds buffer full");
text_char_type_hints.set("then press generate");
Expand Down Expand Up @@ -274,12 +274,15 @@ void RandomPasswordView::new_password() {
* (assume AFSK data is averaged in chaotic space, which maybe no one can garentee but I hope so)
* */

for (int i = 0; i < password_length; i++) {
for (int i = 0; i < password_length * 2; i += 2) {
unsigned int seed = seeds_deque[i];
std::srand(seed);
uint8_t rollnum = (uint8_t)(seeds_deque[i + 1] % 128);
uint8_t nu = 0;
for (uint8_t o = 0; o < rollnum; ++o) nu = std::rand();
nu++;
char c = charset[std::rand() % charset.length()];
password += c;
char_deque.push_back(std::string(1, c));

if (std::isdigit(c)) {
char_type_hints += "1";
Expand All @@ -304,19 +307,19 @@ void RandomPasswordView::new_password() {
}

if (check_auto_send.value() || flooding) {
async_prev_val = portapack::async_tx_enabled;
portapack::async_tx_enabled = true;
// printing out seeds buufer
// for (auto seed : seeds_deque) {
// UsbSerialAsyncmsg::asyncmsg(std::to_string(seed));
// }
UsbSerialAsyncmsg::asyncmsg(password);
portapack::async_tx_enabled = false;
portapack::async_tx_enabled = async_prev_val;
}

clean_buffer();
}

// TODO: why flash and disappeared
// tried:
// 1. paint inline in new_password func
// 2. paint in a seperate func and call from new_password
Expand All @@ -328,7 +331,7 @@ void RandomPasswordView::paint_password_hints() {
Painter painter;
const int char_width = 8;
const int char_height = 16;
const int start_y = 6 * char_height + 5;
const int start_y = 7 * char_height + 5;
const int rect_height = 4;

for (size_t i = 0; i < password.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
* copyleft zxkmm
* Copyright (C) 2024 HToToo
* Copyright (C) 2024 zxkmm
* Copyright (C) 2024 HTotoo
*
* This file is part of PortaPack.
*
Expand Down Expand Up @@ -64,17 +64,17 @@ class RandomPasswordView : public View {

void focus() override;

std::string title() const override { return "r.passwd"; };
std::string title() const override { return "R.passwd"; };

private:
unsigned int seed = 0; // extern void srand (unsigned int __seed) __THROW;
std::string password = "";
std::deque<unsigned int> seeds_deque = {0};
std::deque<std::string> char_deque = {""};
bool seeds_buffer_not_full = true;
bool in_benchmark = false;
bool flooding = false;
bool logging = false;
bool async_prev_val = false;
std::string str_log{""};

void on_data(uint32_t value, bool is_data);
Expand All @@ -86,7 +86,7 @@ class RandomPasswordView : public View {
NavigationView& nav_;
RxRadioState radio_state_{};
app_settings::SettingsManager settings_{
"rx_afsk", app_settings::Mode::RX};
"rx_passgen", app_settings::Mode::RX};

Labels labels{
{{0 * 8, 0 * 16}, "------------seeds-------------", Theme::getInstance()->fg_light->foreground},
Expand All @@ -112,7 +112,7 @@ class RandomPasswordView : public View {

Button button_modem_setup{
{screen_width - 12 * 8, 2 * 16 - 1, 96, 16 + 2},
"AFSK modem"};
LanguageHelper::currentMessages[LANG_MODEM_SETUP]};

Text text_seed{
{0, 2 * 16, 10 * 8, 16},
Expand All @@ -122,22 +122,22 @@ class RandomPasswordView : public View {
{10 * 8 + 2, 2 * 16, screen_width - 96 - (10 * 8 + 4) - 1, 16}};

Text text_generated_passwd{
{0, 4 * 16, screen_width, 28},
{0, 4 * 16, screen_width, 16},
"000000000000000000000000000000"};

Text text_char_type_hints{
{0, 5 * 16, screen_width, 28},
{0, 5 * 16, screen_width, 16},
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"};

Checkbox check_show_seeds{
{17 * 8, 8 * 16},
6,
"show seed"};
"Show seed"};

Checkbox check_auto_send{
{1 * 8, 8 * 16},
20,
"auto send"};
"Auto send"};

Checkbox check_punctuation{
{17 * 8, 12 * 16},
Expand Down Expand Up @@ -167,23 +167,23 @@ class RandomPasswordView : public View {
Checkbox check_log{
{17 * 8, 10 * 16},
3,
"savin"};
LanguageHelper::currentMessages[LANG_SAVE]};

Button button_flood{
{0 * 8, 15 * 16 + 18, screen_width / 2, 22},
"flood"};
LanguageHelper::currentMessages[LANG_FLOOD]};

Button button_send{
{screen_width / 2 + 2, 15 * 16 + 18, screen_width / 2 - 2, 22},
"send pwd"};
"Send pwd"};

Button button_refresh{
{0 * 8, 17 * 16 + 10, screen_width / 2, 22},
"generate"};
"Generate"};

Button button_show_qr{
{screen_width / 2 + 2, 17 * 16 + 10, screen_width / 2 - 2, 22},
"show QR"};
LanguageHelper::currentMessages[LANG_SHOWQR]};

NumberField field_digits{
{16 * 8, 7 * 16 - 2},
Expand Down
2 changes: 1 addition & 1 deletion firmware/common/ui_language.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "ui_language.hpp"

// use the exact position in this array! the enum's value is the identifier. Best to add to the end
const char* LanguageHelper::englishMessages[] = {"OK", "Cancel", "Error", "Modem setup", "Debug", "Log", "Done", "Start", "Stop", "Scan", "Clear", "Ready", "Data:", "Loop", "Reset", "Pause", "Resume"};
const char* LanguageHelper::englishMessages[] = {"OK", "Cancel", "Error", "Modem setup", "Debug", "Log", "Done", "Start", "Stop", "Scan", "Clear", "Ready", "Data:", "Loop", "Reset", "Pause", "Resume", "Flood", "Show QR", "Save"};

// multi language support will changes (not in use for now)
const char** LanguageHelper::currentMessages = englishMessages;
Expand Down
5 changes: 4 additions & 1 deletion firmware/common/ui_language.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ enum LangConsts {
LANG_LOOP,
LANG_RESET,
LANG_PAUSE,
LANG_RESUME
LANG_RESUME,
LANG_FLOOD,
LANG_SHOWQR,
LANG_SAVE
};

class LanguageHelper {
Expand Down

0 comments on commit a398ed1

Please sign in to comment.