CRC an actual dbuser struct

Per-field method left as a fallback
This commit is contained in:
0xCA 2023-12-30 14:14:32 +05:00
parent 38e4f5cbec
commit 845f438087

View file

@ -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 {