forked from iromli/go-itsdangerous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitsdangerous.go
48 lines (40 loc) · 1.04 KB
/
itsdangerous.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
40
41
42
43
44
45
46
47
48
/*
Package itsdangerous implements various functions to deal with untrusted sources.
Mainly useful for web applications.
This package exists purely as a port of https://github.com/mitsuhiko/itsdangerous,
where the original version is written in Python.
*/
package itsdangerous
import (
"encoding/base64"
"fmt"
"strings"
"time"
)
// 2011/01/01 in UTC
const EPOCH = 1293840000
// Encodes a single string. The resulting string is safe for putting into URLs.
func base64Encode(src []byte) string {
s := base64.URLEncoding.EncodeToString(src)
return strings.Trim(s, "=")
}
// Decodes a single string.
func base64Decode(s string) ([]byte, error) {
var padLen int
if l := len(s) % 4; l > 0 {
padLen = 4 - l
} else {
padLen = 1
}
b, err := base64.URLEncoding.DecodeString(s + strings.Repeat("=", padLen))
if err != nil {
fmt.Println(s)
return []byte(""), err
}
return b, nil
}
// Returns the current timestamp. This implementation returns the
// seconds since 1/1/2011.
func getTimestamp() uint32 {
return uint32(time.Now().Unix() - EPOCH)
}