summaryrefslogtreecommitdiff
path: root/internal/users/sessionids.go
blob: 9b0d59baf83b1a8ef8f353a1e1e5868de7c8772c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
}