Add the encrypt toolkit

This commit is contained in:
2025-10-25 02:59:57 +08:00
parent 41783813b1
commit 3c01aed25a

View File

@@ -1,6 +1,8 @@
package admin package admin
import ( import (
"crypto/rand"
"encoding/hex"
"encoding/json" "encoding/json"
"net/http" "net/http"
"networkDev/database" "networkDev/database"
@@ -9,8 +11,6 @@ import (
"networkDev/utils/encrypt" "networkDev/utils/encrypt"
"strconv" "strconv"
"strings" "strings"
"crypto/rand"
"encoding/hex"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -281,8 +281,8 @@ func APIGenerateKeysHandler(w http.ResponseWriter, r *http.Request) {
} }
var req struct { var req struct {
Side string `json:"side"` // submit | return Side string `json:"side"` // submit | return
Algorithm int `json:"algorithm"` // 与 models.Algorithm* 对应 Algorithm int `json:"algorithm"` // 与 models.Algorithm* 对应
} }
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest) http.Error(w, "Invalid JSON", http.StatusBadRequest)
@@ -377,62 +377,62 @@ func APIGenerateKeysHandler(w http.ResponseWriter, r *http.Request) {
} }
func APIResetKeyHandler(w http.ResponseWriter, r *http.Request) { func APIResetKeyHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
} }
var req struct { var req struct {
ID uint `json:"id"` ID uint `json:"id"`
} }
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest) http.Error(w, "Invalid JSON", http.StatusBadRequest)
return return
} }
if req.ID == 0 { if req.ID == 0 {
http.Error(w, "接口ID不能为空", http.StatusBadRequest) http.Error(w, "接口ID不能为空", http.StatusBadRequest)
return return
} }
db, err := database.GetDB() db, err := database.GetDB()
if err != nil { if err != nil {
logrus.WithError(err).Error("Failed to get database connection") logrus.WithError(err).Error("Failed to get database connection")
http.Error(w, "Internal server error", http.StatusInternalServerError) http.Error(w, "Internal server error", http.StatusInternalServerError)
return return
} }
var api models.API var api models.API
if err := db.First(&api, req.ID).Error; err != nil { if err := db.First(&api, req.ID).Error; err != nil {
http.Error(w, "接口不存在", http.StatusNotFound) http.Error(w, "接口不存在", http.StatusNotFound)
return return
} }
// 生成新的16位大写十六进制密钥 // 生成新的16位大写十六进制密钥
bytes := make([]byte, 8) bytes := make([]byte, 8)
if _, err := rand.Read(bytes); err != nil { if _, err := rand.Read(bytes); err != nil {
logrus.WithError(err).Error("Failed to generate random API key") logrus.WithError(err).Error("Failed to generate random API key")
http.Error(w, "生成密钥失败", http.StatusInternalServerError) http.Error(w, "生成密钥失败", http.StatusInternalServerError)
return return
} }
newKey := strings.ToUpper(hex.EncodeToString(bytes)) newKey := strings.ToUpper(hex.EncodeToString(bytes))
if err := db.Model(&api).Update("api_key", newKey).Error; err != nil { if err := db.Model(&api).Update("api_key", newKey).Error; err != nil {
logrus.WithError(err).Error("Failed to update API key") logrus.WithError(err).Error("Failed to update API key")
http.Error(w, "更新密钥失败", http.StatusInternalServerError) http.Error(w, "更新密钥失败", http.StatusInternalServerError)
return return
} }
response := map[string]interface{}{ response := map[string]interface{}{
"success": true, "success": true,
"message": "接口密钥重置成功", "message": "接口密钥重置成功",
"data": map[string]interface{}{ "data": map[string]interface{}{
"id": api.ID, "id": api.ID,
"api_key": newKey, "api_key": newKey,
}, },
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response) json.NewEncoder(w).Encode(response)
} }