Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New discards 25% of the random bits #23

Open
martin-sucha opened this issue May 11, 2021 · 0 comments
Open

New discards 25% of the random bits #23

martin-sucha opened this issue May 11, 2021 · 0 comments

Comments

@martin-sucha
Copy link

martin-sucha commented May 11, 2021

New uses bits 0-6 of each random byte (masks with 63) and the two high random bits are discarded.

I think it would be better not to discard randomness.

go-nanoid/gonanoid.go

Lines 89 to 97 in 0cc1b25

bytes := make([]byte, size)
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
id := make([]rune, size)
for i := 0; i < size; i++ {
id[i] = defaultAlphabet[bytes[i]&63]
}

It is possible to write a loop and output 4 characters per each 3 random bytes and handle the remainder separately.

Or maybe reuse the base64 encoding code from standard library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant