Browse Source

完善带密写接口

master
刘子旺 7 months ago
parent
commit
50ab4ff8b7
  1. 9
      godo/files/fs.go
  2. 9
      godo/files/os.go
  3. 20
      godo/files/pwdfile.go
  4. 3
      godo/libs/encode.go

9
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, "创建隐藏文件失败")

9
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

20
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}

3
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

Loading…
Cancel
Save