2021-12-14 18:34:52 +01:00
|
|
|
package sanitize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hex removes invalid character from a hex string and makes it lowercase.
|
|
|
|
func Hex(s string) string {
|
2021-12-16 15:26:54 +01:00
|
|
|
if s == "" || reject(s, 1024) {
|
2021-12-15 12:24:05 +01:00
|
|
|
return ""
|
2021-12-14 18:34:52 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 15:26:54 +01:00
|
|
|
s = strings.ToLower(strings.TrimSpace(s))
|
2021-12-14 18:34:52 +01:00
|
|
|
|
|
|
|
// Remove all invalid characters.
|
|
|
|
s = strings.Map(func(r rune) rune {
|
|
|
|
if (r < '0' || r > '9') && (r < 'a' || r > 'f') {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}, s)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|