sec.go 738 B

12345678910111213141516171819202122232425262728293031323334
  1. package sec
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "math/rand"
  7. "git.clearsky.net.au/cody/gex.git/srv"
  8. )
  9. var permissions = make(map[string][]string)
  10. func Route(pattern string, roles []string, handler func(req *srv.Req, res srv.Res)) {
  11. permissions[pattern] = roles
  12. srv.Route(pattern, handler)
  13. }
  14. func Hash(key string, data string) string {
  15. bKey := []byte(key)
  16. bData := []byte(data)
  17. h := hmac.New(sha256.New, bKey)
  18. h.Write(bData)
  19. return hex.EncodeToString(h.Sum(nil))
  20. }
  21. func Salt(length int) string {
  22. const charset = "1234567890-=!@#$%^&*()_+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  23. b := make([]byte, length)
  24. for i := range b {
  25. b[i] = charset[rand.Intn(len(charset))]
  26. }
  27. return string(b)
  28. }