| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- // 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"
- "github.com/astaxie/beego"
- )
- func HashPassword(password, salt string) string {
- h := md5.New()
- enableDbSalt, _ := NewConfig().Bool("System::EnableDBSalt", true) //beego.AppConfig.Bool("System::EnableDBSalt")
- customSalt := NewConfig().String("System::PasswordSalt") //beego.AppConfig.String("System::PasswordSalt")
- enableBase64, _ := NewConfig().Bool("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 beego.AppConfig.String("System::PasswordEnableBase64") == "true" {
- if enableBase64 {
- base64_str := base64.StdEncoding.EncodeToString(h.Sum(nil))
- beego.Debug("启用编码:", base64_str)
- return base64_str
- } else {
- str := hex.EncodeToString(h.Sum(nil))
- beego.Debug("未启用编码:", str)
- return str
- }
- } else {
- h.Write([]byte(password))
- md5Str := hex.EncodeToString(h.Sum(nil))
- beego.Debug("第一次加密:", md5Str)
- if len(salt) > 0 {
- h.Reset()
- h.Write([]byte(md5Str + salt))
- md5Str = hex.EncodeToString(h.Sum(nil))
- }
- beego.Debug("第二次加密:", md5Str)
- return md5Str
- }
- }
|