package main

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"sort"
	"strings"
)

func main() {
	data := make(map[string]string)
	data["REQ_ACCESS_POP_TOKEN"] = "4fb818df-af9e-4590-868f-52a6647cb91b9a6a7474-5891-4468-84fb-ccd0beef91a5"
	data["REQ_ACCESS_POP_TIMESTRAP"] = "cdd08ede-b836-44d2-8c10-b6abc0ac57aa"
	data["REQ_ACCESS_POP_NONCESTR"] = "abcabcabcabcabc1"

	fmt.Println("Data Map:", data)

	serializedData := serializeData(data)
	fmt.Println("Serialized Data:", serializedData)

	key := "cdd08ede-b836-44d2-8c10-b6abc0ac57aa"
	signature := genHMAC(serializedData, key)
	fmt.Println("Signature:", signature)
}

func genHMAC(data string, key string) string {
	h := hmac.New(sha256.New, []byte(key))
	h.Write([]byte(data))
	signature := h.Sum(nil)
	fmt.Printf("HMAC SHA256: %x\n", signature)
	return hex.EncodeToString(signature)
}

func serializeData(data map[string]string) string {
	keys := make([]string, 0, len(data))
	for k := range data {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	var sb strings.Builder
	for _, k := range keys {
		sb.WriteString(k)
		sb.WriteString(data[k])
	}
	return sb.String()
}
