utils.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package common
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "fmt"
  8. "time"
  9. )
  10. import (
  11. "github.com/golang-jwt/jwt/v5"
  12. "github.com/spf13/cast"
  13. )
  14. var CstSh, _ = time.LoadLocation("Asia/Shanghai")
  15. const (
  16. LocateDateFormat = "2006-01-02"
  17. LocateTimeFormat = "2006-01-02 15:04:05"
  18. LocateMilliFormat = "2006-01-02 15:04:05.9999"
  19. )
  20. func Date() string {
  21. return time.Now().In(CstSh).Format(LocateDateFormat)
  22. }
  23. func Now() string {
  24. return time.Now().In(CstSh).Format(LocateTimeFormat)
  25. }
  26. func NowMilli() string {
  27. return time.Now().In(CstSh).Format(LocateMilliFormat)
  28. }
  29. func NewToken(userId uint, name string) (string, int64, error) {
  30. expire := time.Now().AddDate(0, 1, 0)
  31. hmacSampleSecret := []byte(BasicConfig.Jwt)
  32. token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
  33. "id": userId,
  34. "name": name,
  35. "exp": expire.Unix(),
  36. })
  37. str, err := token.SignedString(hmacSampleSecret)
  38. return str, expire.Unix(), err
  39. }
  40. func ParseToken(tokenString string) (uint, string, error) {
  41. token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
  42. if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
  43. return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
  44. }
  45. return []byte(BasicConfig.Jwt), nil
  46. })
  47. if err != nil {
  48. return 0, "", err
  49. }
  50. claims := token.Claims.(jwt.MapClaims)
  51. return cast.ToUint(claims["id"]), claims["name"].(string), nil
  52. }
  53. func MD5(v []byte) string {
  54. h := md5.New()
  55. h.Write(v)
  56. re := h.Sum(nil)
  57. return hex.EncodeToString(re)
  58. }
  59. var dbPwKey = []byte("X3O6wVF&6*&lSVk0*504V~q7>\"k]6S'*") // 32 bytes for AES-256
  60. var dbPwNonceHex = "1962a6f6f9999447632c8a34"
  61. func EncryptGCM(key []byte, nonce []byte, plaintext []byte) ([]byte, error) {
  62. block, err := aes.NewCipher(key)
  63. if err != nil {
  64. return nil, err
  65. }
  66. gcm, err := cipher.NewGCM(block)
  67. if err != nil {
  68. return nil, err
  69. }
  70. ciphertext := gcm.Seal(nil, nonce, plaintext, nonce)
  71. return ciphertext, nil
  72. }
  73. func DecryptGCM(key []byte, nonce []byte, ciphertext []byte) ([]byte, error) {
  74. block, err := aes.NewCipher(key)
  75. if err != nil {
  76. return nil, err
  77. }
  78. gcm, err := cipher.NewGCM(block)
  79. if err != nil {
  80. return nil, err
  81. }
  82. plaintext, err := gcm.Open(nil, nonce, ciphertext, nonce)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return plaintext, nil
  87. }
  88. func DBPwdEncrypt(ciphertext []byte) ([]byte, error) {
  89. nonce, _ := hex.DecodeString(dbPwNonceHex)
  90. return EncryptGCM(dbPwKey, nonce, ciphertext)
  91. }
  92. func DBPwdDecrypt(ciphertext []byte) ([]byte, error) {
  93. nonce, _ := hex.DecodeString(dbPwNonceHex)
  94. return DecryptGCM(dbPwKey, nonce, ciphertext)
  95. }