Files
pastebin/handler/util.go
T
2026-04-21 06:00:03 +02:00

47 lines
884 B
Go

package handler
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"time"
)
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
func generateId() (string, error) {
bytes := make([]byte, 10)
if _, err := io.ReadFull(rand.Reader, bytes); err != nil {
return "", err
}
for i := range bytes {
bytes[i] = charset[bytes[i]%byte(len(charset))]
}
return string(bytes), nil
}
func TimeAgo(t time.Time) string {
ago := time.Since(t)
seconds := int(ago.Seconds())
switch {
case seconds < 60:
return fmt.Sprintf("%ds ago", seconds)
case seconds < 3600:
return fmt.Sprintf("%dm ago", seconds/60)
case seconds < 86400:
return fmt.Sprintf("%dh ago", seconds/3600)
default:
return fmt.Sprintf("%dd ago", seconds/86400)
}
}
func hash(content string) string {
h := sha256.New()
h.Write([]byte(content))
return hex.EncodeToString(h.Sum(nil))
}