cityuni-webserver/pageHandler/utils/etag.go
Captain ALM 42dd0259af
All checks were successful
continuous-integration/drone/push Build is passing
Finish the page-handler.
Remove unneeded utils.
Migrate ETag calculation code.
2022-07-15 12:18:33 +01:00

49 lines
982 B
Go

package utils
import (
"crypto"
"encoding/hex"
"strings"
)
func GetValueForETagUsingByteArray(b []byte) string {
theHash := crypto.SHA1.New()
_, _ = theHash.Write(b)
theSum := theHash.Sum(nil)
theHash.Reset()
return "\"" + hex.EncodeToString(theSum) + "\""
}
func GetETagValues(stringIn string) []string {
if strings.ContainsAny(stringIn, ",") {
seperated := strings.Split(stringIn, ",")
toReturn := make([]string, len(seperated))
pos := 0
for _, s := range seperated {
cETag := GetETagValue(s)
if cETag != "" {
toReturn[pos] = cETag
pos += 1
}
}
if pos == 0 {
return nil
}
return toReturn[:pos]
}
toReturn := []string{GetETagValue(stringIn)}
if toReturn[0] == "" {
return nil
}
return toReturn
}
func GetETagValue(stringIn string) string {
startIndex := strings.IndexAny(stringIn, "\"") + 1
endIndex := strings.LastIndexAny(stringIn, "\"")
if endIndex > startIndex {
return stringIn[startIndex:endIndex]
}
return ""
}