plakken/utils.go

37 lines
701 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 {
2023-10-02 20:33:13 +02:00
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 {
return db.Exists(ctx, url).Val() == 1
}
func VerifySecret(url string, secret string) bool {
return secret == db.HGet(ctx, url, "secret").Val()
}