-
Notifications
You must be signed in to change notification settings - Fork 69
/
dongle.go
69 lines (61 loc) · 1.48 KB
/
dongle.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// @Package dongle
// @Description a simple, semantic and developer-friendly golang crypto package
// @Page github.com/dromara/dongle
// @Developer gouguoyin
// @Blog www.gouguoyin.com
// @Email [email protected]
// Package dongle is a simple, semantic and developer-friendly golang crypto package.
package dongle
import (
"unsafe"
)
// Version current version
const Version = "1.0.1"
// dongle defines a dongle struct.
type dongle struct {
src []byte
dst []byte
Error error
}
var (
// Encode returns a new Encoder instance
Encode = newEncoder()
// Decode returns a new Decoder instance
Decode = newDecoder()
// Encrypt returns a new Encrypter instance
Encrypt = newEncrypter()
// Decrypt returns a new Decrypter instance
Decrypt = newDecrypter()
// Sign returns a new Signer instance
Sign = newSigner()
// Verify returns a new Verifier instance
Verify = newVerifier()
)
// converts string to byte slice without a memory allocation.
func string2bytes(s string) []byte {
if len(s) == 0 {
return []byte("")
}
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}
func bytes2string(b []byte) string {
if len(b) == 0 {
return ""
}
return *(*string)(unsafe.Pointer(&b))
}
// split the byte slice by the specified size.
func bytesSplit(buf []byte, size int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/size+1)
for len(buf) >= size {
chunk, buf = buf[:size], buf[size:]
chunks = append(chunks, chunk)
}
return chunks
}