From 50ab4ff8b747e5f6ff6be3eef470fe8e7dff1e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=AD=90=E6=97=BA?= <15039612+liu-ziwang123@user.noreply.gitee.com> Date: Wed, 30 Oct 2024 18:55:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=B8=A6=E5=AF=86=E5=86=99?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- godo/files/fs.go | 9 +-------- godo/files/os.go | 9 +++++++-- godo/files/pwdfile.go | 20 ++++++++------------ godo/libs/encode.go | 3 ++- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/godo/files/fs.go b/godo/files/fs.go index d7e635d..6a3556e 100644 --- a/godo/files/fs.go +++ b/godo/files/fs.go @@ -318,7 +318,6 @@ func HandleCopyFile(w http.ResponseWriter, r *http.Request) { // 带加密写 func HandleWriteFile(w http.ResponseWriter, r *http.Request) { filePath := r.URL.Query().Get("filePath") - pwd := r.Header.Get("filePwd") basePath, err := libs.GetOsDir() if err != nil { libs.HTTPError(w, http.StatusInternalServerError, err.Error()) @@ -367,12 +366,6 @@ func HandleWriteFile(w http.ResponseWriter, r *http.Request) { libs.SuccessMsg(w, "", "success") return } - // 校验密码 - salt := GetSalt(r) - if !CheckFilePwd(pwd, salt) { - libs.HTTPError(w, http.StatusBadRequest, "密码错误") - return - } // 加密 data, err := libs.EncryptData(filedata, libs.EncryptionKey) if err != nil { @@ -385,7 +378,7 @@ func HandleWriteFile(w http.ResponseWriter, r *http.Request) { return } // 加密文件在同级目录下创建一个同名隐藏文件 - hiddenFilePath := filepath.Join(basePath, "."+filePath) + hiddenFilePath := filepath.Join(basePath, filepath.Dir(filePath), "."+filepath.Base(filePath)) _, err = os.Create(hiddenFilePath) if err != nil { libs.ErrorMsg(w, "创建隐藏文件失败") diff --git a/godo/files/os.go b/godo/files/os.go index 0de8c97..e8f2831 100644 --- a/godo/files/os.go +++ b/godo/files/os.go @@ -339,11 +339,14 @@ func CheckDeleteDesktop(filePath string) error { // 校验文件密码 func CheckFilePwd(fpwd, salt string) bool { + // 1. 对输入的密码进行哈希 pwd := libs.HashPassword(fpwd, salt) + // 2. 获取存储的密码哈希 oldpwd, err := libs.GetConfig("filePwd") if !err { return false } + // 3. 比对密码哈希 return oldpwd == pwd } @@ -367,10 +370,11 @@ func GetSalt(r *http.Request) string { } } -// 获取密码标识位,没有添加上 +// 获取加密标志 func GetPwdFlag() bool { isPwd, has := libs.GetConfig("isPwd") if !has { + // 默认不加密 req := libs.ReqBody{ Name: "isPwd", Value: false, @@ -382,8 +386,9 @@ func GetPwdFlag() bool { return isPwd.(bool) } -// 判读一个目录下有没有同名隐藏文件 +// 检查文件是否加密 func IsHaveHiddenFile(basePath, filePath string) bool { + // 通过查找同名隐藏文件判断 hiddenFilePath := filepath.Join(basePath, "."+filePath) _, err := os.Stat(hiddenFilePath) return err == nil diff --git a/godo/files/pwdfile.go b/godo/files/pwdfile.go index 37e14a1..546e340 100644 --- a/godo/files/pwdfile.go +++ b/godo/files/pwdfile.go @@ -6,7 +6,6 @@ import ( "godo/libs" "net/http" "strconv" - "strings" ) // 加密读 @@ -36,34 +35,31 @@ func HandleReadFile(w http.ResponseWriter, r *http.Request) { return } - // 没有加密 base64明文传输 + // 无加密情况 if !hasPwd { - data := string(fileContent) - if !strings.HasPrefix(data, "link::") { - data = base64.StdEncoding.EncodeToString(fileContent) - } + // 直接base64编码原文返回 + data := base64.StdEncoding.EncodeToString(fileContent) resp := libs.APIResponse{Message: "success", Data: data} json.NewEncoder(w).Encode(resp) return } - // 有加密,先校验密码,再解密 + // 加密情况 + // 1. 验证文件密码 if !CheckFilePwd(fPwd, salt) { libs.HTTPError(w, http.StatusBadRequest, "密码错误") return } + // 2. 解密文件内容 fileContent, err = libs.DecryptData(fileContent, libs.EncryptionKey) if err != nil { libs.HTTPError(w, http.StatusInternalServerError, err.Error()) return } - content := string(fileContent) - // 检查文件内容是否以"link::"开头 - if !strings.HasPrefix(content, "link::") { - content = base64.StdEncoding.EncodeToString(fileContent) - } + // 3. base64编码后返回 + content := base64.StdEncoding.EncodeToString(fileContent) // 初始响应 res := libs.APIResponse{Code: 0, Message: "success", Data: content} diff --git a/godo/libs/encode.go b/godo/libs/encode.go index 007b98a..30e3a10 100644 --- a/godo/libs/encode.go +++ b/godo/libs/encode.go @@ -35,6 +35,7 @@ func pkcs7Unpad(data []byte) []byte { return data[:length-padding] } +// 加密实现 func EncryptData(data []byte, key []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { @@ -66,7 +67,7 @@ func EncryptData(data []byte, key []byte) ([]byte, error) { return result, nil } -// DecryptData 使用 AES 解密数据,并验证 HMAC-SHA256 签名 +// 解密实现 func DecryptData(ciphertext []byte, key []byte) ([]byte, error) { // 检查 HMAC-SHA256 签名 expectedMacSize := sha256.Size