forked from hyperworks/go-getstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.go
39 lines (33 loc) · 833 Bytes
/
sign.go
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
36
37
38
39
package getstream
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"strings"
)
func urlSafe(src string) string {
src = strings.Replace(src, "+", "-", -1)
src = strings.Replace(src, "/", "_", -1)
src = strings.Trim(src, "=")
return src
}
func Sign(secret, message string) string {
hash := sha1.New()
hash.Write([]byte(secret))
key := hash.Sum(nil)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(message))
digest := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return urlSafe(digest)
}
func SignSlug(secret string, slug Slug) Slug {
return Slug{slug.Slug, slug.ID, Sign(secret, slug.Slug+slug.ID)}
}
func SignActivity(secret string, activity *Activity) *Activity {
result := &Activity{}
*result = *activity
for i, slug := range result.To {
result.To[i] = SignSlug(secret, slug)
}
return result
}