12345678910111213141516171819202122232425262728293031323334 |
- package sec
- import (
- "crypto/hmac"
- "crypto/sha256"
- "encoding/hex"
- "math/rand"
- "git.clearsky.net.au/cody/gex.git/srv"
- )
- var permissions = make(map[string][]string)
- func Route(pattern string, roles []string, handler func(req *srv.Req, res srv.Res)) {
- permissions[pattern] = roles
- srv.Route(pattern, handler)
- }
- func Hash(key string, data string) string {
- bKey := []byte(key)
- bData := []byte(data)
- h := hmac.New(sha256.New, bKey)
- h.Write(bData)
- return hex.EncodeToString(h.Sum(nil))
- }
- func Salt(length int) string {
- const charset = "1234567890-=!@#$%^&*()_+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- b := make([]byte, length)
- for i := range b {
- b[i] = charset[rand.Intn(len(charset))]
- }
- return string(b)
- }
|