Skip to content

Commit fad5f11

Browse files
authored
feat: 平台中创建的用户密码默认使用ssha加密策略 (#208)
1 parent 1699aba commit fad5f11

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repos:
66
- id: trailing-whitespace
77
- id: check-added-large-files
88
- repo: https://github.com/golangci/golangci-lint # golangci-lint hook repo
9-
rev: v1.47.3 # golangci-lint hook repo revision
9+
rev: v1.52.2 # golangci-lint hook repo revision
1010
hooks:
1111
- id: golangci-lint
1212
name: golangci-lint

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
<a href="https://github.com/ckyoung123421">
162162
<img src="https://avatars.githubusercontent.com/u/16368382?v=4" width="100;" alt="ckyoung123421"/>
163163
<br />
164-
<sub><b>Null</b></sub>
164+
<sub><b>ckyoung123421</b></sub>
165165
</a>
166166
</td></tr>
167167
<tr>

public/tools/ssha.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package tools
2+
3+
import (
4+
"bytes"
5+
"crypto/sha1"
6+
"encoding/base64"
7+
"fmt"
8+
"math/rand"
9+
)
10+
11+
// code from https://gist.github.com/newm4n/ce9ac4308ae1beb4250efddad28e3f85
12+
13+
// Encode encodes the []byte of raw password
14+
func EncodePass(rawPassPhrase []byte) string {
15+
hash := makeSSHAHash(rawPassPhrase, makeSalt())
16+
b64 := base64.StdEncoding.EncodeToString(hash)
17+
return string([]byte(fmt.Sprintf("{SSHA}%s", b64)))
18+
}
19+
20+
// makeSalt make a 4 byte array containing random bytes.
21+
func makeSalt() []byte {
22+
sbytes := make([]byte, 4)
23+
rand.Read(sbytes)
24+
return sbytes
25+
}
26+
27+
// makeSSHAHash make hasing using SHA-1 with salt. This is not the final output though. You need to append {SSHA} string with base64 of this hash.
28+
func makeSSHAHash(passphrase, salt []byte) []byte {
29+
sha := sha1.New()
30+
sha.Write(passphrase)
31+
sha.Write(salt)
32+
33+
h := sha.Sum(nil)
34+
return append(h, salt...)
35+
}
36+
37+
// Matches matches the encoded password and the raw password
38+
func Matches(encodedPassPhrase, rawPassPhrase []byte) bool {
39+
//strip the {SSHA}
40+
eppS := string(encodedPassPhrase)[6:]
41+
hash, err := base64.StdEncoding.DecodeString(eppS)
42+
if err != nil {
43+
return false
44+
}
45+
salt := hash[len(hash)-4:]
46+
47+
sha := sha1.New()
48+
sha.Write(rawPassPhrase)
49+
sha.Write(salt)
50+
sum := sha.Sum(nil)
51+
52+
return bytes.Equal(sum, hash[:len(hash)-4])
53+
}

public/tools/util_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@ func TestSliceToString(t *testing.T) {
2525
a := []uint{1}
2626
fmt.Printf("%s\n", SliceToString(a, ","))
2727
}
28+
29+
func TestEncodePass(t *testing.T) {
30+
// to encode a password into ssha
31+
hashed := EncodePass([]byte("testpass"))
32+
fmt.Println(string(hashed))
33+
// to validate a password against saved hash.
34+
if Matches([]byte(hashed), []byte("testpass")) {
35+
fmt.Println("Its a match.")
36+
} else {
37+
fmt.Println("its not match")
38+
}
39+
}

service/ildap/user_ildap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (x UserService) Add(user *model.User) error {
2929
add.Attribute("postalAddress", []string{user.PostalAddress})
3030
add.Attribute("mobile", []string{user.Mobile})
3131
add.Attribute("uid", []string{user.Username})
32-
add.Attribute("userPassword", []string{tools.NewParPasswd(user.Password)})
32+
add.Attribute("userPassword", []string{tools.EncodePass([]byte(tools.NewParPasswd(user.Password)))})
3333

3434
// 获取 LDAP 连接
3535
conn, err := common.GetLDAPConn()

0 commit comments

Comments
 (0)