gravitl / netmaker

Netmaker makes networks with WireGuard. Netmaker automates fast, secure, and distributed virtual networks.
https://netmaker.io
Other
9.52k stars 552 forks source link

Biased Random String #2244

Closed pyinthesky closed 1 year ago

pyinthesky commented 1 year ago

https://github.com/gravitl/netmaker/blob/1b62af180a7b489b37672a99efbf8a56d4a9c808/netclient/ncutils/netclientutils.go#L44

The function as written is modulo biased where the first 8 characters in the list of validChars have a higher probability of being in the random string than the latter ones.

Proposed change:

func MakeRandomString(n int) string {
    const validChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

    result := make([]byte, n)

    for x = 0; x < n; x++ {
        result = validChars[rand.Intn(len(validChars))]
    }
    return string(result)
}
mattkasun commented 1 year ago

fixed in v0.19.0