Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bc2532c

Browse files
author
陈弘桂
committedJul 11, 2018
fixe password encryption problem
1 parent 0f3d23c commit bc2532c

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed
 

‎admin.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ LOCK TABLES `goadmin_users` WRITE;
7777

7878
INSERT INTO `goadmin_users` (`id`, `username`, `password`, `name`, `avatar`, `remember_token`, `created_at`, `updated_at`)
7979
VALUES
80-
(1,'admin','8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918','admin',NULL,'tlNcBVK9AvfYH7WEnwB1RKvocJu8FfRy4um3DJtwdHuJy0dwFsLOgAc0xUfh','2018-05-13 10:00:33','2018-05-13 10:00:33');
80+
(1,'admin','$2a$10$YDoHIAPcGpa3/Pm0f5Q/HeAlhOaRUgL.eyF8Ne/Mc1dp9esEQEV5e','admin',NULL,'tlNcBVK9AvfYH7WEnwB1RKvocJu8FfRy4um3DJtwdHuJy0dwFsLOgAc0xUfh','2018-05-13 10:00:33','2018-05-13 10:00:33');
8181

8282
/*!40000 ALTER TABLE `goadmin_users` ENABLE KEYS */;
8383
UNLOCK TABLES;

‎auth/auth.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package auth
22

33
import (
4-
"crypto/sha256"
5-
"encoding/hex"
64
"github.com/valyala/fasthttp"
75
"goAdmin/connections/mysql"
86
"strconv"
97
"time"
8+
"golang.org/x/crypto/bcrypt"
109
)
1110

1211
func Check(password []byte, username string) (user User, ok bool) {
@@ -16,8 +15,7 @@ func Check(password []byte, username string) (user User, ok bool) {
1615
if len(admin) < 1 {
1716
ok = false
1817
} else {
19-
hashpwd := EncodePassword(password)
20-
if hashpwd == admin[0]["password"].(string) {
18+
if ComparePassword(password, admin[0]["password"].(string)) {
2119
ok = true
2220
user.ID = strconv.FormatInt(admin[0]["id"].(int64), 10)
2321
user.Level = "super"
@@ -31,11 +29,21 @@ func Check(password []byte, username string) (user User, ok bool) {
3129
return
3230
}
3331

32+
func ComparePassword(comPwd []byte, pwdHash string) bool {
33+
err := bcrypt.CompareHashAndPassword([]byte(pwdHash), comPwd)
34+
if err != nil {
35+
return false
36+
} else {
37+
return true
38+
}
39+
}
40+
3441
func EncodePassword(pwd []byte) string {
35-
hash := sha256.New()
36-
hash.Write(pwd)
37-
md := hash.Sum(nil)
38-
return hex.EncodeToString(md)
42+
hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.DefaultCost)
43+
if err != nil {
44+
return ""
45+
}
46+
return string(hash[:])
3947
}
4048

4149
func SetCookie(ctx *fasthttp.RequestCtx, user User) bool {

‎server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func NotFoundHandler(ctx *fasthttp.RequestCtx) {
143143

144144
defer controller.GlobalDeferHandler(ctx)
145145

146-
if !PathExist(string(ctx.Path())) {
146+
if !PathExist("./resources" + string(ctx.Path())) {
147147
ctx.SetStatusCode(fasthttp.StatusNotFound)
148148
ctx.SetContentType("application/json")
149149
ctx.WriteString(`{"code":404, "msg":"route not found"}`)

‎vendor/vendor.json

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@
112112
"revision": "ceec8f93295a060cdb565ec25e4ccf17941dbd55",
113113
"revisionTime": "2016-11-14T21:01:44Z"
114114
},
115+
{
116+
"path": "golang.org/x/crypto/bcrypt",
117+
"revision": ""
118+
},
115119
{
116120
"path": "golang.org/x/sys/unix",
117121
"revision": ""

0 commit comments

Comments
 (0)
Please sign in to comment.