瀏覽代碼

Add passgen from https://github.com/anakod/flipper_passgen

git-subtree-dir: passgen
git-subtree-mainline: cae6e91e051701e17f6b19d3e4b424e8caa56e9e
git-subtree-split: 6324f81657eabb6473d36f36090e0032baf07450
Willy-JL 2 年之前
父節點
當前提交
030c79d6c0

+ 191 - 0
passgen/.clang-format

@@ -0,0 +1,191 @@
+---
+Language:        Cpp
+AccessModifierOffset: -4
+AlignAfterOpenBracket: AlwaysBreak
+AlignArrayOfStructures: None
+AlignConsecutiveMacros: None
+AlignConsecutiveAssignments: None
+AlignConsecutiveBitFields: None
+AlignConsecutiveDeclarations: None
+AlignEscapedNewlines: Left
+AlignOperands:   Align
+AlignTrailingComments: false
+AllowAllArgumentsOnNextLine: true
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortEnumsOnASingleLine: true
+AllowShortBlocksOnASingleLine: Never
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortLambdasOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: WithoutElse
+AllowShortLoopsOnASingleLine: true
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: Yes
+AttributeMacros:
+  - __capability
+BinPackArguments: false
+BinPackParameters: false
+BraceWrapping:
+  AfterCaseLabel:  false
+  AfterClass:      false
+  AfterControlStatement: Never
+  AfterEnum:       false
+  AfterFunction:   false
+  AfterNamespace:  false
+  AfterObjCDeclaration: false
+  AfterStruct:     false
+  AfterUnion:      false
+  AfterExternBlock: false
+  BeforeCatch:     false
+  BeforeElse:      false
+  BeforeLambdaBody: false
+  BeforeWhile:     false
+  IndentBraces:    false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeConceptDeclarations: true
+BreakBeforeBraces: Attach
+BreakBeforeInheritanceComma: false
+BreakInheritanceList: BeforeColon
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit:     99
+CommentPragmas:  '^ IWYU pragma:'
+QualifierAlignment: Leave
+CompactNamespaces: false
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DeriveLineEnding: true
+DerivePointerAlignment: false
+DisableFormat:   false
+EmptyLineAfterAccessModifier: Never
+EmptyLineBeforeAccessModifier: LogicalBlock
+ExperimentalAutoDetectBinPacking: false
+PackConstructorInitializers: BinPack
+BasedOnStyle:    ''
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+AllowAllConstructorInitializersOnNextLine: true
+FixNamespaceComments: false
+ForEachMacros:
+  - foreach
+  - Q_FOREACH
+  - BOOST_FOREACH
+IfMacros:
+  - KJ_IF_MAYBE
+IncludeBlocks:   Preserve
+IncludeCategories:
+  - Regex:           '.*'
+    Priority:        1
+    SortPriority:    0
+    CaseSensitive:   false
+  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
+    Priority:        3
+    SortPriority:    0
+    CaseSensitive:   false
+  - Regex:           '.*'
+    Priority:        1
+    SortPriority:    0
+    CaseSensitive:   false
+IncludeIsMainRegex: '(Test)?$'
+IncludeIsMainSourceRegex: ''
+IndentAccessModifiers: false
+IndentCaseLabels: false
+IndentCaseBlocks: false
+IndentGotoLabels: true
+IndentPPDirectives: None
+IndentExternBlock: AfterExternBlock
+IndentRequires:  false
+IndentWidth:     4
+IndentWrappedFunctionNames: true
+InsertTrailingCommas: None
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+LambdaBodyIndentation: Signature
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 4
+ObjCBreakBeforeNestedBlockParam: true
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakOpenParenthesis: 0
+PenaltyBreakString: 10
+PenaltyBreakTemplateDeclaration: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+PenaltyIndentedWhitespace: 0
+PointerAlignment: Left
+PPIndentWidth:   -1
+ReferenceAlignment: Pointer
+ReflowComments:  false
+RemoveBracesLLVM: false
+SeparateDefinitionBlocks: Leave
+ShortNamespaceLines: 1
+SortIncludes:    Never
+SortJavaStaticImport: Before
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterLogicalNot: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCaseColon: false
+SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: Never
+SpaceBeforeParensOptions:
+  AfterControlStatements: false
+  AfterForeachMacros: false
+  AfterFunctionDefinitionName: false
+  AfterFunctionDeclarationName: false
+  AfterIfMacros:   false
+  AfterOverloadedOperator: false
+  BeforeNonEmptyParentheses: false
+SpaceAroundPointerQualifiers: Default
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyBlock: false
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles:  Never
+SpacesInConditionalStatement: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInLineCommentPrefix:
+  Minimum:         1
+  Maximum:         -1
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+SpaceBeforeSquareBrackets: false
+BitFieldColonSpacing: Both
+Standard:        c++03
+StatementAttributeLikeMacros:
+  - Q_EMIT
+StatementMacros:
+  - Q_UNUSED
+  - QT_REQUIRE_VERSION
+TabWidth:        4
+UseCRLF:         false
+UseTab:          Never
+WhitespaceSensitiveMacros:
+  - STRINGIZE
+  - PP_STRINGIZE
+  - BOOST_PP_STRINGIZE
+  - NS_SWIFT_NAME
+  - CF_SWIFT_NAME
+...
+

+ 13 - 0
passgen/.editorconfig

@@ -0,0 +1,13 @@
+root = true
+
+[*]
+end_of_line = lf
+insert_final_newline = true
+charset = utf-8
+
+[*.{cpp,h,c,py,sh}]
+indent_style = space
+indent_size = 4
+
+[{Makefile,*.mk}]
+indent_size = tab

+ 36 - 0
passgen/.github/workflows/build.yml

@@ -0,0 +1,36 @@
+name: "FAP: Build and lint"
+on: [push, pull_request]
+jobs:
+  ufbt-build-action:
+    runs-on: ubuntu-latest
+    name: 'ufbt: Build for Release branch'
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v3
+
+      - name: Setup ufbt
+        uses: flipperdevices/flipperzero-ufbt-action@v0.1.2
+        with:
+          task: setup
+          sdk-channel: release
+
+      - name: Build with ufbt
+        uses: flipperdevices/flipperzero-ufbt-action@v0.1.2
+        id: build-app
+        with:
+          skip-setup: true
+          sdk-channel: release
+
+      - name: Upload app artifacts
+        uses: actions/upload-artifact@v3
+        with:
+          name: ${{ github.event.repository.name }}-${{ steps.build-app.outputs.suffix }}
+          path: ${{ steps.build-app.outputs.fap-artifacts }}
+
+      # # You can remove this step if you don't want to check source code formatting
+      # - name: Lint sources
+      #   uses: flipperdevices/flipperzero-ufbt-action@v0.1.2
+      #   with:
+      #     # skip SDK setup, we already did it in previous step
+      #     skip-setup: true
+      #     task: lint

+ 54 - 0
passgen/.gitignore

@@ -0,0 +1,54 @@
+dist/
+
+# Prerequisites
+*.d
+
+# Object files
+*.o
+*.ko
+*.obj
+*.elf
+
+# Linker output
+*.ilk
+*.map
+*.exp
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Libraries
+*.lib
+*.a
+*.la
+*.lo
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
+*.i*86
+*.x86_64
+*.hex
+
+# Debug files
+*.dSYM/
+*.su
+*.idb
+*.pdb
+
+# Kernel Module Compile Results
+*.mod*
+*.cmd
+.tmp_versions/
+modules.order
+Module.symvers
+Mkfile.old
+dkms.conf

+ 1 - 0
passgen/.gitsubtree

@@ -0,0 +1 @@
+https://github.com/anakod/flipper_passgen main

+ 21 - 0
passgen/LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 Skurydin Alexey
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 4 - 0
passgen/README.md

@@ -0,0 +1,4 @@
+# flipper_passgen
+This is a simple Password Generator plugin (**fap**) for the [Flipper Zero](https://www.flipperzero.one).
+
+![preview](images/preview.png)

+ 12 - 0
passgen/application.fam

@@ -0,0 +1,12 @@
+App(
+    appid="passgen",
+    name="Password Generator",
+    apptype=FlipperAppType.EXTERNAL,
+    entry_point="passgenapp",
+    requires=[
+        "gui",
+    ],
+    fap_category="Tools",
+    fap_icon="icons/passgen_icon.png",
+    fap_icon_assets="icons",
+)

二進制
passgen/icons/Horizontal_arrow_9x7.png


二進制
passgen/icons/Ok_btn_9x9.png


二進制
passgen/icons/Pin_back_arrow_10x8.png


二進制
passgen/icons/Vertical_arrow_7x9.png


二進制
passgen/icons/passgen_icon.png


二進制
passgen/images/preview.png


+ 275 - 0
passgen/passgen.c

@@ -0,0 +1,275 @@
+#include <furi.h>
+#include <furi_hal_random.h>
+#include <gui/gui.h>
+#include <gui/elements.h>
+#include <input/input.h>
+#include <notification/notification_messages.h>
+#include <stdlib.h>
+#include <passgen_icons.h>
+
+#define PASSGEN_MAX_LENGTH 16
+#define PASSGEN_CHARACTERS_LENGTH (26*4)
+
+#define PASSGEN_DIGITS "0123456789"
+#define PASSGEN_LETTERS_LOW "abcdefghijklmnopqrstuvwxyz"
+#define PASSGEN_LETTERS_UP "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+#define PASSGEN_SPECIAL "!#$%^&*.-_"
+
+typedef enum PassGen_Alphabet
+{
+	Digits = 1,
+	Lowercase = 2,
+	
+	Uppercase = 4,
+	Special = 8,
+	
+	DigitsLower = Digits | Lowercase,
+	DigitsAllLetters = Digits | Lowercase | Uppercase,
+	Mixed = DigitsAllLetters | Special
+} PassGen_Alphabet;
+
+const char * const PassGen_AlphabetChars [16] = {
+	"0", // invalid value
+	/*    PASSGEN_SPECIAL    PASSGEN_LETTERS_UP    PASSGEN_LETTERS_LOW */ PASSGEN_DIGITS   ,
+	/*    PASSGEN_SPECIAL    PASSGEN_LETTERS_UP */ PASSGEN_LETTERS_LOW /* PASSGEN_DIGITS */,
+	/*    PASSGEN_SPECIAL    PASSGEN_LETTERS_UP */ PASSGEN_LETTERS_LOW    PASSGEN_DIGITS   ,
+	/*    PASSGEN_SPECIAL */ PASSGEN_LETTERS_UP /* PASSGEN_LETTERS_LOW    PASSGEN_DIGITS */,
+	/*    PASSGEN_SPECIAL */ PASSGEN_LETTERS_UP /* PASSGEN_LETTERS_LOW */ PASSGEN_DIGITS   ,
+	/*    PASSGEN_SPECIAL */ PASSGEN_LETTERS_UP    PASSGEN_LETTERS_LOW /* PASSGEN_DIGITS */,
+	/*    PASSGEN_SPECIAL */ PASSGEN_LETTERS_UP    PASSGEN_LETTERS_LOW    PASSGEN_DIGITS   ,
+	      PASSGEN_SPECIAL /* PASSGEN_LETTERS_UP    PASSGEN_LETTERS_LOW    PASSGEN_DIGITS */,
+	      PASSGEN_SPECIAL /* PASSGEN_LETTERS_UP    PASSGEN_LETTERS_LOW */ PASSGEN_DIGITS   ,
+	      PASSGEN_SPECIAL /* PASSGEN_LETTERS_UP */ PASSGEN_LETTERS_LOW /* PASSGEN_DIGITS */,
+	      PASSGEN_SPECIAL /* PASSGEN_LETTERS_UP */ PASSGEN_LETTERS_LOW    PASSGEN_DIGITS   ,
+	      PASSGEN_SPECIAL    PASSGEN_LETTERS_UP /* PASSGEN_LETTERS_LOW    PASSGEN_DIGITS */,
+	      PASSGEN_SPECIAL    PASSGEN_LETTERS_UP /* PASSGEN_LETTERS_LOW */ PASSGEN_DIGITS   ,
+	      PASSGEN_SPECIAL    PASSGEN_LETTERS_UP    PASSGEN_LETTERS_LOW /* PASSGEN_DIGITS */,
+	      PASSGEN_SPECIAL    PASSGEN_LETTERS_UP    PASSGEN_LETTERS_LOW    PASSGEN_DIGITS   ,
+};
+
+const int AlphabetLevels[] = { Digits, Lowercase, DigitsLower, DigitsAllLetters, Mixed };
+const char* AlphabetLevelNames[] = { "1234", "abcd", "ab12", "Ab12", "Ab1#" };
+const int AlphabetLevelsCount = sizeof(AlphabetLevels) / sizeof(int);
+
+const NotificationSequence PassGen_Alert_vibro = {
+    &message_vibro_on,
+    &message_blue_255,
+	&message_delay_50,
+    &message_vibro_off,
+    NULL,
+};
+
+typedef struct {
+    FuriMessageQueue* input_queue;
+    ViewPort* view_port;
+    Gui* gui;
+    FuriMutex** mutex;
+	NotificationApp* notify;
+	const char* alphabet;
+	char password[PASSGEN_MAX_LENGTH+1];
+	int length; // must be <= PASSGEN_MAX_LENGTH
+	int level;
+} PassGen;
+
+void state_free(PassGen* app) {
+	// NOTE: would have preferred if a "safe" memset() was available...
+	//       but, since cannot prevent optimization from removing
+	//       memset(), fill with random data instead.
+	furi_hal_random_fill_buf((void*)(app->password), PASSGEN_MAX_LENGTH);
+
+    gui_remove_view_port(app->gui, app->view_port);
+    furi_record_close(RECORD_GUI);
+    view_port_free(app->view_port);
+    furi_message_queue_free(app->input_queue);
+    furi_mutex_free(app->mutex);
+    furi_record_close(RECORD_NOTIFICATION);
+    free(app);
+}
+
+static void input_callback(InputEvent* input_event, void* ctx) {
+    PassGen* app = ctx;
+    if(input_event->type == InputTypeShort) {
+        furi_message_queue_put(app->input_queue, input_event, 0);
+    }
+}
+
+static void render_callback(Canvas* canvas, void* ctx) {
+    char str_length[8];
+    PassGen* app = ctx;
+    furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
+	
+    canvas_clear(canvas);
+    canvas_draw_box(canvas, 0, 0, 128, 14);
+    canvas_set_color(canvas, ColorWhite);
+    canvas_set_font(canvas, FontPrimary);
+    canvas_draw_str(canvas, 2, 11, "Password Generator");
+    
+    canvas_set_color(canvas, ColorBlack);
+	canvas_draw_str_aligned(canvas, 64, 35, AlignCenter, AlignCenter, app->password);
+
+	// Navigation menu:
+    canvas_set_font(canvas, FontSecondary);
+    canvas_draw_icon(canvas, 96, 52, &I_Pin_back_arrow_10x8);
+    canvas_draw_str(canvas, 108, 60, "Exit");
+	
+    canvas_draw_icon(canvas, 54, 52, &I_Vertical_arrow_7x9);
+    canvas_draw_str(canvas, 64, 60, AlphabetLevelNames[app->level]);
+	
+    snprintf(str_length, sizeof(str_length), "Len: %d", app->length);
+    canvas_draw_icon(canvas, 4, 53, &I_Horizontal_arrow_9x7);
+    canvas_draw_str(canvas, 15, 60, str_length);
+	
+    furi_mutex_release(app->mutex);
+}
+
+void build_alphabet(PassGen* app)
+{
+	PassGen_Alphabet mode = AlphabetLevels[app->level];
+	if (mode > 0 && mode < 16) {
+		app->alphabet = PassGen_AlphabetChars[mode];
+	} else {
+		app->alphabet = PassGen_AlphabetChars[0]; // Invalid mode ... password will be all zero digits
+	}
+}
+
+PassGen* state_init() {
+    PassGen* app = malloc(sizeof(PassGen));
+	_Static_assert(8 <= PASSGEN_MAX_LENGTH, "app->length must be set <= PASSGEN_MAX_LENGTH");
+	app->length = 8;
+	app->level = 2;
+	build_alphabet(app);
+    app->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
+    app->view_port = view_port_alloc();
+    app->gui = furi_record_open(RECORD_GUI);
+    app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
+    view_port_input_callback_set(app->view_port, input_callback, app);
+    view_port_draw_callback_set(app->view_port, render_callback, app);
+    gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
+	
+	app->notify = furi_record_open(RECORD_NOTIFICATION);
+	
+    return app;
+}
+
+void generate(PassGen* app)
+{
+	memset(app->password, 0, PASSGEN_MAX_LENGTH+1);
+
+	int char_option_count = strlen(app->alphabet);
+	if (char_option_count < 0) {
+		return;
+	}
+
+	// determine largest character value that avoids bias
+	char ceil = CHAR_MAX - (CHAR_MAX % char_option_count) - 1;
+
+	// iteratively fill the password buffer with random values
+	// then keep only values that are in-range (no bias)
+	void* remaining_buffer = app->password;
+	size_t remaining_length = (app->length * sizeof(char));
+
+	while (remaining_length != 0) {
+		// fewer calls to hardware TRNG is more efficient
+		furi_hal_random_fill_buf(remaining_buffer, remaining_length);
+
+		// keep only values that are in-range (no bias)
+		char* target = remaining_buffer;
+		char* source = remaining_buffer;
+		size_t valid_count = 0;
+
+		for (size_t i = 0; i < remaining_length; i++) {
+			int v = *source;
+			// if the generated random value is in range, keep it
+			if (v < ceil) {
+				v %= char_option_count;
+				*target = app->alphabet[v];
+				// increment target pointer and count of valid items found
+				target++;
+				valid_count++;
+			}
+			// always increment the source pointer
+			source++;
+		}
+		remaining_length -= valid_count;
+		remaining_buffer = target;
+	}
+}
+
+void update_password(PassGen* app, bool vibro)
+{
+	generate(app);
+	
+	if (vibro)
+		notification_message(app->notify, &PassGen_Alert_vibro);
+	else
+		notification_message(app->notify, &sequence_blink_blue_100);
+	view_port_update(app->view_port);
+}
+
+int32_t passgenapp(void) {
+    PassGen* app = state_init();
+	generate(app);
+
+    while(1) {
+        InputEvent input;
+        while(furi_message_queue_get(app->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
+            furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
+
+			if (input.type == InputTypeShort)
+			{
+				switch (input.key) {
+				case InputKeyBack:
+					furi_mutex_release(app->mutex);
+					state_free(app);
+					return 0;
+				case InputKeyDown:
+					if (app->level > 0)
+					{
+						app->level--;
+						build_alphabet(app);
+						update_password(app, false);
+					}
+					else
+						notification_message(app->notify, &sequence_blink_red_100);
+					break;
+				case InputKeyUp:
+					if (app->level < AlphabetLevelsCount - 1)
+					{
+						app->level++;
+						build_alphabet(app);
+						update_password(app, false);
+					}
+					else
+						notification_message(app->notify, &sequence_blink_red_100);
+					break;
+				case InputKeyLeft:
+					if (app->length > 1)
+					{
+						app->length--;
+						update_password(app, false);
+					}
+					else
+						notification_message(app->notify, &sequence_blink_red_100);
+					break;
+				case InputKeyRight:
+					if (app->length < PASSGEN_MAX_LENGTH)
+					{
+						app->length++;
+						update_password(app, false);
+					}
+					else
+						notification_message(app->notify, &sequence_blink_red_100);
+					break;
+				case InputKeyOk:
+					update_password(app, true);
+					break;
+				default:
+					break;
+				}
+			}
+            furi_mutex_release(app->mutex);
+        }
+    }
+    state_free(app);
+    return 0;
+}