Răsfoiți Sursa

feat: 添加随机生成密码

SongZihuan 3 ani în urmă
părinte
comite
1ade8f5e74
4 a modificat fișierele cu 50 adăugiri și 10 ștergeri
  1. 1 1
      CMakeLists.txt
  2. 17 9
      main.c
  3. 4 0
      main.h
  4. 28 0
      random_passwd.c

+ 1 - 1
CMakeLists.txt

@@ -4,4 +4,4 @@ project(H_Passwd C)
 set(CMAKE_C_STANDARD 11)
 
 add_definitions(-DVERSION="0.0.1" -DVERSION_INFO="H-Password, now start.")
-add_executable(H_Passwd main.c base64.c passwd.c argument.c)
+add_executable(H_Passwd main.c base64.c passwd.c argument.c random_passwd.c)

+ 17 - 9
main.c

@@ -24,6 +24,7 @@ bool getPassWd(void);
 
 int main(int argc, char **argv) {
     name = argv[0];
+    randomInit();
     initOpt(true, argc, argv, arg);
 
     for (getOpt(); opt_flat != 0; getOpt()) {
@@ -127,7 +128,7 @@ void printHelp(void) {
 
 }
 
-#define READ_WORD(key, len, key_name, re) do { printf("Please Enter The " key_name ":\n"); \
+#define READ_WORD(key, len, key_name, re) do { printf("Please Enter The " key_name ":"); \
     (key) = calloc((len) + 10, sizeof(char )); \
     fgets((key), (len) + 10, stdin); \
     if (strchr((key), '\n') == NULL) { \
@@ -137,13 +138,20 @@ void printHelp(void) {
     }while(0)
 
 bool setPassWd(void) {
-    char *account;
-    char *passwd;
-    char *note;
-    char *passwd_str;
+    char *account = NULL;
+    char *passwd = NULL;
+    char *note = NULL;
+    char *passwd_str = NULL;
 
     READ_WORD(account, 100, "Your account", ERROR1);
     READ_WORD(passwd, 100, "Your password", ERROR2);
+
+    if (*passwd == 0) {  // 未输入密码内容
+        free(passwd);
+        passwd = randomPasswd();  // 随机密码
+        printf("random password: '%s'\n", passwd);
+    }
+
     READ_WORD(note, 100, "Your note", ERROR3);
 
     passwd_str = makePasswordString(account, passwd, note);
@@ -164,10 +172,10 @@ bool setPassWd(void) {
 }
 
 bool getPassWd(void) {
-    char *account;
-    char *passwd;
-    char *note;
-    char *passwd_str;
+    char *account = NULL;
+    char *passwd = NULL;
+    char *note = NULL;
+    char *passwd_str = NULL;
 
     READ_WORD(passwd_str, 200, "Your Label", ERROR1);
 

+ 4 - 0
main.h

@@ -27,4 +27,8 @@ bool getInfoFromPasswordString(char *passwd_base64, char **account, char **passw
 void printInfo(char *account, char *passwd, char *note);
 void printPasswdStr(char *account, char *passwd, char *note, char *passwd_str);
 
+void randomInit(void);
+unsigned long long getRandom(int min, int max);
+char *randomPasswd(void);
+
 #endif //H_PASSWD_MAIN_H

+ 28 - 0
random_passwd.c

@@ -0,0 +1,28 @@
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+static char *passwd_char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@#$%^&*(){[}]:;\"'<,>.";
+static unsigned long long seed = 2;
+static unsigned long long a = 25214903917;
+static unsigned long long b = 11;
+static unsigned long long c = ((unsigned long long)2) << 48;
+
+void randomInit(void) {
+    seed = (long long)time(NULL);
+}
+
+unsigned long long getRandom(int min, int max) {
+    seed = (a * seed + b) % c;
+    return min + (seed % (max - min));
+}
+
+char *randomPasswd(void) {
+    size_t size = getRandom(12, 17);
+    char *passwd = calloc(size + 1, sizeof(char));
+
+    for (int i = 0; i < size; i++)
+        passwd[i] = passwd_char[getRandom(0, (int)strlen(passwd_char))];
+
+    return passwd;
+}