-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
68 lines (60 loc) · 1.66 KB
/
encode.go
File metadata and controls
68 lines (60 loc) · 1.66 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: EUPL-1.2
// Encoding helpers for the Core framework.
// Wraps encoding/hex so consumers can use core primitives for common
// byte-string encodings.
package core
import (
"encoding/base64"
"encoding/hex"
)
// HexEncode returns src encoded as a lowercase hexadecimal string.
//
// s := core.HexEncode([]byte("hello"))
func HexEncode(src []byte) string {
return hex.EncodeToString(src)
}
// HexDecode decodes a hexadecimal string into bytes.
//
// r := core.HexDecode("68656c6c6f")
// if r.OK { b := r.Value.([]byte) }
func HexDecode(s string) Result {
b, err := hex.DecodeString(s)
if err != nil {
return Result{err, false}
}
return Result{b, true}
}
// Base64Encode returns src encoded as a standard base64 string.
//
// s := core.Base64Encode([]byte("hello"))
func Base64Encode(src []byte) string {
return base64.StdEncoding.EncodeToString(src)
}
// Base64Decode decodes a standard base64 string into bytes.
//
// r := core.Base64Decode("aGVsbG8=")
// if r.OK { b := r.Value.([]byte) }
func Base64Decode(s string) Result {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return Result{err, false}
}
return Result{b, true}
}
// Base64URLEncode returns src encoded as a URL-safe base64 string.
//
// s := core.Base64URLEncode([]byte("hello"))
func Base64URLEncode(src []byte) string {
return base64.URLEncoding.EncodeToString(src)
}
// Base64URLDecode decodes a URL-safe base64 string into bytes.
//
// r := core.Base64URLDecode("aGVsbG8=")
// if r.OK { b := r.Value.([]byte) }
func Base64URLDecode(s string) Result {
b, err := base64.URLEncoding.DecodeString(s)
if err != nil {
return Result{err, false}
}
return Result{b, true}
}