From 845f438087ea6f3feda0ad032322747c38346512 Mon Sep 17 00:00:00 2001 From: 0xCA Date: Sat, 30 Dec 2023 14:14:32 +0500 Subject: [PATCH] CRC an actual dbuser struct Per-field method left as a fallback --- util/util.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/util/util.go b/util/util.go index d9b9bc0..e294639 100644 --- a/util/util.go +++ b/util/util.go @@ -2,6 +2,8 @@ package util import ( "bufio" + "bytes" + "encoding/gob" "encoding/json" "errors" "fmt" @@ -830,11 +832,17 @@ func filterStringSlice(s []string, excludedStr string) []string { } func GetDBUserCRC32(dbuser model.User) uint32 { - var isAdmin byte = 0 - if dbuser.Admin { - isAdmin = 1 + buf := new(bytes.Buffer) + enc := gob.NewEncoder(buf) + if err := enc.Encode(dbuser); err != nil { + // Should be unreachable, fallback for the case + var isAdmin byte = 0 + if dbuser.Admin { + isAdmin = 1 + } + return crc32.ChecksumIEEE(ConcatMultipleSlices([]byte(dbuser.Username), []byte{isAdmin}, []byte(dbuser.PasswordHash), []byte(dbuser.Password))) } - return crc32.ChecksumIEEE(ConcatMultipleSlices([]byte(dbuser.Username), []byte{isAdmin}, []byte(dbuser.PasswordHash), []byte(dbuser.Password))) + return crc32.ChecksumIEEE(buf.Bytes()) } func ConcatMultipleSlices(slices ...[]byte) []byte {