-
-
Save luiz-otavio/9e87a26fb54f17ded7a65070b932df3a to your computer and use it in GitHub Desktop.
BCB PIX End To End Identification Generation in Golang.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment