| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- // Copyright (c) 2015 qianqiusoft.com
- // Licensed to You under the GNU Affero GPL v3
- // See the LICENSE file at git.qianqiusoft.com/qianqiusoft/light-vocation/LICENSE
- // http://www.gnu.org/licenses/why-affero-gpl.en.html
- package utils
- import (
- "crypto/md5"
- "encoding/base64"
- "encoding/hex"
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
- )
- type LoginCheck interface {
- AddPwdErrNum(string) bool
- CheckErrNum(string) bool
- }
- var globalLoginCheck LoginCheck
- func GetGlobalLoginCheck() LoginCheck {
- return globalLoginCheck
- }
- func SetGlobalLoginCheck(loginCheck LoginCheck) {
- globalLoginCheck = loginCheck
- }
- func HashPassword(password, salt string) string {
- h := md5.New()
- enableDbSalt := config.AppConfig.GetBool("enable_db_salt", true)
- customSalt := config.AppConfig.GetKey("password_salt")
- enableBase64 := config.AppConfig.GetBool("password_enable_base64", true)
- if !enableDbSalt {
- h.Write([]byte(password))
- if len(customSalt) > 0 {
- //fmt.Println(customSalt)
- md5Str := base64.StdEncoding.EncodeToString(h.Sum(nil))
- h.Reset()
- h.Write([]byte(md5Str + customSalt))
- }
- if enableBase64 {
- base64_str := base64.StdEncoding.EncodeToString(h.Sum(nil))
- logs.Debug("启用编码:", base64_str)
- return base64_str
- } else {
- str := hex.EncodeToString(h.Sum(nil))
- logs.Debug("未启用编码:", str)
- return str
- }
- } else {
- h.Write([]byte(password))
- md5Str := hex.EncodeToString(h.Sum(nil))
- logs.Debug("第一次加密:", md5Str)
- if len(salt) > 0 {
- h.Reset()
- h.Write([]byte(md5Str + salt))
- md5Str = hex.EncodeToString(h.Sum(nil))
- }
- logs.Debug("第二次加密:", md5Str)
- return md5Str
- }
- }
|