完成数据库敏感信息的加密 close #127 close #46

This commit is contained in:
dushixiang
2021-04-17 20:00:02 +08:00
parent bceda9a95c
commit 8f22ecfb76
5 changed files with 18 additions and 71 deletions

View File

@ -1,11 +1,8 @@
package service
import (
"encoding/base64"
"next-terminal/pkg/global"
"next-terminal/server/repository"
"next-terminal/server/utils"
)
type AssetService struct {
@ -26,31 +23,10 @@ func (r AssetService) Encrypt() error {
if item.Encrypted {
continue
}
if item.Password != "" && item.Password != "-" {
encryptedCBC, err := utils.AesEncryptCBC([]byte(item.Password), global.Config.EncryptionPassword)
if err != nil {
return err
}
item.Password = base64.StdEncoding.EncodeToString(encryptedCBC)
if err := r.assetRepository.Encrypt(&item, global.Config.EncryptionPassword); err != nil {
return err
}
if item.PrivateKey != "" && item.PrivateKey != "-" {
encryptedCBC, err := utils.AesEncryptCBC([]byte(item.PrivateKey), global.Config.EncryptionPassword)
if err != nil {
return err
}
item.PrivateKey = base64.StdEncoding.EncodeToString(encryptedCBC)
}
if item.Passphrase != "" && item.Passphrase != "-" {
encryptedCBC, err := utils.AesEncryptCBC([]byte(item.Passphrase), global.Config.EncryptionPassword)
if err != nil {
return err
}
item.Passphrase = base64.StdEncoding.EncodeToString(encryptedCBC)
}
err = r.assetRepository.EncryptedById(true, item.Password, item.PrivateKey, item.Passphrase, item.ID)
if err != nil {
if err := r.assetRepository.UpdateById(&item, item.ID); err != nil {
return err
}
}