Skip to content

Commit a32a8c6

Browse files
committed
Added SHA-512 hash generation function for middleware.
1 parent 2e2e929 commit a32a8c6

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

controller/account.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131

3232
include_once("db_config.php");
33+
include_once("hash_helper.php");
3334
include_once("session_ctrl.php");
3435

3536
global $db_conn;
@@ -80,7 +81,7 @@ public static function create($name, $username, $email, $password) {
8081
$db_conn,
8182
"INSERT INTO accounts (name, username, email, password) ".
8283
"VALUES (\"".$name."\", \"".$username."\", \"".$email.
83-
"\", \"".md5($password)."\")"
84+
"\", \"".hashString($password)."\")"
8485
);
8586

8687
$status = $result ?
@@ -97,8 +98,8 @@ public static function update($username, $name, $email, $password, $old) {
9798

9899
global $db_conn;
99100
$res = mysqli_query($db_conn, "UPDATE accounts SET name=\"".$name.
100-
"\", email=\"".$email."\", password=\"".md5($password)."\" WHERE username=\"".
101-
$username."\" AND password=\"".md5($old)."\" AND id=".(SessionControl::getId()));
101+
"\", email=\"".$email."\", password=\"".hashString($password)."\" WHERE username=\"".
102+
$username."\" AND password=\"".hashString($old)."\" AND id=".(SessionControl::getId()));
102103

103104
$result = !(!$res);
104105
freeDBQuery($res);
@@ -114,7 +115,7 @@ public static function login($username, $password, $createSession = true) {
114115
$result = mysqli_query(
115116
$db_conn,
116117
"SELECT id FROM accounts WHERE ".
117-
"username=\"".$username."\" AND password=\"".md5($password)."\""
118+
"username=\"".$username."\" AND password=\"".hashString($password)."\""
118119
);
119120

120121
if(!$result || mysqli_num_rows($result) == 0)

controller/apps.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
include_once("account.php");
3333
include_once("db_config.php");
34+
include_once("hash_helper.php");
3435
include_once("session_ctrl.php");
3536
include_once("shell.php");
3637
include_once("util.php");
@@ -67,8 +68,8 @@ public static function create($name, $description) {
6768
freeDBQuery($check);
6869
}
6970

70-
$id_hash = sha1(md5($name));
71-
$app_key = "qba_" . substr_replace($id_hash, '', 10) . "_" . substr(md5($id_hash), 24);
71+
$id_hash = sha1(hashString($name));
72+
$app_key = "qba_" . substr_replace($id_hash, '', 10) . "_" . substr(hashString($id_hash), 120);
7273
$res = mysqli_query(
7374
$db_conn,
7475
"INSERT INTO app (creator_id, app_id, app_key, name, description) VALUES(".

controller/hash_helper.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of QLBase (https://github.com/nthnn/QLBase).
5+
* Copyright 2024 - Nathanne Isip
6+
*
7+
* Permission is hereby granted, free of charge,
8+
* to any person obtaining a copy of this software
9+
* and associated documentation files (the “Software”),
10+
* to deal in the Software without restriction,
11+
* including without limitation the rights to use, copy,
12+
* modify, merge, publish, distribute, sublicense, and/or
13+
* sell copies of the Software, and to permit persons to
14+
* whom the Software is furnished to do so, subject to
15+
* the following conditions:
16+
*
17+
* The above copyright notice and this permission notice
18+
* shall be included in all copies or substantial portions
19+
* of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF
22+
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
23+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
24+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
25+
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
26+
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
29+
* OR OTHER DEALINGS IN THE SOFTWARE.
30+
*/
31+
32+
function hashString($str) {
33+
return hash('sha512', $str);
34+
}
35+
36+
?>

controller/session_ctrl.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131

3232
include_once("db_config.php");
33+
include_once("hash_helper.php");
3334
include_once("util.php");
3435

3536
global $db_conn;
@@ -65,7 +66,7 @@ public static function validate($hash) {
6566
public static function create($user_id) {
6667
global $db_conn;
6768

68-
$hash = md5(Util::guidv4(null));
69+
$hash = hashString(Util::guidv4(null));
6970
$result = mysqli_query(
7071
$db_conn,
7172
"INSERT INTO sessions(user_id, hash, user_agent, remote_addr) ".

controller/validator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function name($name) {
4949
}
5050

5151
public static function loginPassword($password) {
52-
return strlen($password) === 32 &&
52+
return strlen($password) === 128 &&
5353
preg_match("/^[a-f0-9]+$/", $password);
5454
}
5555

0 commit comments

Comments
 (0)