Files
ferurl/utils/hash.go
Andrej Mickov f27f1fc3d6 Fixed database migration, imporved logging, imporved hashing function,
introduced connection pooling instead of single conncetion.
2025-08-07 20:45:18 +02:00

26 lines
475 B
Go

package utils
import (
"crypto/sha256"
)
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
func GenerateUrlHash(url string) string {
hash := sha256.Sum256([]byte(url))
return EncodeToBase62(string(hash[:]))
}
func EncodeToBase62(url string) string {
hash := sha256.Sum256([]byte(url))
encoded := ""
for _, b := range hash[:] {
encoded += string(base62[int(b)%62])
}
if len(encoded) > 7 {
return encoded[:7]
}
return encoded
}