summaryrefslogtreecommitdiff
path: root/internal/auth/users/sessionids.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/auth/users/sessionids.go')
-rw-r--r--internal/auth/users/sessionids.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/internal/auth/users/sessionids.go b/internal/auth/users/sessionids.go
new file mode 100644
index 0000000..9b0d59b
--- /dev/null
+++ b/internal/auth/users/sessionids.go
@@ -0,0 +1,35 @@
+package users
+
+import (
+ "crypto/rand"
+ "crypto/sha256"
+ "encoding/hex"
+)
+
+const SessionPrefix string = "rpu_"
+
+// generates an random id for session unique identifier
+func GenID(byteSize int) string {
+ id := make([]byte, byteSize)
+ _, err := rand.Read(id) // this is never supposed to error apparently
+ if err != nil {
+ return ""
+ }
+ fmtID := hex.EncodeToString(id)
+ return fmtID
+}
+
+// hash using sha256
+func HashSID(sid string) string {
+ h := sha256.New()
+ h.Write([]byte(sid))
+ hb := h.Sum(nil)
+ hs := hex.EncodeToString(hb)
+ return hs
+}
+
+// verify integrity of sid against database sid
+func VerifySID(sid string, dbsid string) bool {
+ n := HashSID(sid)
+ return n == dbsid
+}