-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti.go
55 lines (51 loc) · 1.36 KB
/
multi.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
49
50
51
52
53
54
55
package encoding
import (
"errors"
"strings"
)
var errUnsupportedEncoding = errors.New("encoding not supported")
// Decode returns the bytes represented by the decoded string src.
//
// Decode uses the decode method mapped to kind parameter.
// If the input is kind is unknown or the input is malformed for the decode method it returns an error.
func Decode(encoding, src string) ([]byte, error) {
switch strings.ToLower(encoding) {
case KindBase64:
return DecodeBase64(src)
case KindBase64URL:
return DecodeBase64URL(src)
case KindBase58:
return DecodeBase58(src)
case KindBase32:
return DecodeBase32(src)
case KindHex:
return DecodeHex(src)
case KindHex0XPrefix:
return DecodeHexZeroX(src)
case KindUTF8:
return DecodeUTF8(src), nil
default:
return nil, errUnsupportedEncoding
}
}
// Encode returns the bytes encoded as requested by the encoding parameter.
func Encode(encoding string, src []byte) (string, error) {
switch strings.ToLower(encoding) {
case KindBase64URL:
return EncodeBase64URL(src), nil
case KindBase64:
return EncodeBase64(src), nil
case KindBase58:
return EncodeBase58(src), nil
case KindBase32:
return EncodeBase32(src), nil
case KindHex:
return EncodeHex(src), nil
case KindHex0XPrefix:
return EncodeHexZeroX(src), nil
case KindUTF8:
return EncodeUTF8(src), nil
default:
return "", errUnsupportedEncoding
}
}