added base62

This commit is contained in:
2022-10-27 17:55:27 +02:00
parent a694f36f46
commit c223e2f0fa
2 changed files with 74 additions and 23 deletions

View File

@@ -3,8 +3,6 @@ package langext
import (
"crypto/rand"
"io"
"math"
"math/big"
)
func RandBytes(size int) []byte {
@@ -15,24 +13,3 @@ func RandBytes(size int) []byte {
}
return b
}
func RandBase62(rlen int) string {
ecs := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
bi52 := big.NewInt(int64(len(ecs)))
randMax := big.NewInt(math.MaxInt64)
r := ""
for i := 0; i < rlen; i++ {
v, err := rand.Int(rand.Reader, randMax)
if err != nil {
panic(err)
}
r += string(ecs[v.Mod(v, bi52).Int64()])
}
return r
}