Skip to content

Commit 808a02e

Browse files
committed
Add hashing and serialization utilities with tests
1 parent 4318c26 commit 808a02e

11 files changed

Lines changed: 374 additions & 0 deletions

File tree

.github/workflows/action.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: ["master", "development"]
6+
tags:
7+
- "*"
8+
pull_request:
9+
branches: ["master", "development"]
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: "1.23.1"
21+
22+
- name: Test
23+
run: go test -v ./...

go.mod

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module github.com/PRYVT/utils
2+
3+
go 1.23.1
4+
5+
require (
6+
github.com/bytedance/sonic v1.11.6 // indirect
7+
github.com/bytedance/sonic/loader v0.1.1 // indirect
8+
github.com/cloudwego/base64x v0.1.4 // indirect
9+
github.com/cloudwego/iasm v0.2.0 // indirect
10+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
11+
github.com/gin-contrib/sse v0.1.0 // indirect
12+
github.com/gin-gonic/gin v1.10.0 // indirect
13+
github.com/go-playground/locales v0.14.1 // indirect
14+
github.com/go-playground/universal-translator v0.18.1 // indirect
15+
github.com/go-playground/validator/v10 v10.20.0 // indirect
16+
github.com/goccy/go-json v0.10.2 // indirect
17+
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
18+
github.com/google/uuid v1.6.0 // indirect
19+
github.com/json-iterator/go v1.1.12 // indirect
20+
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
21+
github.com/leodido/go-urn v1.4.0 // indirect
22+
github.com/mattn/go-colorable v0.1.13 // indirect
23+
github.com/mattn/go-isatty v0.0.20 // indirect
24+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
25+
github.com/modern-go/reflect2 v1.0.2 // indirect
26+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
27+
github.com/rs/zerolog v1.33.0 // indirect
28+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
29+
github.com/ugorji/go/codec v1.2.12 // indirect
30+
golang.org/x/arch v0.8.0 // indirect
31+
golang.org/x/crypto v0.23.0 // indirect
32+
golang.org/x/net v0.25.0 // indirect
33+
golang.org/x/sys v0.20.0 // indirect
34+
golang.org/x/text v0.15.0 // indirect
35+
google.golang.org/protobuf v1.34.1 // indirect
36+
gopkg.in/yaml.v3 v3.0.1 // indirect
37+
)

go.sum

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
2+
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
3+
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
4+
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
5+
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
6+
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
7+
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
8+
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
9+
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
10+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
13+
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
14+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
15+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
16+
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
17+
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
18+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
19+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
20+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
21+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
22+
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
23+
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
24+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
25+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
26+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
27+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
28+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
29+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
30+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
31+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
32+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
33+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
34+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
35+
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
36+
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
37+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
38+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
39+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
40+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
41+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
42+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
43+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
44+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
45+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
46+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
47+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
48+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
49+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
50+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
51+
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
52+
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
53+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
54+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55+
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
56+
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
57+
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
58+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
59+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
60+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
61+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
62+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
63+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
64+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
65+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
66+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
67+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
68+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
69+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
70+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
71+
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
72+
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
73+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
74+
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
75+
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
76+
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
77+
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
78+
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
79+
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
80+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
81+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
82+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
83+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
84+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
85+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
86+
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
87+
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
88+
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
89+
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
90+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
91+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
92+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
93+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
94+
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
95+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

pkg/auth/auth_middleware.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package auth
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/rs/zerolog/log"
8+
)
9+
10+
type AuthMiddleware struct {
11+
tokenManager *TokenManager
12+
}
13+
14+
func NewAuthMiddleware(tm *TokenManager) *AuthMiddleware {
15+
return &AuthMiddleware{tokenManager: tm}
16+
}
17+
18+
func (am *AuthMiddleware) AuthenticateMiddleware(c *gin.Context) {
19+
// Retrieve the token from the Authorization header
20+
authHeader := c.GetHeader("Authorization")
21+
if authHeader == "" {
22+
log.Error().Msg("Authorization header is missing")
23+
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
24+
c.Abort()
25+
return
26+
}
27+
28+
// Check if the token has the Bearer prefix
29+
const prefix = "Bearer "
30+
if len(authHeader) < len(prefix) || authHeader[:len(prefix)] != prefix {
31+
log.Error().Msg("Authorization header format must be Bearer {token}")
32+
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
33+
c.Abort()
34+
return
35+
}
36+
37+
// Extract the token
38+
tokenString := authHeader[len(prefix):]
39+
40+
// Verify the token
41+
_, err := am.tokenManager.VerifyToken(tokenString)
42+
if err != nil {
43+
log.Error().Err(err).Msg("Token verification failed")
44+
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
45+
c.Abort()
46+
return
47+
}
48+
c.Next()
49+
}

pkg/auth/token_manager.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package auth
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
"github.com/golang-jwt/jwt/v5"
9+
"github.com/google/uuid"
10+
"github.com/rs/zerolog/log"
11+
)
12+
13+
type TokenManager struct {
14+
signingSecret string
15+
}
16+
17+
func NewTokenManager() (*TokenManager, error) {
18+
secret := os.Getenv("SIGNING_SECRET")
19+
if secret == "" {
20+
return nil, fmt.Errorf("SIGNING_SECRET environment variable not set")
21+
}
22+
return &TokenManager{signingSecret: secret}, nil
23+
}
24+
25+
func (tm *TokenManager) CreateToken(userUuid uuid.UUID) (string, error) {
26+
t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
27+
"sub": userUuid,
28+
"iss": "pryvt",
29+
"aud": "local-audience",
30+
"exp": time.Now().Add(time.Second * 30).Unix(),
31+
"iat": time.Now().Unix(),
32+
})
33+
34+
signedKey, err := t.SignedString([]byte(tm.signingSecret))
35+
if err != nil {
36+
log.Err(err).Msg("Error while signing token")
37+
return "", fmt.Errorf("error while signing token")
38+
}
39+
return signedKey, nil
40+
}
41+
func (tm *TokenManager) VerifyToken(tokenString string) (*jwt.Token, error) {
42+
// Parse the token with the secret key
43+
44+
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
45+
return []byte(tm.signingSecret), nil
46+
})
47+
48+
// Check for verification errors
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
// Check if the token is valid
54+
if !token.Valid {
55+
return nil, fmt.Errorf("invalid token")
56+
}
57+
58+
// Return the verified token
59+
return token, nil
60+
}

pkg/hash/hashing.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package hash
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
)
7+
8+
func HashPassword(password string) string {
9+
hasher := sha256.New()
10+
hasher.Write([]byte(password))
11+
return hex.EncodeToString(hasher.Sum(nil))
12+
}

pkg/hash/hashing_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package hash
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestHashPassword(t *testing.T) {
8+
password := "mysecretpassword"
9+
hashedPassword := HashPassword(password)
10+
11+
if hashedPassword != "94aefb8be78b2b7c344d11d1ba8a79ef087eceb19150881f69460b8772753263" {
12+
t.Errorf("Expected hashed password to be non-empty")
13+
}
14+
15+
}

pkg/hash/uuid.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package hash
2+
3+
import (
4+
"github.com/google/uuid"
5+
)
6+
7+
func GenerateGUID(input string) uuid.UUID {
8+
namespace := uuid.MustParse("6b37b120-9dad-1ad1-8fb4-00c04fd430c8")
9+
guid := uuid.NewSHA1(namespace, []byte(input))
10+
return guid
11+
}

pkg/hash/uuid_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package hash
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGenerateGUID(t *testing.T) {
8+
input := "test-input"
9+
guid := GenerateGUID(input)
10+
expectedGuid := "ed5d3bd6-3c53-5059-be16-b5dd4b170406"
11+
if guid.String() != expectedGuid {
12+
t.Errorf("Expected %v, but got %v", expectedGuid, guid)
13+
}
14+
}

pkg/serialization/serialization.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package serialization
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
func UnsafeDeserializeAny[T any](b []byte) *T {
8+
out := new(T)
9+
err := json.Unmarshal(b, &out)
10+
if err != nil {
11+
panic(err)
12+
}
13+
return out
14+
}
15+
16+
func UnsafeSerializeAny[T any](t T) []byte {
17+
18+
b, err := json.Marshal(t)
19+
if err != nil {
20+
panic(err)
21+
}
22+
return b
23+
}

0 commit comments

Comments
 (0)