package common import ( "context" "crypto/aes" "crypto/cipher" "crypto/md5" "encoding/hex" "fmt" "time" ) import ( "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v5" "github.com/spf13/cast" grpcmd "google.golang.org/grpc/metadata" ) var CstSh, _ = time.LoadLocation("Asia/Shanghai") const ( LocateDateFormat = "2006-01-02" LocateTimeFormat = "2006-01-02 15:04:05" LocateMilliFormat = "2006-01-02 15:04:05.9999" ) func Date() string { return time.Now().In(CstSh).Format(LocateDateFormat) } func Now() string { return time.Now().In(CstSh).Format(LocateTimeFormat) } func NowMilli() string { return time.Now().In(CstSh).Format(LocateMilliFormat) } func NewToken(userId uint, name string) (string, int64, error) { expire := time.Now().AddDate(0, 1, 0) hmacSampleSecret := []byte(BasicConfig.Jwt) token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "id": userId, "name": name, "exp": expire.Unix(), }) str, err := token.SignedString(hmacSampleSecret) return str, expire.Unix(), err } func ParseToken(tokenString string) (uint, string, error) { token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } return []byte(BasicConfig.Jwt), nil }) if err != nil { return 0, "", err } claims := token.Claims.(jwt.MapClaims) return cast.ToUint(claims["id"]), claims["name"].(string), nil } func GC2GM(c *gin.Context) context.Context { md := grpcmd.New(map[string]string{ "uid": cast.ToString(c.GetInt("uid")), "username": c.GetString("username"), "language": c.GetString("language"), "source": c.GetString("source"), "product": c.GetString("product"), }, ) return grpcmd.NewOutgoingContext(context.Background(), md) } func GetQueryString(c *gin.Context, key string) *string { if t, ok := c.GetQuery(key); ok { return &t } return nil } func IsEnabled(c *gin.Context) *bool { if isEnabled, ok := c.GetQuery("is_enabled"); ok { if r, e := cast.ToBoolE(isEnabled); e == nil { return &r } } return nil } func MD5(v []byte) string { h := md5.New() h.Write(v) re := h.Sum(nil) return hex.EncodeToString(re) } var dbPwKey = []byte("X3O6wVF&6*&lSVk0*504V~q7>\"k]6S'*") // 32 bytes for AES-256 var dbPwNonceHex = "1962a6f6f9999447632c8a34" func EncryptGCM(key []byte, nonce []byte, plaintext []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } gcm, err := cipher.NewGCM(block) if err != nil { return nil, err } ciphertext := gcm.Seal(nil, nonce, plaintext, nonce) return ciphertext, nil } func DecryptGCM(key []byte, nonce []byte, ciphertext []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } gcm, err := cipher.NewGCM(block) if err != nil { return nil, err } plaintext, err := gcm.Open(nil, nonce, ciphertext, nonce) if err != nil { return nil, err } return plaintext, nil } func DBPwdEncrypt(ciphertext []byte) ([]byte, error) { nonce, _ := hex.DecodeString(dbPwNonceHex) return EncryptGCM(dbPwKey, nonce, ciphertext) } func DBPwdDecrypt(ciphertext []byte) ([]byte, error) { nonce, _ := hex.DecodeString(dbPwNonceHex) return DecryptGCM(dbPwKey, nonce, ciphertext) }