package main

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha256"
	"encoding/gob"
	"encoding/hex"
	"fmt"
)

func main() {
	// 初始化数据
	data := map[string]string{
		"access-pop-timestrap": "cdd08ede-b836-44d2-8c10-b6abc0ac57aa",
		"access-pop-noncestr":  "abcabcabcabcabc1",
		"access-pop-token":     "4fb818df-af9e-4590-868f-52a6647cb91b9a6a7474-5891-4468-84fb-ccd0beef91a5",
	}

	// 生成 HMAC
	key := "cdd08ede-b836-44d2-8c10-b6abc0ac57aa"
	signature, err := generateHMAC(data, key)
	if err != nil {
		fmt.Println("Error generating HMAC:", err)
		return
	}
	fmt.Println("Signature:", signature)
}

// 生成HMAC SHA-256签名
func generateHMAC(data map[string]string, key string) (string, error) {
	serializedData, err := getSerializedBytes(data)
	if err != nil {
		return "", err
	}

	h := hmac.New(sha256.New, []byte(key))
	h.Write(serializedData)
	return hex.EncodeToString(h.Sum(nil)), nil
}

// 序列化数据
func getSerializedBytes(data map[string]string) ([]byte, error) {
	var b bytes.Buffer
	e := gob.NewEncoder(&b)
	err := e.Encode(data)
	if err != nil {
		return nil, err
	}
	return b.Bytes(), nil
}
