password.go 784 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package password
  2. import (
  3. "crypto/hmac"
  4. "crypto/md5"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "fmt"
  8. )
  9. const (
  10. saltPassword = "qkhPAGA13HocW3GAEWwb"
  11. defaultPassword = "123456"
  12. )
  13. func GeneratePassword(str string) (password string) {
  14. // md5
  15. m := md5.New()
  16. m.Write([]byte(str))
  17. mByte := m.Sum(nil)
  18. // hmac
  19. h := hmac.New(sha256.New, []byte(saltPassword))
  20. h.Write(mByte)
  21. password = hex.EncodeToString(h.Sum(nil))
  22. return
  23. }
  24. func ResetPassword() (password string) {
  25. m := md5.New()
  26. m.Write([]byte(defaultPassword))
  27. mStr := hex.EncodeToString(m.Sum(nil))
  28. password = GeneratePassword(mStr)
  29. return
  30. }
  31. func GenerateLoginToken(id int32) (token string) {
  32. m := md5.New()
  33. m.Write([]byte(fmt.Sprintf("%d%s", id, saltPassword)))
  34. token = hex.EncodeToString(m.Sum(nil))
  35. return
  36. }