// 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/logs" "git.qianqiusoft.com/qianqiusoft/light-apiengine/config" ) func HashPassword(password, salt string) string { h := md5.New() enableDbSalt, _ := config.AppConfig.GetBool("System::EnableDBSalt", true) customSalt := config.AppConfig.GetKey("System::PasswordSalt") enableBase64, _ := config.AppConfig.GetBool("System::PasswordEnableBase64", 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 } }