1111
1212#include <commands/login.h>
1313
14- char usernames_total [MAX_USERS_ALLOWED ][ MAX_USERNAME_LENGTH ];
15- char passwords_total [MAX_USERS_ALLOWED ][ MAX_PASSWORD_LENGTH ];
14+ int64 usernames_total [MAX_USERS_ALLOWED ];
15+ int64 passwords_total [MAX_USERS_ALLOWED ];
1616int users_index = 0 ;
1717
18- void create_user (char * name , char * password ){
18+ void create_user (int64 name_hash , int64 password_hash ){
1919 if (users_index >= MAX_USERS_ALLOWED ){
2020 error ("Too many user accounts!" , __FILE__ );
2121 return ;
2222 }
23-
24-
25- for (int i = 0 ; i < MAX_USERS_ALLOWED ; i ++ ){
26- char * current_username = usernames_total [i ];
2723
28- if (strcmp (current_username , name ) == 0 ){
24+ for (int i = 0 ; i < users_index ; i ++ ){ // only check existing users
25+ int64 current_username = usernames_total [i ];
26+ if (current_username == name_hash ){
2927 error ("Already have an user with same name!" , __FILE__ );
3028 return ;
3129 }
3230 }
3331
34- strcpy ( usernames_total [users_index ], name ) ;
35- strcpy ( passwords_total [users_index ], password ) ;
32+ usernames_total [users_index ] = name_hash ;
33+ passwords_total [users_index ] = password_hash ;
3634 users_index ++ ;
37- printf ("Created a user named \'%s\'" , name );
35+ printf ("Created a user. id → 0x%X" , (unsigned long long )name_hash );
36+ }
37+
38+
39+ void create_user_str (cstring name , cstring password ){
40+ int64 name_hash = baranium_hash (name );
41+ int64 password_hash = baranium_hash (password );
42+
43+ create_user (name_hash , password_hash );
3844}
3945
4046char * login_request (){
@@ -93,13 +99,16 @@ char* login_request(){
9399 password [i ] = '\0' ;
94100
95101 __putc ('\n' );
102+
103+ // Start hashing
104+ int64 username_hash = baranium_hash (username );
105+ int64 password_hash = baranium_hash (password );
96106
97107 for (int i = 0 ; i < users_index ; i ++ ){
98- char * current_username = usernames_total [i ];
99- char * current_password = passwords_total [i ];
100-
108+ int64 current_username = usernames_total [i ];
109+ int64 current_password = passwords_total [i ];
101110
102- if (strcmp ( current_username , & username [ 0 ]) == 0 && strcmp ( current_password , password ) == 0 ){
111+ if (current_username == username_hash && current_password == password_hash ){
103112 return & username [0 ];
104113 }
105114 }
@@ -112,6 +121,8 @@ int ask_password(const char* username){
112121 char temp ;
113122 int i ;
114123
124+ int64 username_hash = baranium_hash (username );
125+
115126 static char password [21 ];
116127
117128 print ("Password: " );
@@ -136,12 +147,14 @@ int ask_password(const char* username){
136147 password [i ] = '\0' ;
137148 putc ('\n' );
138149
150+ int64 password_hash = baranium_hash (password );
151+
139152 for (int i = 0 ; i < users_index ; i ++ ){
140- char * current_username = usernames_total [i ];
141- char * current_password = passwords_total [i ];
153+ int64 current_username = usernames_total [i ];
154+ int64 current_password = passwords_total [i ];
142155
143156
144- if (strcmp ( current_username , username ) == 0 && strcmp ( current_password , password ) == 0 ){
157+ if (current_username == username_hash && current_password == password_hash ){
145158 strcpy (password , "" );
146159 return 0 ;
147160 }
0 commit comments