Commit 6c742843 by 陈森彬

更改为MD5加密,并从配置文件读取用户名和密码

parent f87c8290
Showing with 87 additions and 7 deletions
package code package code
import ( import (
"crypto/md5"
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"encoding/json"
"fmt" "fmt"
"io"
"io/ioutil"
"log"
"regexp" "regexp"
"sync"
) )
var userMap = make(map[string]string) //var userMap = make(map[string]string)
var sessionIdMap = make(map[string]string) var sessionIdMap = make(map[string]string)
var configPath string = "E:\\gotest\\test.json"
type Configs map[string]json.RawMessage
type MainConfig struct {
}
var conf *MainConfig
var confs Configs
var instanceOnce sync.Once
func init() { func init() {
userMap["weblog"] = "MTIzNDU2" //userMap["weblog"] = "MTIzNDU2"
sessionIdMap["S1oI0nPch9PzJ0ClGEODdzbqcq85JK7YwnLesuTWayU="] = "" sessionIdMap["S1oI0nPch9PzJ0ClGEODdzbqcq85JK7YwnLesuTWayU="] = ""
} }
//初始化读取配置文件
func Init(path string) *MainConfig {
if conf != nil && path != configPath {
log.Printf("the config is already initialized, oldPath=%s, path=%s", configPath, path)
}
instanceOnce.Do(func() {
allConfigs, mainConfig := LoadConfig(path)
configPath = path
conf = mainConfig
confs = allConfigs
})
return conf
}
//获取配置文件路径
func ConfigPath() string {
return configPath
}
/** /**
* 验证用户 * 验证用户
* 密码加密解密 * 密码加密解密
...@@ -22,15 +58,18 @@ func init() { ...@@ -22,15 +58,18 @@ func init() {
*/ */
func VerifyUser(name string, pwd string) (result bool, msg string) { func VerifyUser(name string, pwd string) (result bool, msg string) {
// 先进行校验 // 先进行校验
if !UserNameVerify(name) || !UserPwdVerify(pwd) { if !UserNameVerify(name) || !UserPwdVerify(pwd) {
return false, "密码或用户名格式错误" return false, "密码或用户名格式错误"
} }
// 密码加密 path := ConfigPath()
pwd = EnPwdCode(pwd) //初始化配置文件
if mapPwd, ok := userMap[name]; ok { Init(path)
if mapPwd == pwd { // 密码md5加密
pwd = EnMD5PwdCode(pwd)
if mapPwd, ok := confs[name]; ok {
if CutWord(string(mapPwd)) == pwd {
token := GenerateRandomString(32) token := GenerateRandomString(32)
fmt.Println(token)
sessionIdMap[token] = name sessionIdMap[token] = name
return true, token return true, token
} else { } else {
...@@ -61,6 +100,17 @@ func EnPwdCode(pwd string) string { ...@@ -61,6 +100,17 @@ func EnPwdCode(pwd string) string {
return base64.StdEncoding.EncodeToString([]byte(pwd)) return base64.StdEncoding.EncodeToString([]byte(pwd))
} }
//加密md5
func EnMD5PwdCode(str string) string {
m := md5.New()
_, err := io.WriteString(m, str)
if err != nil {
log.Fatal(err)
}
arr := m.Sum(nil)
return fmt.Sprintf("%x", arr)
}
//解密base64 //解密base64
func DePwdCode(pwd string) string { func DePwdCode(pwd string) string {
decoded, _ := base64.StdEncoding.DecodeString(pwd) decoded, _ := base64.StdEncoding.DecodeString(pwd)
...@@ -100,3 +150,33 @@ func GenerateRandomString(s int) string { ...@@ -100,3 +150,33 @@ func GenerateRandomString(s int) string {
b, _ := GenerateRandomBytes(s) b, _ := GenerateRandomBytes(s)
return base64.URLEncoding.EncodeToString(b) return base64.URLEncoding.EncodeToString(b)
} }
//去掉双引号
func CutWord(s string) string {
if len(s) >= 2 {
if c := s[len(s)-1]; s[0] == c && (c == '"' || c == '\'') {
return s[1 : len(s)-1]
}
}
return s
}
//从配置文件中载入json字符串
func LoadConfig(path string) (Configs, *MainConfig) {
buf, err := ioutil.ReadFile(path)
if err != nil {
log.Panicln("load config conf failed: ", err)
}
mainConfig := &MainConfig{}
err = json.Unmarshal(buf, mainConfig)
if err != nil {
log.Panicln("decode config file failed:", string(buf), err)
}
allConfigs := make(Configs, 0)
err = json.Unmarshal(buf, &allConfigs)
if err != nil {
log.Panicln("decode config file failed:", string(buf), err)
}
return allConfigs, mainConfig
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment