hashing password

This commit is contained in:
wt
2024-12-04 10:14:10 +07:00
parent 1d7b443697
commit 5432e18a77
2 changed files with 13 additions and 2 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ void auth(const httplib::Request &request, httplib::Response &response) {
std::string userid;
for (nlohmann::basic_json<> user: all_users) {
if ((user["username"] == request_username) && (user["password"] == hashPassword(request_password))) {
if ((user["username"] == request_username) && (user["password"] == request_password)) {
userid = user["id"];
break;
}
+12 -1
View File
@@ -11,9 +11,20 @@ if (localStorage.getItem("token") === null) {
loginformbody.addEventListener('submit', (e) => {
e.preventDefault()
login(loginforminputusername.value, loginforminputpassword.value)
hashPassword(loginforminputpassword.value).then(hashedpassword => {
login(loginforminputusername.value, hashedpassword)
})
})
const hashPassword = async (password) => {
const encoder = new TextEncoder()
const data = encoder.encode(password)
const hashBuffer = await crypto.subtle.digest('SHA-256', data)
const hashArray = Array.from(new Uint8Array(hashBuffer))
const hashHex = hashArray.map(b=>('00' + b.toString(16)).slice(-2)).join('')
return hashHex
}
const login = async (username, password) => {
const response = await fetch(routes.login(), {
method: 'POST',