Commit 5a3713c3 by lichenggang

Merge branch 'master' of http://119.23.72.7/lic/web_log

parents 23ea8bab a157592d
Showing with 106 additions and 0 deletions
package code
import (
"crypto/rand"
"encoding/base64"
"fmt"
"regexp"
)
var userMap = make(map[string]string)
var sessionIdMap = make(map[string]string)
func init() {
userMap["zzl"] = "THhANTIwbSFAQCM="
userMap["lcg"] = "THhANTIwbSFAQCM="
userMap["zgj"] = "THhANTIwbSFAQCM="
userMap["ljm"] = "THhANTIwbSFAQCM="
userMap["opt"] = "THhANTIwbSFAQCM="
sessionIdMap["S1oI0nPch9PzJ0ClGEODdzbqcq85JK7YwnLesuTWayU="] = ""
}
/**
* 验证用户
* 密码加密解密
* 账号密码验证
*/
func VerifyUser(name string, pwd string) (result bool, msg string) {
// 先进行校验
if !UserNameVerify(name) || !UserPwdVerify(pwd) {
return false, "密码或用户名格式错误"
}
// 密码加密
pwd = EnPwdCode(pwd)
if mapPwd, ok := userMap[name]; ok {
if mapPwd == pwd {
token := GenerateRandomString(32)
fmt.Println(token)
sessionIdMap[token] = name
return ok, "存在用户,密码正确"
} else {
return !ok, "存在用户,密码不正确"
}
} else {
return !ok, "不存在用户"
}
}
// 验证sessionID
func VerifySessionID(sessionID string) (bool, string) {
if _, ok := sessionIdMap[sessionID]; ok {
return ok, fmt.Sprintf("sessionID: %s 存在", sessionID)
} else {
return !ok, fmt.Sprintf("sessionID: %s 不存在", sessionID)
}
}
// 删除sessionID
func DeleteSessionID(sessionID string) (bool, string) {
delete(sessionIdMap, sessionID)
return true, fmt.Sprintf("sessionID: %s 已删除", sessionID)
}
//加密base64
func EnPwdCode(pwd string) string {
return base64.StdEncoding.EncodeToString([]byte(pwd))
}
//解密base64
func DePwdCode(pwd string) string {
decoded, _ := base64.StdEncoding.DecodeString(pwd)
return string(decoded)
}
//用户名校验
func UserNameVerify(userName string) bool {
if ok, _ := regexp.MatchString("^[a-zA-Z0-9]{3,16}$", userName); !ok {
return false
}
return true
}
//密码校验
func UserPwdVerify(pwd string) bool {
if ok, _ := regexp.MatchString("^[a-zA-Z0-9!@#]{4,16}$", pwd); !ok {
return false
}
return true
}
//生成随机字符数组
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
//生成随机字符串
func GenerateRandomString(s int) string {
b, _ := GenerateRandomBytes(s)
return base64.URLEncoding.EncodeToString(b)
}
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