kukkubukku/data/users.sh

39 lines
848 B
Bash
Raw Permalink Normal View History

#!/bin/bash
createSalt() {
head -c 12 /dev/urandom | base64
}
hashPassword() {
2022-03-30 18:41:59 +00:00
_password="$1"
_salt="$2"
echo "$_password$_salt" | sha512sum | cut -d' ' -f1
}
createUser() {
2022-03-30 18:41:59 +00:00
local username="$1"
local password="$2"
local salt="$(createSalt)"
local password="$(hashPassword "$password" "$salt")"
echo "INSERT INTO users (username, password, salt) VALUES (
'$(escape "$username")',
'$(escape "$password")',
'$(escape "$salt")'
)" | execute
}
loginUser() {
2022-03-30 18:41:59 +00:00
local username="$1"
local password="$2"
2022-03-30 18:41:59 +00:00
local result="$(echo "SELECT password, salt FROM users WHERE username='$(escape "$username")'" | query)"
local hash="$(echo "$result" | getColumns 1)"
local salt="$(echo "$result" | getColumns 2)"
2022-03-30 18:41:59 +00:00
local password="$(hashPassword "$password" "$salt")"
# return true if password is correct
2022-03-30 18:41:59 +00:00
test "$password" = "$hash"
return
}