Skip to content

Commit 523186f

Browse files
authored
feat: Support sha512 password encryption algorithm (casdoor#2657)
* add sha512 encryption support for password * fead: add sha512 encryption support for password
1 parent ef373ca commit 523186f

File tree

7 files changed

+57
-5
lines changed

7 files changed

+57
-5
lines changed

cred/manager.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func GetCredManager(passwordType string) CredManager {
2424
return NewPlainCredManager()
2525
} else if passwordType == "salt" {
2626
return NewSha256SaltCredManager()
27+
} else if passwordType == "sha512-salt" {
28+
return NewSha512SaltCredManager()
2729
} else if passwordType == "md5-salt" {
2830
return NewMd5UserSaltCredManager()
2931
} else if passwordType == "bcrypt" {

cred/sha512-salt.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2024 The Casdoor Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cred
16+
17+
import (
18+
"crypto/sha512"
19+
"encoding/hex"
20+
)
21+
22+
type Sha512SaltCredManager struct{}
23+
24+
func getSha512(data []byte) []byte {
25+
hash := sha512.Sum512(data)
26+
return hash[:]
27+
}
28+
29+
func getSha512HexDigest(s string) string {
30+
b := getSha512([]byte(s))
31+
res := hex.EncodeToString(b)
32+
return res
33+
}
34+
35+
func NewSha512SaltCredManager() *Sha512SaltCredManager {
36+
cm := &Sha512SaltCredManager{}
37+
return cm
38+
}
39+
40+
func (cm *Sha512SaltCredManager) GetHashedPassword(password string, userSalt string, organizationSalt string) string {
41+
res := getSha512HexDigest(password)
42+
if organizationSalt != "" {
43+
res = getSha512HexDigest(res + organizationSalt)
44+
}
45+
return res
46+
}
47+
48+
func (cm *Sha512SaltCredManager) IsPasswordCorrect(plainPwd string, hashedPwd string, userSalt string, organizationSalt string) bool {
49+
return hashedPwd == cm.GetHashedPassword(plainPwd, userSalt, organizationSalt)
50+
}

object/adapter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Adapter struct {
3737
Host string `xorm:"varchar(100)" json:"host"`
3838
Port int `json:"port"`
3939
User string `xorm:"varchar(100)" json:"user"`
40-
Password string `xorm:"varchar(100)" json:"password"`
40+
Password string `xorm:"varchar(150)" json:"password"`
4141
Database string `xorm:"varchar(100)" json:"database"`
4242

4343
*xormadapter.Adapter `xorm:"-" json:"-"`

object/syncer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Syncer struct {
4343
Host string `xorm:"varchar(100)" json:"host"`
4444
Port int `json:"port"`
4545
User string `xorm:"varchar(100)" json:"user"`
46-
Password string `xorm:"varchar(100)" json:"password"`
46+
Password string `xorm:"varchar(150)" json:"password"`
4747
Database string `xorm:"varchar(100)" json:"database"`
4848
Table string `xorm:"varchar(100)" json:"table"`
4949
TableColumns []*TableColumn `xorm:"mediumtext" json:"tableColumns"`

object/token_jwt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type UserWithoutThirdIdp struct {
5252

5353
Id string `xorm:"varchar(100) index" json:"id"`
5454
Type string `xorm:"varchar(100)" json:"type"`
55-
Password string `xorm:"varchar(100)" json:"password"`
55+
Password string `xorm:"varchar(150)" json:"password"`
5656
PasswordSalt string `xorm:"varchar(100)" json:"passwordSalt"`
5757
PasswordType string `xorm:"varchar(100)" json:"passwordType"`
5858
DisplayName string `xorm:"varchar(100)" json:"displayName"`

object/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type User struct {
5454
Id string `xorm:"varchar(100) index" json:"id"`
5555
ExternalId string `xorm:"varchar(100) index" json:"externalId"`
5656
Type string `xorm:"varchar(100)" json:"type"`
57-
Password string `xorm:"varchar(100)" json:"password"`
57+
Password string `xorm:"varchar(150)" json:"password"`
5858
PasswordSalt string `xorm:"varchar(100)" json:"passwordSalt"`
5959
PasswordType string `xorm:"varchar(100)" json:"passwordType"`
6060
DisplayName string `xorm:"varchar(100)" json:"displayName"`

web/src/OrganizationEditPage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class OrganizationEditPage extends React.Component {
184184
</Col>
185185
<Col span={22} >
186186
<Select virtual={false} style={{width: "100%"}} value={this.state.organization.passwordType} onChange={(value => {this.updateOrganizationField("passwordType", value);})}
187-
options={["plain", "salt", "md5-salt", "bcrypt", "pbkdf2-salt", "argon2id"].map(item => Setting.getOption(item, item))}
187+
options={["plain", "salt", "sha512-salt", "md5-salt", "bcrypt", "pbkdf2-salt", "argon2id"].map(item => Setting.getOption(item, item))}
188188
/>
189189
</Col>
190190
</Row>

0 commit comments

Comments
 (0)