// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors // // SPDX-License-Identifier: MIT package mail import ( "crypto/rand" "encoding/binary" "strings" ) // Range of characters for the secure string generation const cr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-" // Bitmask sizes for the string generators (based on 93 chars total) // // These constants define bitmask-related values used for efficient random string generation. // The bitmask operates over 66 possible characters, and the constants help determine the // number of bits and indices used in the process. const ( // letterIdxBits: Number of bits needed to represent a letter index. We have 64 possible characters // which fit into 6 bits. letterIdxBits = 6 // letterIdxMask: Bitmask to extract letter indices (all 1-bits for letterIdxBits). letterIdxMask = 1<= 0; { if rest == 0 { _, err = rand.Read(randPool) if err != nil { return randString.String(), err } char, rest = binary.BigEndian.Uint64(randPool), letterIdxMax } if i := int(char & letterIdxMask); i < charRangeLength { randString.WriteByte(cr[i]) idx-- } char >>= letterIdxBits rest-- } return randString.String(), nil }