photoprism/pkg/rnd/crc.go
Michael Mayer f5a8c5a45d Auth: Session and ACL enhancements #98 #1746
Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-09-28 09:01:17 +02:00

38 lines
726 B
Go

package rnd
import (
"fmt"
"hash/crc32"
"strconv"
)
// CrcToken returns a string token with checksum.
func CrcToken() string {
token := make([]byte, 0, 14)
token = append(token, []byte(GenerateToken(4))...)
token = append(token, '-')
token = append(token, []byte(GenerateToken(4))...)
checksum := crc32.ChecksumIEEE(token)
sum := strconv.FormatInt(int64(checksum), 16)
return fmt.Sprintf("%s-%.4s", token, sum)
}
// ValidateCrcToken tests if the token string is valid.
func ValidateCrcToken(s string) bool {
if len(s) != 14 {
return false
}
token := []byte(s[:9])
checksum := crc32.ChecksumIEEE(token)
sum := strconv.FormatInt(int64(checksum), 16)
return s == fmt.Sprintf("%s-%.4s", token, sum)
}