1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-07-01 00:00:23 +08:00

complete en/decode

This commit is contained in:
fumiama
2021-12-27 23:24:41 +08:00
parent 7cb3ef6488
commit bba9662a5b
6 changed files with 41 additions and 14 deletions

View File

@@ -2,6 +2,9 @@ package link
import (
"net"
"unsafe"
tea "github.com/fumiama/gofastTEA"
)
var (
@@ -9,7 +12,7 @@ var (
// 利用 Curve25519 生成
// https://pkg.go.dev/golang.org/x/crypto/curve25519
// https://www.zhihu.com/question/266758647
privKey []byte
privKey [32]byte
// 本机虚拟 ip
me net.IP
// 本机 endpoint
@@ -17,7 +20,7 @@ var (
)
// SetMyself 设置本机参数
func SetMyself(privateKey []byte, myIP string, myEndpoint string) {
func SetMyself(privateKey [32]byte, myIP string, myEndpoint string) {
privKey = privateKey
var err error
myend, err = net.ResolveUDPAddr("udp", myEndpoint)
@@ -31,8 +34,7 @@ func SetMyself(privateKey []byte, myIP string, myEndpoint string) {
}
}
// Encode 使用 ChaCha20-Poly1305 加密
// https://pkg.go.dev/golang.org/x/crypto/chacha20poly1305
// Encode 使用 TEA 加密
func (l *Link) Encode(b []byte) (eb []byte, err error) {
if b == nil {
return
@@ -42,13 +44,12 @@ func (l *Link) Encode(b []byte) (eb []byte, err error) {
} else {
// 在此处填写加密逻辑密钥是l.key输入是b输出是eb
// 不用写return直接赋值给eb即可
eb = b
eb = (*tea.TEA)(unsafe.Pointer(&privKey)).Encrypt(b)
}
return
}
// Decode 使用 ChaCha20-Poly1305 解密
// https://pkg.go.dev/golang.org/x/crypto/chacha20poly1305
// Decode 使用 TEA 解密
func (l *Link) Decode(b []byte) (db []byte, err error) {
if b == nil {
return
@@ -58,7 +59,7 @@ func (l *Link) Decode(b []byte) (db []byte, err error) {
} else {
// 在此处填写解密逻辑密钥是l.key输入是b输出是db
// 不用写return直接赋值给db即可
db = b
db = (*tea.TEA)(unsafe.Pointer(&privKey)).Decrypt(b)
}
return
}