plakken/utils.go

41 lines
746 B
Go
Raw Normal View History

2023-10-02 20:33:13 +02:00
package main
import (
"crypto/rand"
"encoding/hex"
"log"
mathrand "math/rand"
)
2023-10-02 20:33:13 +02:00
func generateUrl() string {
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, currentConfig.urlLength)
2023-10-02 20:33:13 +02:00
for i := range b {
b[i] = listChars[mathrand.Intn(len(listChars))]
2023-10-02 20:33:13 +02:00
}
return string(b)
}
func generateSecret() string {
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
log.Printf("Failed to generate secret")
}
return hex.EncodeToString(key)
}
func urlExist(url string) bool {
exist := db.Exists(ctx, url).Val()
return exist == 1
}
func verifySecret(url string, secret string) bool {
if secret == db.HGet(ctx, url, "secret").Val() {
return true
}
return false
}