Skip to content

Instantly share code, notes, and snippets.

@arantesxyz
Last active June 18, 2025 22:26
Show Gist options
  • Select an option

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

Select an option

Save arantesxyz/b78f00ac3589f93deeefa331e1f1fdb3 to your computer and use it in GitHub Desktop.
BCB PIX End To End Identification Generation in Golang.
package helpers
import (
"fmt"
"math/rand"
"time"
"unsafe"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
const (
letterIdxBits = 6
letterIdxMask = 1<<letterIdxBits - 1
letterIdxMax = 63 / letterIdxBits
)
var src = rand.NewSource(time.Now().UnixNano())
// Receive a transaction type and ISPB and returns a 32 characters string.
// TransactionType should contain a single character.
// ISPB should be 8 characters.
func GenerateE2E(transactionType string, ispb string) (string) {
location := time.FixedZone("GMT-3", -3*60*60)
date := time.Now().In(location).Format("200601021504")
randString := randString(11)
return fmt.Sprintf("%s%s%s%s", transactionType, ispb, date, randString)
}
func randString(n int) string {
b := make([]byte, n)
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return *(*string)(unsafe.Pointer(&b))
}
@arantesxyz
Copy link
Copy Markdown
Author

As mesmas instruções podem ser utilizadas para gerar o MessageIdentification MsgId, apenas a data e hora não são obrigatórios, mas não fazem diferença, BCB só irá validar se é único.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment