Skip to content

Instantly share code, notes, and snippets.

@arantesxyz
Created November 24, 2023 19:44
Show Gist options
  • Select an option

  • Save arantesxyz/f7677d481ed88dc7fd55fe5233effcf2 to your computer and use it in GitHub Desktop.

Select an option

Save arantesxyz/f7677d481ed88dc7fd55fe5233effcf2 to your computer and use it in GitHub Desktop.
Brazil's PIX CID Calculation in Golang
package main
import (
"fmt"
"strings"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
)
func hmacSha256(key []byte, data []byte) []byte {
r := hmac.New(sha256.New, key)
r.Write([]byte(data))
return r.Sum(nil)
}
func lowercaseHexadecimal(data []byte) string {
return strings.ToLower(hex.EncodeToString(data))
}
func main() {
var (
keyType string = "EMAIL"
key string = "[email protected]"
ownerTaxIdNumber string = "01234567890"
ownerName string = "John Doe"
ownerTradeName string = ""
participant string = "00000001"
branch string = "0001"
accountNumber string = "1"
accountType string = "TRAN"
)
requestId := "c1e63c6a-7c9f-4be0-8413-4729ffe3e5ea"
requestIdBytes, _ := hex.DecodeString(strings.ReplaceAll(requestId, "-", ""))
entryAttributes := fmt.Sprintf("%s&%s&%s&%s&%s&%s&%s&%s&%s", keyType, key, ownerTaxIdNumber, ownerName, ownerTradeName, participant, branch, accountNumber, accountType)
cidBytes := hmacSha256(requestIdBytes, []byte(entryAttributes))
cid := lowercaseHexadecimal(cidBytes)
fmt.Println("Request ID:", requestId)
fmt.Println("Entry Attributes:", entryAttributes)
fmt.Println("CID:", cid)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment