-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbase64url.go
More file actions
34 lines (29 loc) · 830 Bytes
/
base64url.go
File metadata and controls
34 lines (29 loc) · 830 Bytes
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
package base64url
/*
Implementation of base64url removing the trailing = and also changing +,/ to the more
url friendly -,_ characters.
The decode implementation is reversing the process before running DecodeString from the base64 package.
*/
import (
"encoding/base64"
"errors"
"strings"
)
func Encode(data []byte) string {
str := base64.StdEncoding.EncodeToString(data)
str = strings.Replace(str, "+", "-", -1)
str = strings.Replace(str, "/", "_", -1)
str = strings.Replace(str, "=", "", -1)
return str
}
func Decode(str string) ([]byte, error) {
if strings.ContainsAny(str, "+/") {
return nil, errors.New("invalid base64url encoding")
}
str = strings.Replace(str, "-", "+", -1)
str = strings.Replace(str, "_", "/", -1)
for len(str)%4 != 0 {
str += "="
}
return base64.StdEncoding.DecodeString(str)
}