From 97deedd97a1d026586afa23d47a00322bcd22e59 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 15 Jun 2025 09:40:02 +0200 Subject: Update auf neue lua-bcrypt version --- .clang-format | 45 +++++++++++++++++++++++++++++++++++++++++++++ .clang-tidy | 20 ++++++++++++++++++++ .gitignore | 5 +---- bcrypt-test.c | 48 +++++++++++++++++++++++------------------------- compile_flags.txt | 5 +++++ makefile | 19 ++++++++++++++----- 6 files changed, 108 insertions(+), 34 deletions(-) create mode 100644 .clang-format create mode 100644 .clang-tidy create mode 100644 compile_flags.txt diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b32bad6 --- /dev/null +++ b/.clang-format @@ -0,0 +1,45 @@ +--- +AccessModifierOffset: -4 +AlignConsecutiveAssignments: 'true' +AlignConsecutiveDeclarations: 'true' +AlignEscapedNewlines: Left +AlignTrailingComments: 'true' +AlwaysBreakAfterReturnType: TopLevelDefinitions +BreakBeforeBraces: Stroustrup +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +ColumnLimit: '0' +CompactNamespaces: 'false' +Cpp11BracedListStyle: 'false' +FixNamespaceComments: 'true' +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^.*(precomp|pch|stdafx)' + Priority: -1 + - Regex: '^<.*>' + Priority: 1 + - Regex: '^".*"' + Priority: 2 + - Regex: '.*' + Priority: 3 +IndentCaseLabels: 'false' +IndentPPDirectives: AfterHash +IndentWidth: '4' +IndentWrappedFunctionNames: 'false' +KeepEmptyLinesAtTheStartOfBlocks: 'false' +PointerAlignment: Right +SortIncludes: 'true' +SpaceAfterCStyleCast: 'true' +SpaceAfterTemplateKeyword: 'false' +SpaceBeforeAssignmentOperators: 'true' +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: 'false' +SpaceInEmptyParentheses: 'false' +SpacesInAngles: 'false' +SpacesInCStyleCastParentheses: 'false' +SpacesInConditionalStatement: 'true' +SpacesInParentheses: 'false' +Standard: Auto +TabWidth: '4' +UseTab: ForIndentation +... diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..fe17118 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,20 @@ +--- +Checks: "*, + -abseil-*, + -altera-*, + -android-*, + -fuchsia-*, + -google-*, + -llvm*, + -modernize-use-trailing-return-type, + -zircon-*, + -readability-else-after-return, + -readability-static-accessed-through-instance, + -readability-avoid-const-params-in-decls, + -cppcoreguidelines-non-private-member-variables-in-classes, + -misc-non-private-member-variables-in-classes, +" +WarningsAsErrors: '' +HeaderFilterRegex: '' +FormatStyle: none + diff --git a/.gitignore b/.gitignore index b64b38c..4eca2bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,4 @@ -lua-bcrypt - *.o +*.a bcrypt-test -tags -.tags user.db diff --git a/bcrypt-test.c b/bcrypt-test.c index 1df296d..19bd85e 100644 --- a/bcrypt-test.c +++ b/bcrypt-test.c @@ -1,19 +1,20 @@ +#include +#include #include #include -#include -#include #include -#include "lua-bcrypt/compat/include/pwd.h" +#include "bcrypt.h" #define USER_DATABASE "./user.db" #define SECURITY_LEVEL 12 #define SEP '|' +#define MAX_LINE_LENGTH 256 static bool -valid_char(int ch) +valid_char(int chr) { - return isalnum(ch) || ch == '_' || ch == '-' || ch == '.'; + return isalnum(chr) || chr == '_' || chr == '-' || chr == '.'; } /** @@ -25,7 +26,7 @@ valid_char(int ch) * \return true on success (user was created) or false on error */ bool -create_user(const char *username, const char *password) +create_user(const char *username, const char *password) // NOLINT { // check for invalid characters in username for ( const char *ptr = username; *ptr; ++ptr ) { @@ -34,8 +35,8 @@ create_user(const char *username, const char *password) } } - FILE *db = fopen(USER_DATABASE, "a"); - if ( db == NULL ) { + FILE *database = fopen(USER_DATABASE, "a"); + if ( database == NULL ) { return false; } @@ -45,8 +46,8 @@ create_user(const char *username, const char *password) return false; } - fprintf(db, "%s%c%s\n", username, SEP, hash); - fclose(db); + (void) fprintf(database, "%s%c%s\n", username, SEP, hash); + (void) fclose(database); return true; } @@ -60,18 +61,18 @@ create_user(const char *username, const char *password) * @return true, if the user was successfully authenticated, otherwise false */ bool -check_user(const char *const username, const char *const password) +check_user(const char *const username, const char *const password) // NOLINT { - FILE *db = fopen(USER_DATABASE, "r"); - if ( db == NULL ) { + FILE *database = fopen(USER_DATABASE, "r"); + if ( database == NULL ) { return false; } const size_t username_length = strlen(username); - char line[256]; + char line[MAX_LINE_LENGTH]; bool found = false; - while ( !found && fgets(line, sizeof line, db) ) { + while ( !found && fgets(line, sizeof line, database) ) { line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) if ( strncmp(line, username, username_length) != 0 ) { @@ -82,7 +83,7 @@ check_user(const char *const username, const char *const password) continue; } - if ( bcrypt_checkpass(password, &line[username_length+1]) != 0 ) { + if ( bcrypt_checkpass(password, &line[username_length + 1]) != 0 ) { continue; } @@ -90,7 +91,7 @@ check_user(const char *const username, const char *const password) found = true; } - fclose(db); + (void) fclose(database); return found; } @@ -105,16 +106,16 @@ check_user(const char *const username, const char *const password) bool check_user_if_exists(const char *username) { - FILE *db = fopen(USER_DATABASE, "r"); - if ( db == NULL ) { + FILE *database = fopen(USER_DATABASE, "r"); + if ( database == NULL ) { return false; } const size_t username_length = strlen(username); - char line[256]; + char line[MAX_LINE_LENGTH]; bool found = false; - while ( !found && fgets(line, sizeof line, db) ) { + while ( !found && fgets(line, sizeof line, database) ) { line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) if ( strncmp(line, username, username_length) != 0 ) { @@ -129,7 +130,7 @@ check_user_if_exists(const char *username) found = true; } - fclose(db); + (void) fclose(database); return found; } @@ -143,6 +144,3 @@ main(void) return EXIT_SUCCESS; } - - - diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..93961f5 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,5 @@ +-Wall +-Werror +-Wno-pointer-sign +-std=c17 +-I../lua-bcrypt/src diff --git a/makefile b/makefile index f98f66c..e0a8644 100644 --- a/makefile +++ b/makefile @@ -1,13 +1,22 @@ all: bcrypt-test -CFLAGS =-Wall -Werror -std=c99 -pedantic -g +# git clone https://github.com/mikejsavage/lua-bcrypt.git +LUA_BCRYPT_PATH=../lua-bcrypt/src -bcrypt-test: bcrypt-test.o - cc -o $@ bcrypt-test.o lua-bcrypt/bcrypt.a +CFLAGS =-Wall -Werror -Wno-pointer-sign -std=c17 -O2 -I$(LUA_BCRYPT_PATH) -bcrypt-test.o: bcrypt-test.c +bcrypt-test: bcrypt-test.o bcrypt-lib.a + cc -o $@ bcrypt-test.o bcrypt-lib.a + +bcrypt-lib.a: bcrypt.o blowfish.o ggentropy.o safebfuns.o + ar -crs $@ $^ + +%.o: %.c + cc $(CFLAGS) -o $@ -c $< + +%.o: $(LUA_BCRYPT_PATH)/%.c cc $(CFLAGS) -o $@ -c $< .PHONY: clean clean: - rm -f bcrypt-test bcrypt-test.o + rm -f bcrypt-test *.a *.o -- cgit v1.3