Golang-та қолтаңбаны қалыптастыру мысалы:

package main

import (
   "crypto/sha256"
   "encoding/base64"
   "encoding/json"
//Сауал денесі сауалға байланысты өзгереді

type Request struct {
   Agent       string `json:"agent"`
   Project     string `json:"project"`
   ServiceCode string `json:"service_code"`
}
//Мысал үшін secret 12345
const secret = "12345"

func main() {
   request := Request{
      Agent:       "tarlan",
      Project:     "mobile",
      ServiceCode: "101",
   }

   notSortedJson, err := json.Marshal(&request)
   if err != nil {
      panic(err)
   }

   var notSortedMap map[string]interface{}

   if err = json.Unmarshal(notSortedJson, &notSortedMap); err != nil {
      panic(err)
   }

   sortedJson, err := json.Marshal(&notSortedMap)
   if err != nil {
      panic(err)
   }

   signData := base64.StdEncoding.EncodeToString(sortedJson)

   sign := sha256.Sum256([]byte(signData + secret))

   fmt.Printf("Sign: %x", sign)
}

Last updated