Compare commits
No commits in common. "main" and "v0.0.59" have entirely different histories.
20
README.md
20
README.md
|
@ -5,23 +5,3 @@ go env -w GONOPROXY=git.apinb.com/*
|
||||||
go env -w GOINSECURE=git.apinb.com/*
|
go env -w GOINSECURE=git.apinb.com/*
|
||||||
go env -w GONOSUMDB=git.apinb.com/*
|
go env -w GONOSUMDB=git.apinb.com/*
|
||||||
```
|
```
|
||||||
# crypto 加密与解密
|
|
||||||
## GCM加密
|
|
||||||
```
|
|
||||||
AESGCMEncrypt GCM 加密
|
|
||||||
AESGCMDecrypt GCM 解密
|
|
||||||
```
|
|
||||||
## CBC加密
|
|
||||||
```
|
|
||||||
Encrypt CBC加密
|
|
||||||
Decrypt CBC解密
|
|
||||||
```
|
|
||||||
## ECB加密
|
|
||||||
```
|
|
||||||
AesEncryptECB ECB加密
|
|
||||||
AesDecryptECB ECB解密
|
|
||||||
```
|
|
||||||
## 环境变量检测
|
|
||||||
```
|
|
||||||
AesKeyCheck 秘钥环境变量检测
|
|
||||||
```
|
|
||||||
|
|
|
@ -4,58 +4,10 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"crypto/rand"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// =================== GCM ======================
|
// AES加密
|
||||||
// AEC GCM 加密
|
|
||||||
func AESGCMEncrypt(plaintext, key []byte) (string, error) {
|
|
||||||
block, err := aes.NewCipher(key)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
gcm, err := cipher.NewGCM(block)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
nonce := make([]byte, gcm.NonceSize())
|
|
||||||
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
ciphertext := gcm.Seal(nonce, nonce, plaintext, nil)
|
|
||||||
return hex.EncodeToString(ciphertext), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AEC GCM 解密
|
|
||||||
func AESGCMDecrypt(ciphertext string, key []byte) ([]byte, error) {
|
|
||||||
data, err := hex.DecodeString(ciphertext)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
block, err := aes.NewCipher(key)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
gcm, err := cipher.NewGCM(block)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
nonceSize := gcm.NonceSize()
|
|
||||||
if len(data) < nonceSize {
|
|
||||||
return nil, errors.New("密文无效")
|
|
||||||
}
|
|
||||||
nonce, cipherbyte := data[:nonceSize], data[nonceSize:]
|
|
||||||
return gcm.Open(nil, nonce, cipherbyte, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// =================== CBC ======================
|
|
||||||
// AES CBC加密
|
|
||||||
func Encrypt(key string, iv string, data string) string {
|
func Encrypt(key string, iv string, data string) string {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return ""
|
return ""
|
||||||
|
@ -72,7 +24,7 @@ func Encrypt(key string, iv string, data string) string {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// AES CBC解密
|
// AES解密
|
||||||
func Decrypt(key string, iv string, data string) string {
|
func Decrypt(key string, iv string, data string) string {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return ""
|
return ""
|
||||||
|
@ -150,24 +102,3 @@ func generateKey(key []byte) (genKey []byte) {
|
||||||
}
|
}
|
||||||
return genKey
|
return genKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func AesKeyCheck(key string) (string, error) {
|
|
||||||
// 从环境变量获取密钥
|
|
||||||
keyHex := os.Getenv(key)
|
|
||||||
if keyHex == "" {
|
|
||||||
fmt.Println("环境变量 RST_KEY 未设置")
|
|
||||||
return "", errors.New("环境变量 RST_KEY 未设置")
|
|
||||||
}
|
|
||||||
// 解码十六进制字符串的密钥
|
|
||||||
byteKey, err := hex.DecodeString(keyHex)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("密钥解码失败: %v\n", err)
|
|
||||||
return "", errors.New("密钥解码失败")
|
|
||||||
}
|
|
||||||
// 检查密钥长度
|
|
||||||
if len(byteKey) != 16 && len(key) != 24 && len(key) != 32 {
|
|
||||||
fmt.Printf("无效的密钥长度: %d 字节 (需要16,24或32字节)\n", len(key))
|
|
||||||
return "", errors.New("无效的密钥长度,需要16,24或32字节")
|
|
||||||
}
|
|
||||||
return keyHex, nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ func StringToFile(path, content string) error {
|
||||||
// rootDir: 文件夹根目录
|
// rootDir: 文件夹根目录
|
||||||
// s: 存储文件名的切片
|
// s: 存储文件名的切片
|
||||||
// filter: 过滤条件:".git", ".idea", ".vscode", ".gitignore", ".gitea", ".github", ".golangci.yml", "*.pyc"
|
// filter: 过滤条件:".git", ".idea", ".vscode", ".gitignore", ".gitea", ".github", ".golangci.yml", "*.pyc"
|
||||||
func FileTreeByFilter(rootDir string, s []string, filter []string) ([]string, error) {
|
func FileTree(rootDir string, s []string, filter []string) ([]string, error) {
|
||||||
rd, err := os.ReadDir(rootDir)
|
rd, err := os.ReadDir(rootDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s, err
|
return s, err
|
||||||
|
@ -50,48 +50,7 @@ func FileTreeByFilter(rootDir string, s []string, filter []string) ([]string, er
|
||||||
|
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
fullDir := rootDir + "/" + fi.Name()
|
fullDir := rootDir + "/" + fi.Name()
|
||||||
s, err = FileTreeByFilter(fullDir, s, filter)
|
s, err = FileTree(fullDir, s, filter)
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fullName := rootDir + "/" + fi.Name()
|
|
||||||
s = append(s, fullName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 递归遍历文件夹
|
|
||||||
// rootDir: 文件夹根目录
|
|
||||||
// s: 存储文件名的切片
|
|
||||||
// allow: 允许条件:".zip", ".check"
|
|
||||||
func FileTreeBySelect(rootDir string, s []string, allow []string) ([]string, error) {
|
|
||||||
rd, err := os.ReadDir(rootDir)
|
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
for _, fi := range rd {
|
|
||||||
// 检查文件名是否匹配任何一个过滤模式
|
|
||||||
matched := false
|
|
||||||
for _, item := range allow {
|
|
||||||
exists, err := filepath.Match(item, fi.Name())
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if exists {
|
|
||||||
matched = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !matched {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if fi.IsDir() {
|
|
||||||
fullDir := rootDir + "/" + fi.Name()
|
|
||||||
s, err = FileTreeBySelect(fullDir, s, allow)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue