pwd.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) 2015 qianqiusoft.com
  2. // Licensed to You under the GNU Affero GPL v3
  3. // See the LICENSE file at git.qianqiusoft.com/qianqiusoft/light-vocation/LICENSE
  4. // http://www.gnu.org/licenses/why-affero-gpl.en.html
  5. package utils
  6. import (
  7. "crypto/md5"
  8. "encoding/base64"
  9. "encoding/hex"
  10. "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
  11. "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
  12. )
  13. func HashPassword(password, salt string) string {
  14. h := md5.New()
  15. enableDbSalt := config.AppConfig.GetBool("System::EnableDBSalt", true)
  16. customSalt := config.AppConfig.GetKey("System::PasswordSalt")
  17. enableBase64 := config.AppConfig.GetBool("System::PasswordEnableBase64", true)
  18. if !enableDbSalt {
  19. h.Write([]byte(password))
  20. if len(customSalt) > 0 {
  21. //fmt.Println(customSalt)
  22. md5Str := base64.StdEncoding.EncodeToString(h.Sum(nil))
  23. h.Reset()
  24. h.Write([]byte(md5Str + customSalt))
  25. }
  26. if enableBase64 {
  27. base64_str := base64.StdEncoding.EncodeToString(h.Sum(nil))
  28. logs.Debug("启用编码:", base64_str)
  29. return base64_str
  30. } else {
  31. str := hex.EncodeToString(h.Sum(nil))
  32. logs.Debug("未启用编码:", str)
  33. return str
  34. }
  35. } else {
  36. h.Write([]byte(password))
  37. md5Str := hex.EncodeToString(h.Sum(nil))
  38. logs.Debug("第一次加密:", md5Str)
  39. if len(salt) > 0 {
  40. h.Reset()
  41. h.Write([]byte(md5Str + salt))
  42. md5Str = hex.EncodeToString(h.Sum(nil))
  43. }
  44. logs.Debug("第二次加密:", md5Str)
  45. return md5Str
  46. }
  47. }