Files
spend-sparrow/internal/service/random_generator.go
Tim Wundenberg 6219741634
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 4m49s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 5m8s
fix: move implementation to "internal" package
2025-05-29 13:42:13 +02:00

55 lines
1.0 KiB
Go

package service
import (
"crypto/rand"
"encoding/base64"
"spend-sparrow/internal/log"
"spend-sparrow/internal/types"
"github.com/google/uuid"
)
type Random interface {
Bytes(size int) ([]byte, error)
String(size int) (string, error)
UUID() (uuid.UUID, error)
}
type RandomImpl struct {
}
func NewRandom() *RandomImpl {
return &RandomImpl{}
}
func (r *RandomImpl) Bytes(size int) ([]byte, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
log.Error("Error generating random bytes: %v", err)
return []byte{}, types.ErrInternal
}
return b, nil
}
func (r *RandomImpl) String(size int) (string, error) {
bytes, err := r.Bytes(size)
if err != nil {
log.Error("Error generating random string: %v", err)
return "", types.ErrInternal
}
return base64.StdEncoding.EncodeToString(bytes), nil
}
func (r *RandomImpl) UUID() (uuid.UUID, error) {
id, err := uuid.NewRandom()
if err != nil {
log.Error("Error generating random UUID: %v", err)
return uuid.Nil, types.ErrInternal
}
return id, nil
}