MWSE/internal/httpserver/util.go

27 lines
674 B
Go

package httpserver
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
)
// newToken returns a random UUIDv4-shaped token for API keys (the original used
// crypto.randomUUID()).
func newToken() string {
var b [16]byte
if _, err := rand.Read(b[:]); err != nil {
panic(fmt.Sprintf("httpserver: cannot read random bytes: %v", err))
}
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16])
}
// sha256hex hashes a credential to a hex digest, matching the Room service.
func sha256hex(s string) string {
sum := sha256.Sum256([]byte(s))
return hex.EncodeToString(sum[:])
}