-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
53 lines (46 loc) · 1.1 KB
/
util.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
package sm2lib
import (
"encoding/base64"
"encoding/hex"
"unsafe"
)
// []byte转string
func BytesToString(value []byte) string {
return *(*string)(unsafe.Pointer(&value)) // nolint
}
// 字符串转[]byte
func StringToBytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s)) // nolint
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h)) // nolint
}
// Base64编码
func Base64Encode(encoding *base64.Encoding, src []byte) []byte {
dst := make([]byte, encoding.EncodedLen(len(src)))
encoding.Encode(dst, src)
return dst
}
// Base64解码
func Base64Decode(encoding *base64.Encoding, src []byte) ([]byte, error) {
dst := make([]byte, encoding.DecodedLen(len(src)))
_, err := encoding.Decode(dst, src)
if err != nil {
return nil, err
}
return dst, nil
}
// Hex编码
func HexEncode(src []byte) []byte {
dst := make([]byte, hex.EncodedLen(len(src)))
hex.Encode(dst, src)
return dst
}
// Hex解码
func HexDecode(src []byte) ([]byte, error) {
dst := make([]byte, hex.DecodedLen(len(src)))
_, err := hex.Decode(dst, src)
if err != nil {
return nil, err
}
return dst, nil
}