security/lib/util.go

58 lines
895 B
Go
Raw Permalink Normal View History

2024-03-21 20:20:24 +00:00
package lib
import (
"crypto/rand"
"crypto/sha256"
"fmt"
"log"
"strings"
"time"
"github.com/gofiber/fiber/v2"
)
func GetFTime() string {
2024-08-13 19:25:36 +00:00
return time.Now().Format("01/02/2006")
2024-03-21 20:20:24 +00:00
}
func GetRandom() string {
2024-08-13 19:25:36 +00:00
buf := make([]byte, 128)
_, err := rand.Read(buf)
if err != nil {
log.Fatalf("Failed to get random bytes: %s", err.Error())
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
return GetSHA256(buf)
2024-03-21 20:20:24 +00:00
}
func GetSHA256(s []byte) string {
2024-08-13 19:25:36 +00:00
sum := sha256.Sum256(s)
return fmt.Sprintf("%x", sum)
2024-03-21 20:20:24 +00:00
}
func ContainsCase(s1 string, s2 string) bool {
2024-08-13 19:25:36 +00:00
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,
})
2024-03-21 20:20:24 +00:00
}