Change base64 to hex encode

This commit is contained in:
Andreas Mieke 2018-06-09 19:38:15 +02:00
parent 09ce8b265c
commit 5a22e9ee01
No known key found for this signature in database
GPG key ID: 0C26F7695C85F85A

View file

@ -2,7 +2,7 @@ package routes
import (
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"net/http"
"git.1750studios.com/ToddShepard/ShortDragon/database"
@ -47,17 +47,17 @@ func Encode(c *gin.Context) {
if URL.Short.String == "" {
hasher := sha512.New()
hasher.Write([]byte(URL.Long.String))
base := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
hash := hex.EncodeToString(hasher.Sum(nil))
i := 2
for {
database.Db.Model(&database.URL{}).Where("short = ?", base[0:i]).Count(&count)
if count > 0 && i < len(base) {
database.Db.Model(&database.URL{}).Where("short = ?", hash[0:i]).Count(&count)
if count > 0 && i < len(hash) {
i = i + 1
} else if count > 0 {
c.AbortWithStatus(http.StatusConflict)
return
} else {
URL.Short.String = base[0:i]
URL.Short.String = hash[0:i]
break
}
}