From 2d5f777efd684025e4d236722f31d6b700b38425 Mon Sep 17 00:00:00 2001 From: maru Date: Sun, 14 Apr 2024 19:05:04 -0400 Subject: [PATCH] Move argon2 functions to its own file --- api/account.go | 17 ++++++----------- api/argon2.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 api/argon2.go diff --git a/api/account.go b/api/account.go index c060bb2..86fbe3c 100644 --- a/api/account.go +++ b/api/account.go @@ -12,16 +12,11 @@ import ( "time" "github.com/Flashfyre/pokerogue-server/db" - "golang.org/x/crypto/argon2" ) const ( - UUIDSize = 16 - ArgonTime = 1 - ArgonMemory = 256 * 1024 - ArgonThreads = 4 - ArgonKeySize = 32 - ArgonSaltSize = 16 + UUIDSize = 16 + TokenSize = 32 ) var isValidUsername = regexp.MustCompile(`^\w{1,16}$`).MatchString @@ -79,7 +74,7 @@ func handleAccountRegister(request AccountRegisterRequest) error { return fmt.Errorf(fmt.Sprintf("failed to generate salt: %s", err)) } - err = db.AddAccountRecord(uuid, request.Username, argon2.IDKey([]byte(request.Password), salt, ArgonTime, ArgonMemory, ArgonThreads, ArgonKeySize), salt) + err = db.AddAccountRecord(uuid, request.Username, deriveArgon2IDKey([]byte(request.Password), salt), salt) if err != nil { return fmt.Errorf("failed to add account record: %s", err) } @@ -109,11 +104,11 @@ func handleAccountLogin(request AccountLoginRequest) (AccountLoginResponse, erro return AccountLoginResponse{}, err } - if !bytes.Equal(key, argon2.IDKey([]byte(request.Password), salt, ArgonTime, ArgonMemory, ArgonThreads, ArgonKeySize)) { + if !bytes.Equal(key, deriveArgon2IDKey([]byte(request.Password), salt)) { return AccountLoginResponse{}, fmt.Errorf("password doesn't match") } - token := make([]byte, 32) + token := make([]byte, TokenSize) _, err = rand.Read(token) if err != nil { return AccountLoginResponse{}, fmt.Errorf("failed to generate token: %s", err) @@ -129,7 +124,7 @@ func handleAccountLogin(request AccountLoginRequest) (AccountLoginResponse, erro // /account/logout - log out of account func handleAccountLogout(token []byte) error { - if len(token) != 32 { + if len(token) != TokenSize { return fmt.Errorf("invalid token") } diff --git a/api/argon2.go b/api/argon2.go new file mode 100644 index 0000000..a595511 --- /dev/null +++ b/api/argon2.go @@ -0,0 +1,15 @@ +package api + +import "golang.org/x/crypto/argon2" + +const ( + ArgonTime = 1 + ArgonMemory = 256 * 1024 + ArgonThreads = 4 + ArgonKeySize = 32 + ArgonSaltSize = 16 +) + +func deriveArgon2IDKey(password, salt []byte) []byte { + return argon2.IDKey(password, salt, ArgonTime, ArgonMemory, ArgonThreads, ArgonKeySize) +}