Skip to content

Commit

Permalink
feat: 强制切换到 argon2id
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Aug 14, 2024
1 parent d37c88d commit 2062063
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 57 deletions.
1 change: 0 additions & 1 deletion backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func SetDefault() {
viper.SetDefault("mq.redis.hash.post_publish_status", "campux_post_publish_status")
viper.SetDefault("mq.redis.prefix.oauth2_code", "campux_oauth2_code")

viper.SetDefault("experimental.password.hash.argon", true)
}

// 创建配置文件对象
Expand Down
5 changes: 2 additions & 3 deletions backend/database/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (m *MongoDBManager) GetAccountByUIN(uin int64) (*AccountPO, error) {
return &acc, nil
}

func (m *MongoDBManager) UpdatePassword(uin int64, pwd, salt string) error {
func (m *MongoDBManager) UpdatePassword(uin int64, pwd string) error {

// 更新
_, err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(ACCOUNT_COLLECTION).UpdateOne(
Expand All @@ -178,8 +178,7 @@ func (m *MongoDBManager) UpdatePassword(uin int64, pwd, salt string) error {
},
bson.M{
"$set": bson.M{
"pwd": pwd,
"salt": salt,
"pwd": pwd,
},
},
)
Expand Down
3 changes: 1 addition & 2 deletions backend/database/po.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ type AccountPO struct {
Uin int64 `json:"uin" bson:"uin"` // QQ号
Pwd string `json:"pwd" bson:"pwd"` // 数据库存md5之后的密码
CreatedAt time.Time `json:"created_at" bson:"created_at"` // CST时间
UserGroup UserGroup `json:"user_group" bson:"user_group"` // 用户组
Salt string `json:"salt" bson:"salt"` // 加盐
UserGroup UserGroup `json:"user_group" bson:"user_group"` // 用户
}

type AccountExpose struct {
Expand Down
52 changes: 17 additions & 35 deletions backend/service/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"errors"
"github.com/spf13/viper"
"time"

"github.com/RockChinQ/Campux/backend/database"
Expand Down Expand Up @@ -35,22 +34,16 @@ func (as *AccountService) CreateAccount(uin int64) (string, error) {
return "", ErrAccountAlreadyExist
} else {
initPwd := util.GenerateRandomPassword()
salt := util.GenerateRandomSalt()

var pwdHash string
if viper.GetBool(`experimental.password.hash.argon`) {
pwdHash, err = util.CreateHash(initPwd, util.DefaultParams)
if err != nil {
return "", err
}
} else {
pwdHash = util.EncryptPassword(initPwd, salt)
pwdHash, err = util.CreateHash(initPwd, util.DefaultParams)
if err != nil {
return "", err
}
acc := &database.AccountPO{
Uin: uin,
Pwd: pwdHash,
UserGroup: database.USER_GROUP_USER,
Salt: salt,
CreatedAt: util.GetCSTTime(),
}

Expand All @@ -72,13 +65,13 @@ func (as *AccountService) CheckAccount(uin int64, pwd string) (string, error) {
}

var valid bool
if viper.GetBool(`experimental.password.hash.argon`) {
valid, err = util.ComparePasswordAndHash(pwd, acc.Pwd)
if err != nil {
return "", err
valid, err = util.ComparePasswordAndHash(pwd, acc.Pwd)
if err != nil {
if err == util.ErrInvalidHash {
return "", errors.New("hash 算法已更改,请重置密码")
}
} else {
valid = acc.Pwd == util.EncryptPassword(pwd, acc.Salt)

return "", err
}

if !valid {
Expand All @@ -104,21 +97,16 @@ func (as *AccountService) ResetPassword(uin int64) (string, error) {

// 生成新密码
newPwd := util.GenerateRandomPassword()
salt := util.GenerateRandomSalt()

var encryptedPwd string

if viper.GetBool(`experimental.password.hash.argon`) {
encryptedPwd, err = util.CreateHash(newPwd, util.DefaultParams)
if err != nil {
return "", err
}
} else {
encryptedPwd = util.EncryptPassword(newPwd, salt)
encryptedPwd, err = util.CreateHash(newPwd, util.DefaultParams)
if err != nil {
return "", err
}

// 更新密码
err = as.DB.UpdatePassword(uin, encryptedPwd, salt)
err = as.DB.UpdatePassword(uin, encryptedPwd)

return newPwd, err
}
Expand All @@ -135,20 +123,14 @@ func (as *AccountService) ChangePassword(uin int64, newPwd string) error {
return ErrAccountNotFound
}

salt := util.GenerateRandomSalt()

var encryptedPwd string
if viper.GetBool(`experimental.password.hash.argon`) {
encryptedPwd, err = util.CreateHash(newPwd, util.DefaultParams)
if err != nil {
return err
}
} else {
encryptedPwd = util.EncryptPassword(newPwd, salt)
encryptedPwd, err = util.CreateHash(newPwd, util.DefaultParams)
if err != nil {
return err
}

// 更新密码
err = as.DB.UpdatePassword(uin, encryptedPwd, salt)
err = as.DB.UpdatePassword(uin, encryptedPwd)

return err
}
Expand Down
16 changes: 0 additions & 16 deletions backend/util/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,3 @@ func GenerateRandomPassword() string {

return string(b)
}

// 随机生成一个包含小写字母和数字的字符串,长度为16
// 用于生成salt
func GenerateRandomSalt() string {
const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
b := make([]byte, 16)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}

// 计算密码的md5值
func EncryptPassword(password, salt string) string {
return MD5(password + salt)
}

0 comments on commit 2062063

Please sign in to comment.