Skip to content

New discards 25% of the random bits #23

@martin-sucha

Description

@martin-sucha

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions