Implemented basic hashing

This commit is contained in:
Eduard Urbach 2024-01-30 12:50:47 +01:00
parent 9c3d12bb50
commit 40070557b7
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
2 changed files with 10 additions and 2 deletions

View File

@ -22,7 +22,7 @@ func send_login():
if is_logged_in(): if is_logged_in():
return return
var password := "password" var password := "password".sha256_text()
var buffer := StreamPeerBuffer.new() var buffer := StreamPeerBuffer.new()
buffer.put_8(Packet.LOGIN) buffer.put_8(Packet.LOGIN)
buffer.put_data(JSON.stringify([Global.username, password]).to_utf8_buffer()) buffer.put_data(JSON.stringify([Global.username, password]).to_utf8_buffer())

View File

@ -2,7 +2,9 @@ package game
import ( import (
"crypto/rand" "crypto/rand"
"crypto/sha256"
"encoding/base64" "encoding/base64"
"encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"net" "net"
@ -17,6 +19,7 @@ var (
ErrAlreadyLoggedIn = errors.New("already logged in") ErrAlreadyLoggedIn = errors.New("already logged in")
ErrUnknownAccount = errors.New("unknown account") ErrUnknownAccount = errors.New("unknown account")
ErrWrongPassword = errors.New("wrong password") ErrWrongPassword = errors.New("wrong password")
testPassword = sha256Text("password")
) )
// Login checks the account credentials and gives a network peer access to an account. // Login checks the account credentials and gives a network peer access to an account.
@ -42,7 +45,7 @@ func (game *Game) Login(data []byte, address *net.UDPAddr) error {
return ErrUnknownAccount return ErrUnknownAccount
} }
if password != "password" { if password != testPassword {
game.server.Send(Login, []byte{Failure}, address) game.server.Send(Login, []byte{Failure}, address)
return ErrWrongPassword return ErrWrongPassword
} }
@ -91,3 +94,8 @@ func createAuthToken() string {
rand.Read(randomBytes) rand.Read(randomBytes)
return base64.StdEncoding.EncodeToString(randomBytes) return base64.StdEncoding.EncodeToString(randomBytes)
} }
func sha256Text(password string) string {
sum := sha256.Sum256([]byte(password))
return hex.EncodeToString(sum[:])
}