security/lib/util.go

58 lines
895 B
Go

package lib
import (
"crypto/rand"
"crypto/sha256"
"fmt"
"log"
"strings"
"time"
"github.com/gofiber/fiber/v2"
)
func GetFTime() string {
return time.Now().Format("01/02/2006")
}
func GetRandom() string {
buf := make([]byte, 128)
_, err := rand.Read(buf)
if err != nil {
log.Fatalf("Failed to get random bytes: %s", err.Error())
}
return GetSHA256(buf)
}
func GetSHA256(s []byte) string {
sum := sha256.Sum256(s)
return fmt.Sprintf("%x", sum)
}
func ContainsCase(s1 string, s2 string) bool {
return strings.Contains(
strings.ToLower(s1), strings.ToLower(s2),
)
}
func RenderError(c *fiber.Ctx, code int) error {
var msg string = "Server Error"
c.Status(code)
switch code {
case 404:
msg = "Not Found"
case 500:
msg = "Server error"
case 403:
msg = "Forbidden"
case 400:
msg = "Bad request"
}
return c.Render("error", fiber.Map{
"msg": msg,
})
}