It compiles no encript
This commit is contained in:
		| @@ -1,45 +0,0 @@ | |||||||
| #pragma once |  | ||||||
|  |  | ||||||
| struct LoginInfoPointer; |  | ||||||
| class Buffer; |  | ||||||
| class Cryptography; |  | ||||||
| #include <string> |  | ||||||
| #include <optional> |  | ||||||
|  |  | ||||||
| enum class Arg |  | ||||||
| { |  | ||||||
| 	Get, // get password for label |  | ||||||
| 	Generate, // generate password for label |  | ||||||
| 	List, // list all labels |  | ||||||
| 	Delete, // delete password for label |  | ||||||
| 	Print_all_p, // print all passwords |  | ||||||
| 	Input, // input password for label |  | ||||||
| 	Change, // change main password |  | ||||||
| 	Show, // show password for label |  | ||||||
| 	Error, // error |  | ||||||
| 	Username, // update username |  | ||||||
| 	Name, // update label name |  | ||||||
| 	File, // select save file |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| Arg get_args(int argc, char** argv, char** label); |  | ||||||
|  |  | ||||||
| std::optional<LoginInfoPointer> arg_get(Buffer& decrypted_buffer, const char* label); |  | ||||||
|  |  | ||||||
| std::optional<LoginInfoPointer> arg_new_password(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto, bool generate); |  | ||||||
|  |  | ||||||
| void arg_username(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto); |  | ||||||
|  |  | ||||||
| void arg_label_name(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto); |  | ||||||
|  |  | ||||||
| void arg_list(Buffer& decrypted_buffer); |  | ||||||
|  |  | ||||||
| void arg_delete(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto); |  | ||||||
|  |  | ||||||
| void arg_print_all_p(Buffer& decrypted_buffer, std::string& user_pass); |  | ||||||
|  |  | ||||||
| void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& user_pass, Cryptography& crypto); |  | ||||||
|  |  | ||||||
| void arg_show(Buffer& decrypted_buffer, const char* label); |  | ||||||
|  |  | ||||||
| void arg_file(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto, std::string& save_location_path); |  | ||||||
							
								
								
									
										48
									
								
								include/arg_func.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								include/arg_func.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,48 @@ | |||||||
|  | #ifndef ARG_FUNC_HPP | ||||||
|  | #define ARG_FUNC_HPP | ||||||
|  |  | ||||||
|  | struct LoginInfoPointer; | ||||||
|  | class Buffer; | ||||||
|  | class Cryptography; | ||||||
|  | #include <string> | ||||||
|  | #include <optional> | ||||||
|  |  | ||||||
|  | enum class Arg | ||||||
|  | { | ||||||
|  | 	Get,				 // get password for label | ||||||
|  | 	Generate,		 // generate password for label | ||||||
|  | 	List,				 // list all labels | ||||||
|  | 	Delete,			 // delete password for label | ||||||
|  | 	Print_all_p, // print all passwords | ||||||
|  | 	Input,			 // input password for label | ||||||
|  | 	Change,			 // change main password | ||||||
|  | 	Show,				 // show password for label | ||||||
|  | 	Error,			 // error | ||||||
|  | 	Username,		 // update username | ||||||
|  | 	Name,				 // update label name | ||||||
|  | 	File,				 // select save file | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | Arg get_args(int argc, char **argv, char **label); | ||||||
|  |  | ||||||
|  | std::optional<LoginInfoPointer> arg_get(Buffer &decrypted_buffer, const char *label); | ||||||
|  |  | ||||||
|  | std::optional<LoginInfoPointer> arg_new_password(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto, bool generate); | ||||||
|  |  | ||||||
|  | void arg_username(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto); | ||||||
|  |  | ||||||
|  | void arg_label_name(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto); | ||||||
|  |  | ||||||
|  | void arg_list(Buffer &decrypted_buffer); | ||||||
|  |  | ||||||
|  | void arg_delete(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto); | ||||||
|  |  | ||||||
|  | void arg_print_all_p(Buffer &decrypted_buffer, std::string &user_pass); | ||||||
|  |  | ||||||
|  | void arg_change(Buffer &decrypted_buffer, Buffer &encrypted_buffer, std::string &user_pass, Cryptography &crypto); | ||||||
|  |  | ||||||
|  | void arg_show(Buffer &decrypted_buffer, const char *label); | ||||||
|  |  | ||||||
|  | void arg_file(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label, Cryptography &crypto, std::string &save_location_path); | ||||||
|  |  | ||||||
|  | #endif | ||||||
| @@ -1,4 +1,6 @@ | |||||||
| #pragma once | #ifndef BUFFER_HPP | ||||||
|  | #define BUFFER_HPP | ||||||
|  | 
 | ||||||
| #include <cstdint> | #include <cstdint> | ||||||
| #include <string> | #include <string> | ||||||
| 
 | 
 | ||||||
| @@ -33,5 +35,6 @@ public: | |||||||
| 
 | 
 | ||||||
| 	bool load_from_file(); | 	bool load_from_file(); | ||||||
| 	bool load_from_file(std::string file_path); | 	bool load_from_file(std::string file_path); | ||||||
| 
 |  | ||||||
| }; | }; | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
| @@ -1,23 +0,0 @@ | |||||||
| #pragma once |  | ||||||
| #include <cstdint> |  | ||||||
|  |  | ||||||
| struct evp_cipher_ctx_st; |  | ||||||
| typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX; |  | ||||||
|  |  | ||||||
| class Buffer; |  | ||||||
|  |  | ||||||
| class Cryptography |  | ||||||
| { |  | ||||||
| public: |  | ||||||
| 	Cryptography(const char* password, size_t size); |  | ||||||
| 	~Cryptography(); |  | ||||||
| 	bool encrypt(Buffer* plain, Buffer* encrypted); |  | ||||||
| 	bool decrypt(Buffer* encrypted, Buffer* decrypted); |  | ||||||
| 	bool generate_key_and_iv_from_password(const char* password, size_t size); |  | ||||||
|  |  | ||||||
| private: |  | ||||||
| 	uint8_t key[32] = { 0 }; |  | ||||||
| 	uint8_t iv[16] = { 0 }; |  | ||||||
| 	EVP_CIPHER_CTX* ctx = nullptr; |  | ||||||
| 	bool handleErrors(); |  | ||||||
| }; |  | ||||||
							
								
								
									
										27
									
								
								include/cryptography.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								include/cryptography.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | #ifndef CRYPTOGRAPHY_HPP | ||||||
|  | #define CRYPTOGRAPHY_HPP | ||||||
|  |  | ||||||
|  | #include <cstdint> | ||||||
|  |  | ||||||
|  | struct evp_cipher_ctx_st; | ||||||
|  | typedef struct evp_cipher_ctx_st EVP_CIPHER_CTX; | ||||||
|  |  | ||||||
|  | class Buffer; | ||||||
|  |  | ||||||
|  | class Cryptography | ||||||
|  | { | ||||||
|  | public: | ||||||
|  | 	Cryptography(const char *password, size_t size); | ||||||
|  | 	~Cryptography(); | ||||||
|  | 	bool encrypt(Buffer *plain, Buffer *encrypted); | ||||||
|  | 	bool decrypt(Buffer *encrypted, Buffer *decrypted); | ||||||
|  | 	bool generate_key_and_iv_from_password(const char *password, size_t size); | ||||||
|  |  | ||||||
|  | private: | ||||||
|  | 	uint8_t key[32] = {0}; | ||||||
|  | 	uint8_t iv[16] = {0}; | ||||||
|  | 	EVP_CIPHER_CTX *ctx = nullptr; | ||||||
|  | 	bool handleErrors(); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #endif | ||||||
| @@ -1,35 +0,0 @@ | |||||||
| #pragma once |  | ||||||
| #include <string> |  | ||||||
|  |  | ||||||
| class Buffer; |  | ||||||
|  |  | ||||||
| struct LoginInfo |  | ||||||
| { |  | ||||||
| 	uint32_t label; |  | ||||||
| 	uint32_t username; |  | ||||||
| 	uint32_t password;  |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| struct LoginInfoPointer |  | ||||||
| { |  | ||||||
| 	const char* label; |  | ||||||
| 	const char* username; |  | ||||||
| 	const char* password; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| struct Index |  | ||||||
| { |  | ||||||
| 	uint32_t count; |  | ||||||
| 	uint32_t offset; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| int find_logininfo_in_buffer(Buffer& buffer, const char* label); |  | ||||||
|  |  | ||||||
| void delete_logininfo_from_buffer(Buffer& buffer, int index_of_pass); |  | ||||||
|  |  | ||||||
| void add_logininfo_to_buffer(Buffer& buffer, const char* label, const char* username, const char* password); |  | ||||||
|  |  | ||||||
| LoginInfoPointer get_logininfo_pointer_from_buffer(Buffer& buffer, int index_of_pass); |  | ||||||
|  |  | ||||||
| void generate_password(std::string& password, int len); |  | ||||||
|  |  | ||||||
							
								
								
									
										38
									
								
								include/func.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								include/func.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | |||||||
|  | #ifndef FUNC_HPP | ||||||
|  | #define FUNC_HPP | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
|  | class Buffer; | ||||||
|  |  | ||||||
|  | struct LoginInfo | ||||||
|  | { | ||||||
|  | 	uint32_t label; | ||||||
|  | 	uint32_t username; | ||||||
|  | 	uint32_t password; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | struct LoginInfoPointer | ||||||
|  | { | ||||||
|  | 	const char *label; | ||||||
|  | 	const char *username; | ||||||
|  | 	const char *password; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | struct Index | ||||||
|  | { | ||||||
|  | 	uint32_t count; | ||||||
|  | 	uint32_t offset; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | int find_logininfo_in_buffer(Buffer &buffer, const char *label); | ||||||
|  |  | ||||||
|  | void delete_logininfo_from_buffer(Buffer &buffer, int index_of_pass); | ||||||
|  |  | ||||||
|  | void add_logininfo_to_buffer(Buffer &buffer, const char *label, const char *username, const char *password); | ||||||
|  |  | ||||||
|  | LoginInfoPointer get_logininfo_pointer_from_buffer(Buffer &buffer, int index_of_pass); | ||||||
|  |  | ||||||
|  | void generate_password(std::string &password, int len); | ||||||
|  |  | ||||||
|  | #endif | ||||||
| @@ -1,13 +0,0 @@ | |||||||
| #pragma once |  | ||||||
|  |  | ||||||
|  |  | ||||||
| enum class Glob_Result { |  | ||||||
| 	OOM_ERROR, |  | ||||||
| 	ENCODING_ERROR, |  | ||||||
| 	SYNTAX_ERROR, |  | ||||||
| 	UNMATCHED, |  | ||||||
| 	MATCHED, |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| // https://github.com/tsoding/glob.h |  | ||||||
| Glob_Result glob(const char* pattern, const char* text); |  | ||||||
							
								
								
									
										16
									
								
								include/glob.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								include/glob.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | |||||||
|  | #ifndef GLOB_HPP | ||||||
|  | #define GLOB_HPP | ||||||
|  |  | ||||||
|  | enum class Glob_Result | ||||||
|  | { | ||||||
|  | 	OOM_ERROR, | ||||||
|  | 	ENCODING_ERROR, | ||||||
|  | 	SYNTAX_ERROR, | ||||||
|  | 	UNMATCHED, | ||||||
|  | 	MATCHED, | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | // https://github.com/tsoding/glob.h | ||||||
|  | Glob_Result glob(const char *pattern, const char *text); | ||||||
|  |  | ||||||
|  | #endif | ||||||
							
								
								
									
										11
									
								
								include/sys.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								include/sys.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | #ifndef WIN_HPP | ||||||
|  | #define WIN_HPP | ||||||
|  |  | ||||||
|  | #include <optional> | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
|  | bool put_data_on_clipboard(const char *text); | ||||||
|  | std::string get_user_password(); | ||||||
|  | std::optional<std::string> get_save_path(); | ||||||
|  |  | ||||||
|  | #endif | ||||||
| @@ -1,6 +0,0 @@ | |||||||
| #pragma once |  | ||||||
| #include <optional> |  | ||||||
|  |  | ||||||
| bool put_data_on_clipboard(const char* text); |  | ||||||
| std::string get_user_password(); |  | ||||||
| std::optional<std::string> get_save_path(); |  | ||||||
| @@ -1,11 +1,11 @@ | |||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <fstream> | #include <fstream> | ||||||
| 
 | 
 | ||||||
| #include "win.h" | #include "sys.hpp" | ||||||
| #include "buffer.h" | #include "buffer.hpp" | ||||||
| #include "cryptography.h" | #include "cryptography.hpp" | ||||||
| #include "func.h" | #include "func.hpp" | ||||||
| #include "arg_func.h" | #include "arg_func.hpp" | ||||||
| 
 | 
 | ||||||
| #ifdef _DEBUG | #ifdef _DEBUG | ||||||
| #define DEBUG 0 | #define DEBUG 0 | ||||||
| @@ -30,16 +30,17 @@ int main(int argc, char** argv) | |||||||
| #else | #else | ||||||
| 
 | 
 | ||||||
| 	args = get_args(argc, argv, &label); | 	args = get_args(argc, argv, &label); | ||||||
| 	if (args == Arg::Error) return 1; | 	if (args == Arg::Error) | ||||||
|  | 		return 1; | ||||||
| 
 | 
 | ||||||
| 	save_location_path = get_save_path(); | 	save_location_path = get_save_path(); | ||||||
| 	if (!save_location_path.has_value()) | 	if (!save_location_path.has_value()) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error geting file for save path\n"); | 		printf("Error geting file for save path\n"); | ||||||
| 		return 1; | 		return 1; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	std::fstream ifile(save_location_path.value(), std::ios::binary || std::ios::in); | 	std::ifstream ifile(save_location_path.value(), std::ios::binary); | ||||||
| 	if (ifile.is_open()) | 	if (ifile.is_open()) | ||||||
| 	{ | 	{ | ||||||
| 		std::getline(ifile, save_location); | 		std::getline(ifile, save_location); | ||||||
| @@ -48,7 +49,7 @@ int main(int argc, char** argv) | |||||||
| 
 | 
 | ||||||
| 	if (save_location.empty() && args != Arg::File) | 	if (save_location.empty() && args != Arg::File) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("No save location, try selecting folder (-f)\n"); | 		printf("No save location, try selecting folder (-f)\n"); | ||||||
| 		return 1; | 		return 1; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @@ -58,18 +59,18 @@ int main(int argc, char** argv) | |||||||
| 	// load file
 | 	// load file
 | ||||||
| 	if (!encrypted_buffer.load_from_file(save_location)) | 	if (!encrypted_buffer.load_from_file(save_location)) | ||||||
| 		// if file doesn't exist, check if it's a new password is being generated if not, exit
 | 		// if file doesn't exist, check if it's a new password is being generated if not, exit
 | ||||||
| 		if (!(args == Arg::Generate || args == Arg::Input || args == Arg::File)) { | 		if (!(args == Arg::Generate || args == Arg::Input || args == Arg::File)) | ||||||
| 			printf_s("No passwords, try generating password (-g or -i)\n"); | 		{ | ||||||
|  | 			printf("No passwords, try generating password (-g or -i)\n"); | ||||||
| 			return 1; | 			return 1; | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| #if !DEBUG | #if !DEBUG | ||||||
| 	printf_s("Input main password:"); | 	printf("Input main password:"); | ||||||
| 	user_pass = get_user_password(); | 	user_pass = get_user_password(); | ||||||
| 	if (user_pass.empty()) | 	if (user_pass.empty()) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error getting password\n"); | 		printf("Error getting password\n"); | ||||||
| 		return 1; | 		return 1; | ||||||
| 	} | 	} | ||||||
| #endif // !1
 | #endif // !1
 | ||||||
| @@ -79,10 +80,12 @@ int main(int argc, char** argv) | |||||||
| 	Buffer decrypted_buffer; | 	Buffer decrypted_buffer; | ||||||
| 	// check if encrypted buffer is empty if not, decrypt it
 | 	// check if encrypted buffer is empty if not, decrypt it
 | ||||||
| 	if (encrypted_buffer.size > 0) | 	if (encrypted_buffer.size > 0) | ||||||
| 		if (!crypto.decrypt(&encrypted_buffer, &decrypted_buffer)) return 1; | 		if (!crypto.decrypt(&encrypted_buffer, &decrypted_buffer)) | ||||||
|  | 			return 1; | ||||||
| 
 | 
 | ||||||
| 	// if decrypted buffer is empty, add index
 | 	// if decrypted buffer is empty, add index
 | ||||||
| 	if (decrypted_buffer.taken < sizeof(Index)) { | 	if (decrypted_buffer.taken < sizeof(Index)) | ||||||
|  | 	{ | ||||||
| 		Index index = {0}; | 		Index index = {0}; | ||||||
| 		index.offset = sizeof(Index); | 		index.offset = sizeof(Index); | ||||||
| 		decrypted_buffer.add_end((uint8_t *)&index, sizeof(Index)); | 		decrypted_buffer.add_end((uint8_t *)&index, sizeof(Index)); | ||||||
| @@ -137,18 +140,19 @@ int main(int argc, char** argv) | |||||||
| 		break; | 		break; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (!login_info.has_value()) return 0; | 	if (!login_info.has_value()) | ||||||
|  | 		return 0; | ||||||
| 
 | 
 | ||||||
| #if DEBUG | #if DEBUG | ||||||
| 
 | 
 | ||||||
| 	printf_s("Username: %s\n", login_info.value().username); | 	printf("Username: %s\n", login_info.value().username); | ||||||
| 	printf_s("Password: %s\n", login_info.value().password); | 	printf("Password: %s\n", login_info.value().password); | ||||||
| 
 | 
 | ||||||
| #else | #else | ||||||
| 
 | 
 | ||||||
| 	printf_s("Username: %s\n", login_info.value().username); | 	printf("Username: %s\n", login_info.value().username); | ||||||
| 	put_data_on_clipboard(login_info.value().password); | 	put_data_on_clipboard(login_info.value().password); | ||||||
| 	printf_s("Password copied to clipboard\n"); | 	printf("Password copied to clipboard\n"); | ||||||
| 
 | 
 | ||||||
| #endif // _DEBUG
 | #endif // _DEBUG
 | ||||||
| 
 | 
 | ||||||
							
								
								
									
										30
									
								
								makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								makefile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | |||||||
|  | CC=g++ | ||||||
|  |  | ||||||
|  | CFLAGS= -std=c++23 | ||||||
|  |  | ||||||
|  | INCLUDE_DIR= include | ||||||
|  |  | ||||||
|  | SRC_DIR= source | ||||||
|  |  | ||||||
|  | # Get all cpp files from src directory | ||||||
|  | SRCS := $(wildcard $(SRC_DIR)/*.cpp) | ||||||
|  |  | ||||||
|  | # Get all header files from include directory | ||||||
|  | HDRS := $(wildcard $(INCLUDE_DIR)/*.h) | ||||||
|  |  | ||||||
|  | all: main run | ||||||
|  |  | ||||||
|  | main:  main.cpp $(SRCS) $(HDRS) | ||||||
|  | 	$(CC) $(CFLAGS) -O3 -I$(INCLUDE_DIR) $(SRCS) main.cpp -o main | ||||||
|  |  | ||||||
|  | debug:  main.cpp $(SRCS) $(HDRS) | ||||||
|  | 	$(CC) $(CFLAGS) -I$(INCLUDE_DIR) $(SRCS) -g main.cpp -o main | ||||||
|  |  | ||||||
|  | run: main | ||||||
|  | 	./main | ||||||
|  |  | ||||||
|  | zip: | ||||||
|  | 	zip -r main.zip main.cpp $(INCLUDE_DIR) $(SRC_DIR) makefile | ||||||
|  |  | ||||||
|  | clean: | ||||||
|  | 	rm main | ||||||
| @@ -2,34 +2,36 @@ | |||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <sstream> | #include <sstream> | ||||||
| #include <fstream> | #include <fstream> | ||||||
|  | #include <cstring> | ||||||
|  |  | ||||||
| #include "arg_func.h" | #include "arg_func.hpp" | ||||||
| #include "func.h" | #include "func.hpp" | ||||||
| #include "win.h" | #include "sys.hpp" | ||||||
| #include "func.h" | #include "func.hpp" | ||||||
| #include "buffer.h" | #include "buffer.hpp" | ||||||
| #include "cryptography.h" | #include "cryptography.hpp" | ||||||
|  |  | ||||||
| void print_args() | void print_args() | ||||||
| { | { | ||||||
| 	printf_s(" Usage:\n\n"); | 	printf(" Usage:\n\n"); | ||||||
| 	printf_s(" password_manager.exe [flags]\n\n"); | 	printf(" password_manager.exe [flags]\n\n"); | ||||||
| 	printf_s(" Flags:\n\n"); | 	printf(" Flags:\n\n"); | ||||||
| 	printf_s(" -h                   print this message\n"); | 	printf(" -h                   print this message\n"); | ||||||
| 	printf_s(" <label>              get password of this label can use GLOB\n"); | 	printf(" <label>              get password of this label can use GLOB\n"); | ||||||
| 	printf_s(" -g <label>           generate password of this label (or update if exists)\n"); | 	printf(" -g <label>           generate password of this label (or update if exists)\n"); | ||||||
| 	printf_s(" -i <label>           input new password for this label (or update if exists)\n"); | 	printf(" -i <label>           input new password for this label (or update if exists)\n"); | ||||||
| 	printf_s(" -d <label>           delete password of this label\n"); | 	printf(" -d <label>           delete password of this label\n"); | ||||||
| 	printf_s(" -s <label>           show password for this label\n"); | 	printf(" -s <label>           show password for this label\n"); | ||||||
| 	printf_s(" -u <label>           update username for this label\n"); | 	printf(" -u <label>           update username for this label\n"); | ||||||
| 	printf_s(" -n <label>           update label name\n"); | 	printf(" -n <label>           update label name\n"); | ||||||
| 	printf_s(" -l                   list all labels\n"); | 	printf(" -l                   list all labels\n"); | ||||||
| 	printf_s(" -p                   print all passwords\n"); | 	printf(" -p                   print all passwords\n"); | ||||||
| 	printf_s(" -c                   change master password\n"); | 	printf(" -c                   change master password\n"); | ||||||
| 	printf_s(" -f <folder path>     select save folder\n"); | 	printf(" -f <folder path>     select save folder\n"); | ||||||
| } | } | ||||||
|  |  | ||||||
| Arg get_args(int argc, char** argv, char** label) { | Arg get_args(int argc, char **argv, char **label) | ||||||
|  | { | ||||||
| 	if (argc < 2) | 	if (argc < 2) | ||||||
| 	{ | 	{ | ||||||
| 		print_args(); | 		print_args(); | ||||||
| @@ -42,9 +44,12 @@ Arg get_args(int argc, char** argv, char** label) { | |||||||
| 		return Arg::Error; | 		return Arg::Error; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (!strcmp("-l", argv[1])) return Arg::List; | 	if (!strcmp("-l", argv[1])) | ||||||
| 	if (!strcmp("-p", argv[1])) return Arg::Print_all_p; | 		return Arg::List; | ||||||
| 	if (!strcmp("-c", argv[1])) return Arg::Change; | 	if (!strcmp("-p", argv[1])) | ||||||
|  | 		return Arg::Print_all_p; | ||||||
|  | 	if (!strcmp("-c", argv[1])) | ||||||
|  | 		return Arg::Change; | ||||||
|  |  | ||||||
| 	if (argc < 3) | 	if (argc < 3) | ||||||
| 	{ | 	{ | ||||||
| @@ -54,13 +59,20 @@ Arg get_args(int argc, char** argv, char** label) { | |||||||
|  |  | ||||||
| 	*label = argv[2]; | 	*label = argv[2]; | ||||||
|  |  | ||||||
| 	if (!strcmp("-g", argv[1])) return Arg::Generate; | 	if (!strcmp("-g", argv[1])) | ||||||
| 	if (!strcmp("-d", argv[1])) return Arg::Delete; | 		return Arg::Generate; | ||||||
| 	if (!strcmp("-i", argv[1]))	return Arg::Input; | 	if (!strcmp("-d", argv[1])) | ||||||
| 	if (!strcmp("-s", argv[1])) return Arg::Show; | 		return Arg::Delete; | ||||||
| 	if (!strcmp("-u", argv[1])) return Arg::Username; | 	if (!strcmp("-i", argv[1])) | ||||||
| 	if (!strcmp("-n", argv[1])) return Arg::Name; | 		return Arg::Input; | ||||||
| 	if (!strcmp("-f", argv[1])) return Arg::File; | 	if (!strcmp("-s", argv[1])) | ||||||
|  | 		return Arg::Show; | ||||||
|  | 	if (!strcmp("-u", argv[1])) | ||||||
|  | 		return Arg::Username; | ||||||
|  | 	if (!strcmp("-n", argv[1])) | ||||||
|  | 		return Arg::Name; | ||||||
|  | 	if (!strcmp("-f", argv[1])) | ||||||
|  | 		return Arg::File; | ||||||
|  |  | ||||||
| 	return Arg::Error; | 	return Arg::Error; | ||||||
| } | } | ||||||
| @@ -68,8 +80,9 @@ Arg get_args(int argc, char** argv, char** label) { | |||||||
| std::optional<LoginInfoPointer> arg_get(Buffer &decrypted_buffer, const char *label) | std::optional<LoginInfoPointer> arg_get(Buffer &decrypted_buffer, const char *label) | ||||||
| { | { | ||||||
| 	int pass = find_logininfo_in_buffer(decrypted_buffer, label); | 	int pass = find_logininfo_in_buffer(decrypted_buffer, label); | ||||||
| 	if (pass < 0) { | 	if (pass < 0) | ||||||
| 		printf_s("Password not found\n"); | 	{ | ||||||
|  | 		printf("Password not found\n"); | ||||||
| 		return {}; | 		return {}; | ||||||
| 	} | 	} | ||||||
| 	return get_logininfo_pointer_from_buffer(decrypted_buffer, pass); | 	return get_logininfo_pointer_from_buffer(decrypted_buffer, pass); | ||||||
| @@ -82,25 +95,26 @@ std::optional<LoginInfoPointer> arg_new_password(Buffer& decrypted_buffer, Buffe | |||||||
| 	std::string name = label; | 	std::string name = label; | ||||||
| 	std::string password = ""; | 	std::string password = ""; | ||||||
|  |  | ||||||
| 	if (pass >= 0) { | 	if (pass >= 0) | ||||||
|  | 	{ | ||||||
| 		LoginInfoPointer lip = get_logininfo_pointer_from_buffer(decrypted_buffer, pass); | 		LoginInfoPointer lip = get_logininfo_pointer_from_buffer(decrypted_buffer, pass); | ||||||
| 		username = lip.username; | 		username = lip.username; | ||||||
| 		name = lip.label; | 		name = lip.label; | ||||||
| 		delete_logininfo_from_buffer(decrypted_buffer, pass); | 		delete_logininfo_from_buffer(decrypted_buffer, pass); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	printf_s("New password for %s: \n", name.c_str()); | 	printf("New password for %s: \n", name.c_str()); | ||||||
|  |  | ||||||
| 	if (pass < 0) | 	if (pass < 0) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Input username: "); | 		printf("Input username: "); | ||||||
| 		std::getline(std::cin, username); | 		std::getline(std::cin, username); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (generate) | 	if (generate) | ||||||
| 	{ | 	{ | ||||||
| 		int default_length = 15; | 		int default_length = 15; | ||||||
| 		printf_s("Input password length [%d]: ", default_length); | 		printf("Input password length [%d]: ", default_length); | ||||||
| 		std::string input; | 		std::string input; | ||||||
| 		int length = default_length; | 		int length = default_length; | ||||||
|  |  | ||||||
| @@ -108,18 +122,19 @@ std::optional<LoginInfoPointer> arg_new_password(Buffer& decrypted_buffer, Buffe | |||||||
| 		if (!input.empty()) | 		if (!input.empty()) | ||||||
| 		{ | 		{ | ||||||
| 			std::istringstream iss(input); | 			std::istringstream iss(input); | ||||||
| 			if (!(iss >> length)) length = default_length; | 			if (!(iss >> length)) | ||||||
|  | 				length = default_length; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		generate_password(password, length); | 		generate_password(password, length); | ||||||
| 	} | 	} | ||||||
| 	else | 	else | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Input new password: \n"); | 		printf("Input new password: \n"); | ||||||
| 		password = get_user_password(); | 		password = get_user_password(); | ||||||
| 		if (password.empty()) | 		if (password.empty()) | ||||||
| 		{ | 		{ | ||||||
| 			printf_s("error getting password\n"); | 			printf("error getting password\n"); | ||||||
| 			return {}; | 			return {}; | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -136,7 +151,7 @@ void arg_username(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char | |||||||
| 	int pass = find_logininfo_in_buffer(decrypted_buffer, label); | 	int pass = find_logininfo_in_buffer(decrypted_buffer, label); | ||||||
| 	if (pass < 0) | 	if (pass < 0) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("LoginInfo not found\n"); | 		printf("LoginInfo not found\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -144,7 +159,7 @@ void arg_username(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char | |||||||
| 	std::string password = lip.password; | 	std::string password = lip.password; | ||||||
| 	std::string name = lip.label; | 	std::string name = lip.label; | ||||||
|  |  | ||||||
| 	printf_s("Input username for %s: ", name.c_str()); | 	printf("Input username for %s: ", name.c_str()); | ||||||
| 	std::string username; | 	std::string username; | ||||||
| 	std::getline(std::cin, username); | 	std::getline(std::cin, username); | ||||||
|  |  | ||||||
| @@ -160,7 +175,7 @@ void arg_label_name(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const ch | |||||||
|  |  | ||||||
| 	if (pass < 0) | 	if (pass < 0) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("LoginInfo not found\n"); | 		printf("LoginInfo not found\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -168,7 +183,7 @@ void arg_label_name(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const ch | |||||||
| 	std::string password = lip.password; | 	std::string password = lip.password; | ||||||
| 	std::string username = lip.username; | 	std::string username = lip.username; | ||||||
|  |  | ||||||
| 	printf_s("Input new label name for %s:", lip.label); | 	printf("Input new label name for %s:", lip.label); | ||||||
| 	std::string name; | 	std::string name; | ||||||
| 	std::getline(std::cin, name); | 	std::getline(std::cin, name); | ||||||
|  |  | ||||||
| @@ -185,17 +200,17 @@ void arg_list(Buffer& decrypted_buffer) | |||||||
|  |  | ||||||
| 	for (size_t i = 0; i < index->count; i++) | 	for (size_t i = 0; i < index->count; i++) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("label: %s\n", decrypted_buffer.buffer + pass[i].label + index->offset); | 		printf("label: %s\n", decrypted_buffer.buffer + pass[i].label + index->offset); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| void arg_delete(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label_to_del, Cryptography &crypto) | void arg_delete(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *label_to_del, Cryptography &crypto) | ||||||
| { | { | ||||||
| 	printf_s("Deleting password for %s\n", label_to_del); | 	printf("Deleting password for %s\n", label_to_del); | ||||||
| 	int pass = find_logininfo_in_buffer(decrypted_buffer, label_to_del); | 	int pass = find_logininfo_in_buffer(decrypted_buffer, label_to_del); | ||||||
| 	if (pass < 0) | 	if (pass < 0) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Password not found\n"); | 		printf("Password not found\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -203,92 +218,93 @@ void arg_delete(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* | |||||||
|  |  | ||||||
| 	crypto.encrypt(&decrypted_buffer, &encrypted_buffer); | 	crypto.encrypt(&decrypted_buffer, &encrypted_buffer); | ||||||
| 	encrypted_buffer.save_to_file(); | 	encrypted_buffer.save_to_file(); | ||||||
| 	printf_s("Password deleted\n"); | 	printf("Password deleted\n"); | ||||||
| } | } | ||||||
|  |  | ||||||
| void arg_print_all_p(Buffer &decrypted_buffer, std::string &user_pass) | void arg_print_all_p(Buffer &decrypted_buffer, std::string &user_pass) | ||||||
| { | { | ||||||
| 	printf_s("Input main password for confirmation:"); | 	printf("Input main password for confirmation:"); | ||||||
| 	std::string new_string = get_user_password(); | 	std::string new_string = get_user_password(); | ||||||
| 	if (new_string.empty()) | 	if (new_string.empty()) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error getting password\n"); | 		printf("Error getting password\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
| 	if (new_string != user_pass) | 	if (new_string != user_pass) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Wrong password\n"); | 		printf("Wrong password\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	Index *index = (Index *)decrypted_buffer.buffer; | 	Index *index = (Index *)decrypted_buffer.buffer; | ||||||
| 	LoginInfo *pass = (LoginInfo *)(decrypted_buffer.buffer + sizeof(Index)); | 	LoginInfo *pass = (LoginInfo *)(decrypted_buffer.buffer + sizeof(Index)); | ||||||
|  |  | ||||||
| 	printf_s("\n"); | 	printf("\n"); | ||||||
| 	for (size_t i = 0; i < index->count; i++) | 	for (size_t i = 0; i < index->count; i++) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("label: %-10s\t\t", decrypted_buffer.buffer + pass[i].label + index->offset); | 		printf("label: %-10s\t\t", decrypted_buffer.buffer + pass[i].label + index->offset); | ||||||
| 		printf_s("username: %-10s\t\t", decrypted_buffer.buffer + pass[i].username + index->offset); | 		printf("username: %-10s\t\t", decrypted_buffer.buffer + pass[i].username + index->offset); | ||||||
| 		printf_s("password: %s\n", decrypted_buffer.buffer + pass[i].password + index->offset); | 		printf("password: %s\n", decrypted_buffer.buffer + pass[i].password + index->offset); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| void arg_change(Buffer &decrypted_buffer, Buffer &encrypted_buffer, std::string &user_pass, Cryptography &crypto) | void arg_change(Buffer &decrypted_buffer, Buffer &encrypted_buffer, std::string &user_pass, Cryptography &crypto) | ||||||
| { | { | ||||||
| 	printf_s("Input main password for confirmation:"); | 	printf("Input main password for confirmation:"); | ||||||
|  |  | ||||||
| 	std::string new_string = get_user_password(); | 	std::string new_string = get_user_password(); | ||||||
| 	if (new_string.empty()) | 	if (new_string.empty()) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error getting password\n"); | 		printf("Error getting password\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (new_string != user_pass) | 	if (new_string != user_pass) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Passwords don't match\n"); | 		printf("Passwords don't match\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	printf_s("Input new password:"); | 	printf("Input new password:"); | ||||||
| 	new_string = get_user_password(); | 	new_string = get_user_password(); | ||||||
| 	if (new_string.empty()) | 	if (new_string.empty()) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error getting password\n"); | 		printf("Error getting password\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	printf_s("Input new password again:"); | 	printf("Input new password again:"); | ||||||
| 	user_pass = get_user_password(); | 	user_pass = get_user_password(); | ||||||
| 	if (user_pass.empty()) | 	if (user_pass.empty()) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error getting password\n"); | 		printf("Error getting password\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (new_string != user_pass) | 	if (new_string != user_pass) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Passwords don't match\n"); | 		printf("Passwords don't match\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	crypto.generate_key_and_iv_from_password(new_string.c_str(), new_string.size()); | 	crypto.generate_key_and_iv_from_password(new_string.c_str(), new_string.size()); | ||||||
| 	crypto.encrypt(&decrypted_buffer, &encrypted_buffer); | 	crypto.encrypt(&decrypted_buffer, &encrypted_buffer); | ||||||
| 	encrypted_buffer.save_to_file(); | 	encrypted_buffer.save_to_file(); | ||||||
| 	printf_s("Password changed\n"); | 	printf("Password changed\n"); | ||||||
| } | } | ||||||
|  |  | ||||||
| void arg_show(Buffer& decrypted_buffer, const char* label) { | void arg_show(Buffer &decrypted_buffer, const char *label) | ||||||
|  | { | ||||||
|  |  | ||||||
| 	int pass = find_logininfo_in_buffer(decrypted_buffer, label); | 	int pass = find_logininfo_in_buffer(decrypted_buffer, label); | ||||||
|  |  | ||||||
| 	if (pass < 0) | 	if (pass < 0) | ||||||
| 		printf_s("Password not found\n"); | 		printf("Password not found\n"); | ||||||
| 	else | 	else | ||||||
| 	{ | 	{ | ||||||
| 		LoginInfoPointer lip = get_logininfo_pointer_from_buffer(decrypted_buffer, pass); | 		LoginInfoPointer lip = get_logininfo_pointer_from_buffer(decrypted_buffer, pass); | ||||||
| 		printf_s("Username: %s\n", lip.username); | 		printf("Username: %s\n", lip.username); | ||||||
| 		printf_s("Password: %s\n", lip.password); | 		printf("Password: %s\n", lip.password); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -301,7 +317,8 @@ void arg_file(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* la | |||||||
| 	file << save << std::endl; | 	file << save << std::endl; | ||||||
| 	file.close(); | 	file.close(); | ||||||
|  |  | ||||||
| 	if (decrypted_buffer.taken <= sizeof(Index)) return; | 	if (decrypted_buffer.taken <= sizeof(Index)) | ||||||
|  | 		return; | ||||||
|  |  | ||||||
| 	crypto.encrypt(&decrypted_buffer, &encrypted_buffer); | 	crypto.encrypt(&decrypted_buffer, &encrypted_buffer); | ||||||
| 	encrypted_buffer.save_to_file(save); | 	encrypted_buffer.save_to_file(save); | ||||||
|   | |||||||
| @@ -1,6 +1,8 @@ | |||||||
| #include "buffer.h" |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <fstream> | #include <fstream> | ||||||
|  | #include <cstring> | ||||||
|  |  | ||||||
|  | #include "buffer.hpp" | ||||||
|  |  | ||||||
| Buffer::Buffer() | Buffer::Buffer() | ||||||
| { | { | ||||||
| @@ -17,16 +19,18 @@ Buffer::Buffer(size_t size) | |||||||
|  |  | ||||||
| Buffer::~Buffer() | Buffer::~Buffer() | ||||||
| { | { | ||||||
| 	if (buffer)	delete[] buffer; | 	if (buffer) | ||||||
|  | 		delete[] buffer; | ||||||
| } | } | ||||||
|  |  | ||||||
| bool Buffer::resize(size_t new_size) | bool Buffer::resize(size_t new_size) | ||||||
| { | { | ||||||
| 	if (size >= new_size) return true; | 	if (size >= new_size) | ||||||
|  | 		return true; | ||||||
| 	uint8_t *new_buffer = (uint8_t *)realloc(buffer, new_size); | 	uint8_t *new_buffer = (uint8_t *)realloc(buffer, new_size); | ||||||
| 	if (!new_buffer) | 	if (!new_buffer) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error resizing buffer\n"); | 		printf("Error resizing buffer\n"); | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 	buffer = new_buffer; | 	buffer = new_buffer; | ||||||
| @@ -37,9 +41,10 @@ bool Buffer::resize(size_t new_size) | |||||||
| int Buffer::add_end(uint8_t *data, size_t data_size) | int Buffer::add_end(uint8_t *data, size_t data_size) | ||||||
| { | { | ||||||
| 	if (taken + data_size > size) | 	if (taken + data_size > size) | ||||||
| 		if (!resize(size + data_size)) return -1; | 		if (!resize(size + data_size)) | ||||||
|  | 			return -1; | ||||||
|  |  | ||||||
| 	memcpy_s(buffer + taken, size - taken, data, data_size); | 	memcpy(buffer + taken, data, data_size); | ||||||
| 	taken += data_size; | 	taken += data_size; | ||||||
| 	return taken - data_size; | 	return taken - data_size; | ||||||
| } | } | ||||||
| @@ -47,9 +52,10 @@ int Buffer::add_end(uint8_t* data, size_t data_size) | |||||||
| int Buffer::add_middle(uint8_t *data, size_t data_size, size_t index) | int Buffer::add_middle(uint8_t *data, size_t data_size, size_t index) | ||||||
| { | { | ||||||
| 	if (taken + data_size > size) | 	if (taken + data_size > size) | ||||||
| 		if (!resize(size + data_size)) return -1; | 		if (!resize(size + data_size)) | ||||||
| 	memmove_s(buffer + index + data_size, size - index - data_size, buffer + index, taken - index); | 			return -1; | ||||||
| 	memcpy_s(buffer + index, size - index, data, data_size); | 	memmove(buffer + index + data_size, buffer + index, taken - index); | ||||||
|  | 	memcpy(buffer + index, data, data_size); | ||||||
| 	taken += data_size; | 	taken += data_size; | ||||||
| 	return index; | 	return index; | ||||||
| } | } | ||||||
| @@ -59,10 +65,10 @@ void Buffer::remove_fast(uint8_t* data, size_t data_size) | |||||||
| 	int64_t index = data - buffer; | 	int64_t index = data - buffer; | ||||||
| 	if (index < 0 || index > taken || index + data_size > taken) | 	if (index < 0 || index > taken || index + data_size > taken) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error removing from buffer\n"); | 		printf("Error removing from buffer\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
| 	memmove_s(buffer + index, size - index, buffer + index + data_size, taken - index - data_size); | 	memmove(buffer + index, buffer + index + data_size, taken - index - data_size); | ||||||
| 	taken -= data_size; | 	taken -= data_size; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -70,10 +76,10 @@ void Buffer::remove(size_t index, size_t data_size) | |||||||
| { | { | ||||||
| 	if (index + data_size > taken) | 	if (index + data_size > taken) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error removing from buffer\n"); | 		printf("Error removing from buffer\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
| 	memmove_s(buffer + index, size - index, buffer + index + data_size, taken - index - data_size); | 	memmove(buffer + index, buffer + index + data_size, taken - index - data_size); | ||||||
| 	taken -= data_size; | 	taken -= data_size; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -91,7 +97,7 @@ void Buffer::remove(uint8_t* data, size_t data_size) | |||||||
| 	size_t index = find(data, data_size); | 	size_t index = find(data, data_size); | ||||||
| 	if (index == -1) | 	if (index == -1) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error removing from buffer\n"); | 		printf("Error removing from buffer\n"); | ||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
| 	remove(index, data_size); | 	remove(index, data_size); | ||||||
| @@ -102,7 +108,7 @@ bool Buffer::save_to_file() | |||||||
| 	std::ofstream file(this->file_path, std::ios::binary | std::ios::out); | 	std::ofstream file(this->file_path, std::ios::binary | std::ios::out); | ||||||
| 	if (!file.is_open()) | 	if (!file.is_open()) | ||||||
| 	{ | 	{ | ||||||
| 		printf_s("Error saving file\n"); | 		printf("Error saving file\n"); | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 	file.write((char *)this->buffer, this->taken); | 	file.write((char *)this->buffer, this->taken); | ||||||
| @@ -119,7 +125,8 @@ bool Buffer::save_to_file(std::string file_path) | |||||||
| bool Buffer::load_from_file() | bool Buffer::load_from_file() | ||||||
| { | { | ||||||
| 	std::ifstream file(this->file_path, std::ios::binary | std::ios::in); | 	std::ifstream file(this->file_path, std::ios::binary | std::ios::in); | ||||||
| 	if (!file.is_open()) return false; | 	if (!file.is_open()) | ||||||
|  | 		return false; | ||||||
|  |  | ||||||
| 	file.seekg(0, std::ios::end); | 	file.seekg(0, std::ios::end); | ||||||
|  |  | ||||||
| @@ -143,4 +150,3 @@ bool Buffer::load_from_file(std::string file_path) | |||||||
| 	this->file_path = file_path; | 	this->file_path = file_path; | ||||||
| 	return load_from_file(); | 	return load_from_file(); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,87 +1,45 @@ | |||||||
| #include <openssl/evp.h> |  | ||||||
| #include <openssl/err.h> |  | ||||||
| #include <openssl/aes.h> |  | ||||||
| #include <string> | #include <string> | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <fstream> | #include <fstream> | ||||||
|  | #include <cstring> | ||||||
|  |  | ||||||
| #include "cryptography.h" | #include "cryptography.hpp" | ||||||
| #include "Buffer.h" | #include "buffer.hpp" | ||||||
|  |  | ||||||
|  |  | ||||||
| Cryptography::Cryptography(const char *password, size_t size) | Cryptography::Cryptography(const char *password, size_t size) | ||||||
| { | { | ||||||
| 	OpenSSL_add_all_algorithms(); | 	// TODO | ||||||
| 	ERR_load_crypto_strings(); |  | ||||||
|  |  | ||||||
| 	if (!generate_key_and_iv_from_password(password, size)) |  | ||||||
| 	{ |  | ||||||
| 		printf_s("Error generating key and IV from password\n"); |  | ||||||
| 		return; |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
|  |  | ||||||
| Cryptography::~Cryptography() | Cryptography::~Cryptography() | ||||||
| { | { | ||||||
| 	ERR_free_strings(); |  | ||||||
| 	EVP_cleanup(); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| bool Cryptography::encrypt(Buffer *plain, Buffer *encrypted) | bool Cryptography::encrypt(Buffer *plain, Buffer *encrypted) | ||||||
| { | { | ||||||
| 	encrypted->resize(plain->taken + AES_BLOCK_SIZE); | 	// TODO | ||||||
|  | 	encrypted->resize(plain->size); | ||||||
| 	ctx = EVP_CIPHER_CTX_new(); | 	memcpy(encrypted->buffer, plain->buffer, plain->taken); | ||||||
| 	if (!ctx) return handleErrors(); |  | ||||||
| 	if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) return handleErrors(); |  | ||||||
|  |  | ||||||
| 	if (1 != EVP_EncryptUpdate(ctx, encrypted->buffer, (int*)&encrypted->taken, plain->buffer, plain->taken)) return handleErrors(); |  | ||||||
| 	int final_len; |  | ||||||
| 	if (1 != EVP_EncryptFinal_ex(ctx, encrypted->buffer + encrypted->taken, &final_len)) return handleErrors(); |  | ||||||
| 	encrypted->taken += final_len; |  | ||||||
|  |  | ||||||
| 	EVP_CIPHER_CTX_free(ctx); |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
|  |  | ||||||
| bool Cryptography::decrypt(Buffer *encrypted, Buffer *decrypted) | bool Cryptography::decrypt(Buffer *encrypted, Buffer *decrypted) | ||||||
| { | { | ||||||
| 	decrypted->resize(encrypted->taken + AES_BLOCK_SIZE); | 	// TODO | ||||||
|  | 	decrypted->resize(encrypted->size); | ||||||
|  | 	memcpy(encrypted->buffer, decrypted->buffer, decrypted->taken); | ||||||
| 	ctx = EVP_CIPHER_CTX_new(); |  | ||||||
| 	if (!ctx) return handleErrors(); |  | ||||||
| 	if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) return handleErrors(); |  | ||||||
|  |  | ||||||
| 	if (1 != EVP_DecryptUpdate(ctx, decrypted->buffer, (int*)&decrypted->taken, encrypted->buffer, encrypted->taken)) return handleErrors(); |  | ||||||
| 	int final_len; |  | ||||||
| 	if (1 != EVP_DecryptFinal_ex(ctx, decrypted->buffer + decrypted->taken, &final_len)) return handleErrors(); |  | ||||||
| 	decrypted->taken += final_len; |  | ||||||
|  |  | ||||||
| 	EVP_CIPHER_CTX_free(ctx); |  | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
|  |  | ||||||
| bool Cryptography::generate_key_and_iv_from_password(const char *password, size_t size) | bool Cryptography::generate_key_and_iv_from_password(const char *password, size_t size) | ||||||
| { | { | ||||||
| 	int iterations = 10000; | 	// TODO | ||||||
|  |  | ||||||
| 	// Derive key and IV using PBKDF2 |  | ||||||
| 	if (1 != PKCS5_PBKDF2_HMAC(password, size, nullptr, 0, iterations, EVP_sha256(), 32, key)) return false; |  | ||||||
| 	if (1 != PKCS5_PBKDF2_HMAC(password, size, nullptr, 0, iterations, EVP_sha256(), 16, iv)) return false; |  | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
|  |  | ||||||
| bool Cryptography::handleErrors() | bool Cryptography::handleErrors() | ||||||
| { | { | ||||||
| 	EVP_CIPHER_CTX_free(ctx); | 	// TODO | ||||||
|  |  | ||||||
| 	while (auto error = ERR_get_error()) |  | ||||||
| 	{ |  | ||||||
| 		char* error_string = ERR_error_string(error, nullptr); |  | ||||||
| 		printf_s("%s\n", error_string); |  | ||||||
| 	} |  | ||||||
| 	return false; | 	return false; | ||||||
| } | } | ||||||
| @@ -1,7 +1,9 @@ | |||||||
| #include <ctime> | #include <ctime> | ||||||
| #include "func.h" | #include <cstring> | ||||||
| #include "glob.h" |  | ||||||
| #include "Buffer.h" | #include "func.hpp" | ||||||
|  | #include "glob.hpp" | ||||||
|  | #include "buffer.hpp" | ||||||
|  |  | ||||||
| int find_logininfo_in_buffer(Buffer &buffer, const char *label) | int find_logininfo_in_buffer(Buffer &buffer, const char *label) | ||||||
| { | { | ||||||
| @@ -57,7 +59,6 @@ void add_logininfo_to_buffer(Buffer& buffer, const char* label, const char* user | |||||||
| 	index->offset += sizeof(LoginInfo); | 	index->offset += sizeof(LoginInfo); | ||||||
|  |  | ||||||
| 	buffer.add_middle((uint8_t *)&pass, sizeof(LoginInfo), index->offset - sizeof(LoginInfo)); | 	buffer.add_middle((uint8_t *)&pass, sizeof(LoginInfo), index->offset - sizeof(LoginInfo)); | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| LoginInfoPointer get_logininfo_pointer_from_buffer(Buffer &buffer, int index_of_pass) | LoginInfoPointer get_logininfo_pointer_from_buffer(Buffer &buffer, int index_of_pass) | ||||||
|   | |||||||
| @@ -1,50 +1,64 @@ | |||||||
| #include "glob.h" | #include "glob.hpp" | ||||||
|  |  | ||||||
| Glob_Result glob(const char *pattern, const char *text) | Glob_Result glob(const char *pattern, const char *text) | ||||||
| { | { | ||||||
| 	while (*pattern != '\0' && *text != '\0') { | 	while (*pattern != '\0' && *text != '\0') | ||||||
| 		switch (*pattern) { | 	{ | ||||||
| 		case '?': { | 		switch (*pattern) | ||||||
|  | 		{ | ||||||
|  | 		case '?': | ||||||
|  | 		{ | ||||||
| 			pattern += 1; | 			pattern += 1; | ||||||
| 			text += 1; | 			text += 1; | ||||||
| 		} | 		} | ||||||
| 		break; | 		break; | ||||||
|  |  | ||||||
| 		case '*': { | 		case '*': | ||||||
|  | 		{ | ||||||
| 			Glob_Result result = glob(pattern + 1, text); | 			Glob_Result result = glob(pattern + 1, text); | ||||||
| 			if (result != Glob_Result::UNMATCHED) return result; | 			if (result != Glob_Result::UNMATCHED) | ||||||
|  | 				return result; | ||||||
| 			text += 1; | 			text += 1; | ||||||
| 		} | 		} | ||||||
| 		break; | 		break; | ||||||
|  |  | ||||||
| 		case '[': { | 		case '[': | ||||||
|  | 		{ | ||||||
| 			bool matched = false; | 			bool matched = false; | ||||||
| 			bool negate = false; | 			bool negate = false; | ||||||
|  |  | ||||||
| 			pattern += 1; // skipping [ | 			pattern += 1; // skipping [ | ||||||
| 			if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unclosed [ | 			if (*pattern == '\0') | ||||||
|  | 				return Glob_Result::SYNTAX_ERROR; // unclosed [ | ||||||
|  |  | ||||||
| 			if (*pattern == '!') { | 			if (*pattern == '!') | ||||||
|  | 			{ | ||||||
| 				negate = true; | 				negate = true; | ||||||
| 				pattern += 1; | 				pattern += 1; | ||||||
| 				if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unclosed [ | 				if (*pattern == '\0') | ||||||
|  | 					return Glob_Result::SYNTAX_ERROR; // unclosed [ | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			char prev = *pattern; | 			char prev = *pattern; | ||||||
| 			matched |= prev == *text; | 			matched |= prev == *text; | ||||||
| 			pattern += 1; | 			pattern += 1; | ||||||
|  |  | ||||||
| 			while (*pattern != ']' && *pattern != '\0') { | 			while (*pattern != ']' && *pattern != '\0') | ||||||
| 				switch (*pattern) { | 			{ | ||||||
| 				case '-': { | 				switch (*pattern) | ||||||
|  | 				{ | ||||||
|  | 				case '-': | ||||||
|  | 				{ | ||||||
| 					pattern += 1; | 					pattern += 1; | ||||||
| 					switch (*pattern) { | 					switch (*pattern) | ||||||
|  | 					{ | ||||||
| 					case ']': | 					case ']': | ||||||
| 						matched |= '-' == *text; | 						matched |= '-' == *text; | ||||||
| 						break; | 						break; | ||||||
| 					case '\0': | 					case '\0': | ||||||
| 						return Glob_Result::SYNTAX_ERROR; // unclosed [ | 						return Glob_Result::SYNTAX_ERROR; // unclosed [ | ||||||
| 					default: { | 					default: | ||||||
|  | 					{ | ||||||
| 						matched |= prev <= *text && *text <= *pattern; | 						matched |= prev <= *text && *text <= *pattern; | ||||||
| 						prev = *pattern; | 						prev = *pattern; | ||||||
| 						pattern += 1; | 						pattern += 1; | ||||||
| @@ -52,7 +66,8 @@ Glob_Result glob(const char* pattern, const char* text) | |||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
| 				default: { | 				default: | ||||||
|  | 				{ | ||||||
| 					prev = *pattern; | 					prev = *pattern; | ||||||
| 					matched |= prev == *text; | 					matched |= prev == *text; | ||||||
| 					pattern += 1; | 					pattern += 1; | ||||||
| @@ -60,9 +75,12 @@ Glob_Result glob(const char* pattern, const char* text) | |||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if (*pattern != ']') return Glob_Result::SYNTAX_ERROR; // unclosed [ | 			if (*pattern != ']') | ||||||
| 			if (negate) matched = !matched; | 				return Glob_Result::SYNTAX_ERROR; // unclosed [ | ||||||
| 			if (!matched) return Glob_Result::UNMATCHED; | 			if (negate) | ||||||
|  | 				matched = !matched; | ||||||
|  | 			if (!matched) | ||||||
|  | 				return Glob_Result::UNMATCHED; | ||||||
|  |  | ||||||
| 			pattern += 1; | 			pattern += 1; | ||||||
| 			text += 1; | 			text += 1; | ||||||
| @@ -71,23 +89,30 @@ Glob_Result glob(const char* pattern, const char* text) | |||||||
|  |  | ||||||
| 		case '\\': | 		case '\\': | ||||||
| 			pattern += 1; | 			pattern += 1; | ||||||
| 			if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unfinished escape | 			if (*pattern == '\0') | ||||||
|  | 				return Glob_Result::SYNTAX_ERROR; // unfinished escape | ||||||
| 																					// fallthrough | 																					// fallthrough | ||||||
| 		default: { | 		default: | ||||||
| 			if (*pattern == *text) { | 		{ | ||||||
|  | 			if (*pattern == *text) | ||||||
|  | 			{ | ||||||
| 				pattern += 1; | 				pattern += 1; | ||||||
| 				text += 1; | 				text += 1; | ||||||
| 			} | 			} | ||||||
| 			else { | 			else | ||||||
|  | 			{ | ||||||
| 				return Glob_Result::UNMATCHED; | 				return Glob_Result::UNMATCHED; | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (*text == '\0') { | 	if (*text == '\0') | ||||||
| 		while (*pattern == '*') pattern += 1; | 	{ | ||||||
| 		if (*pattern == '\0') return Glob_Result::MATCHED; | 		while (*pattern == '*') | ||||||
|  | 			pattern += 1; | ||||||
|  | 		if (*pattern == '\0') | ||||||
|  | 			return Glob_Result::MATCHED; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return Glob_Result::UNMATCHED; | 	return Glob_Result::UNMATCHED; | ||||||
|   | |||||||
| @@ -1,31 +1,40 @@ | |||||||
| #include <Windows.h> |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <string> | #include <string> | ||||||
| #include <shlobj.h> |  | ||||||
| #include "win.h" |  | ||||||
| #include "func.h" |  | ||||||
| 
 | 
 | ||||||
| bool put_data_on_clipboard(const char* text) { | #include "sys.hpp" | ||||||
|  | #include "func.hpp" | ||||||
|  | 
 | ||||||
|  | #ifdef _WIN32 | ||||||
|  | 
 | ||||||
|  | #include <shlobj.h> | ||||||
|  | #include <Windows.h> | ||||||
|  | 
 | ||||||
|  | bool put_data_on_clipboard(const char *text) | ||||||
|  | { | ||||||
| 	size_t len = strlen(text); | 	size_t len = strlen(text); | ||||||
| 	if (len == 0) { | 	if (len == 0) | ||||||
|  | 	{ | ||||||
| 		printf_s("Text is empty\n"); | 		printf_s("Text is empty\n"); | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 	// Open the clipboard
 | 	// Open the clipboard
 | ||||||
| 	if (!OpenClipboard(nullptr)) { | 	if (!OpenClipboard(nullptr)) | ||||||
|  | 	{ | ||||||
| 		printf_s("Failed to open clipboard"); | 		printf_s("Failed to open clipboard"); | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 	// Allocate global memory for the text
 | 	// Allocate global memory for the text
 | ||||||
| 	HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, strlen(text) + 1); | 	HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, strlen(text) + 1); | ||||||
| 	if (!hMem) { | 	if (!hMem) | ||||||
|  | 	{ | ||||||
| 		CloseClipboard(); | 		CloseClipboard(); | ||||||
| 		printf_s("Failed to allocate memory for text"); | 		printf_s("Failed to allocate memory for text"); | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 	// Copy the text to the allocated memory
 | 	// Copy the text to the allocated memory
 | ||||||
| 	char *memData = static_cast<char *>(GlobalLock(hMem)); | 	char *memData = static_cast<char *>(GlobalLock(hMem)); | ||||||
| 	if (!memData) { | 	if (!memData) | ||||||
|  | 	{ | ||||||
| 		CloseClipboard(); | 		CloseClipboard(); | ||||||
| 		printf_s("Failed to lock memory for text"); | 		printf_s("Failed to lock memory for text"); | ||||||
| 		return false; | 		return false; | ||||||
| @@ -40,7 +49,6 @@ bool put_data_on_clipboard(const char* text) { | |||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| std::string get_user_password() | std::string get_user_password() | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
| @@ -75,7 +83,8 @@ std::optional<std::string> get_save_path() | |||||||
| 
 | 
 | ||||||
| 	HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &path); | 	HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &path); | ||||||
| 
 | 
 | ||||||
| 	if (!SUCCEEDED(result)) { | 	if (!SUCCEEDED(result)) | ||||||
|  | 	{ | ||||||
| 		CoTaskMemFree(path); // Free the allocated memory
 | 		CoTaskMemFree(path); // Free the allocated memory
 | ||||||
| 		return {}; | 		return {}; | ||||||
| 	} | 	} | ||||||
| @@ -90,14 +99,15 @@ std::optional<std::string> get_save_path() | |||||||
| 	// check if it exists
 | 	// check if it exists
 | ||||||
| 	if (!(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY))) | 	if (!(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY))) | ||||||
| 	{ | 	{ | ||||||
| 		if (!CreateDirectory(userDirectory.c_str(), nullptr)) return {}; | 		if (!CreateDirectory(userDirectory.c_str(), nullptr)) | ||||||
|  | 			return {}; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| 	userDirectory.append(L"\\save.txt"); | 	userDirectory.append(L"\\save.txt"); | ||||||
| 
 | 
 | ||||||
| 	int size = WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, nullptr, 0, nullptr, nullptr); | 	int size = WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, nullptr, 0, nullptr, nullptr); | ||||||
| 	if (size > 0) { | 	if (size > 0) | ||||||
|  | 	{ | ||||||
| 		std::string str(size, 0); | 		std::string str(size, 0); | ||||||
| 		WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, &str[0], size, nullptr, nullptr); | 		WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, &str[0], size, nullptr, nullptr); | ||||||
| 
 | 
 | ||||||
| @@ -106,3 +116,25 @@ std::optional<std::string> get_save_path() | |||||||
| 
 | 
 | ||||||
| 	return {}; | 	return {}; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | #else | ||||||
|  | 
 | ||||||
|  | bool put_data_on_clipboard(const char *text) | ||||||
|  | { | ||||||
|  | 	// TODO
 | ||||||
|  | 	return true; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | std::string get_user_password() | ||||||
|  | { | ||||||
|  | 	// TODO
 | ||||||
|  | 	return "123"; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | std::optional<std::string> get_save_path() | ||||||
|  | { | ||||||
|  | 	// TODO
 | ||||||
|  | 	return "save.bin"; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #endif | ||||||
		Reference in New Issue
	
	Block a user
	 Nikola Petrov
					Nikola Petrov