mirror of
https://github.com/sigmasternchen/kukkubukku
synced 2025-03-15 07:18:54 +00:00
added basic data layer functions for users
This commit is contained in:
parent
7fcc9b539f
commit
4215c2c1d9
1 changed files with 37 additions and 0 deletions
37
data/users.sh
Normal file
37
data/users.sh
Normal file
|
@ -0,0 +1,37 @@
|
|||
#!/bin/bash
|
||||
|
||||
createSalt() {
|
||||
head -c 12 /dev/urandom | base64
|
||||
}
|
||||
|
||||
hashPassword() {
|
||||
password="$1"
|
||||
salt="$2"
|
||||
echo "$password$salt" | sha512sum | cut -d' ' -f1
|
||||
}
|
||||
|
||||
createUser() {
|
||||
username="$1"
|
||||
password="$2"
|
||||
salt="$(createSalt)"
|
||||
password="$(hashPassword "$password" "$salt")"
|
||||
echo "INSERT INTO users (username, password, salt) VALUES (
|
||||
'$(escape "$username")',
|
||||
'$(escape "$password")',
|
||||
'$(escape "$salt")'
|
||||
)" | execute
|
||||
}
|
||||
|
||||
loginUser() {
|
||||
username="$1"
|
||||
password="$2"
|
||||
|
||||
result="$(echo "SELECT password, salt FROM users WHERE username='$(escape "$username")'" | query)"
|
||||
hash="$(echo "$result" | getColumns 1)"
|
||||
salt="$(echo "$result" | getColumns 2)"
|
||||
|
||||
password="$(hashPassword "$password" "$salt")"
|
||||
|
||||
test "$password" = "$hash"
|
||||
return
|
||||
}
|
Loading…
Reference in a new issue