Skip to content

Commit 975faef

Browse files
Implemented hashing for passwords, temporarly stoped to enter userland.
1 parent 63601e0 commit 975faef

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

source/includes/commands/login.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,40 @@
1010
*/
1111

1212
#include <basics.h>
13+
#include <algorithms/hashing.h>
1314

1415
#define MAX_USERS_ALLOWED 7
1516
#define MAX_USERNAME_LENGTH 20
1617
#define MAX_PASSWORD_LENGTH 40
1718

1819
/**
19-
* @brief Function to create an user.
20+
* @brief Function to create an user with an hash.
2021
*
2122
* @param name
2223
* @param password
2324
*/
24-
void create_user(char* name, char* password);
25+
void create_user(int64 name, int64 password);
26+
27+
/**
28+
* @brief Function to create an user with plain string.
29+
* @warning Potential security risk.
30+
*
31+
* @param name
32+
* @param password
33+
*/
34+
void create_user_str(cstring name, cstring password);
2535

2636
/**
2737
* @brief Sends a login request
2838
*
2939
* @return char* Returns the username if the login was successful.
3040
*/
31-
char* login_request();
41+
char* login_request();
42+
43+
/**
44+
* @brief Requests password for verification for sudo.
45+
*
46+
* @param username current username
47+
* @return Whether it was successful.
48+
*/
49+
int ask_password(const char* username);

source/kernel/C/kernel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ void main(void) {
320320

321321
mm_print_out();
322322

323-
create_user("root", "prad");
323+
create_user_str("root", "prad");
324324

325325
enable_fpu();
326326

327-
enter_userland();
327+
// enter_userland();
328328

329329
info("Welcome to FrostWing Operating System!", "(https://github.com/Frost-Wing)");
330330

source/kernel/C/shell/commands/login.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,36 @@
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];
1616
int 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

4046
char* 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

Comments
 (0)