Accoding to the comment from dchapes, the pseudorandom number generator should not be re-seeded on each use, nor re-seed the global PRNG in a package. Otherwise the same string is returned on every time. Base 64 text is 1/3 longer than base 256. Both types should be used by a single goroutine at a time: sharing among multiple goroutines requires some kind of synchronization. Give seed value to the Seed func in math/rand package. If you want cryptographically secure random numbers, and the exact charset is flexible (say, base64 is fine), you can calculate exactly what the length of random characters you need from the desired output size. Random numbers are generated by a Source, usually wrapped in a Rand. If nobody can answer that, I'll plot a 2-D scatter plot between the two and find out myself. Package rand implements pseudo-random number generators unsuitable for security-sensitive work. It is equivalent of what I'm doing now, and it doesn't answer my real question, "Regarding randomness of the full 8-digits, would the two be the same, or there is really one better than the other? " Please do not provide low + rand(hi-low) as the answer, as everyone knows that. The vowels and consontants are wighted by the frequency table. The string alternates between vowels and consontants. It should be possible to read out loud and send in an email. Is it ture, or it is only a wishful thinking? Regarding randomness of the full 8-digits, would the two be the same, or there is really one better than the other? HumanFriendlyEnglishString generates a random, but human-friendly, string of. I.e., I guess Int63n might be better for my case than Intn. function correctly, in which case the caller must not continue. It reads random numbers from crypto/rand and searches for printable characters. GenerateRandomASCIIString returns a securely generated random ASCII string. So I'm thinking, for this specific problem, would the following be the fastest and simplest way to do it? 99999999 - rand.Int63n(90000000) How to generate secure random strings in golang with crypto/rand. I.e., I care about the the quality of the randomness more in the sense of its length. Finally, we printed a random string of length 10 in the main function. The randomString () is a convenience function that calls stringWithCharset with the default character set. Say to generate 8-digits long numbers, the problem with rand.Intn(100000000) is that the result might be far less than 8-digits, and padding it with leading zeros doesn't look like a good answer to me. Using the provided character set, the stringWithCharset () function generates a random string of a given length. You could even replace by +3 or more to reduce even more the possibility for an iteration, but then you would loose in performance at the chained replace calls.What is the fastest and simplest way to generate fixed length random numbers in Go? Note that chaining replace turns out to be faster than sequntially calling it, as per this answer.Īlso, in the above, +1 is replaced by +2 when determining rand_lento reduce the number of iterations needed to achieve the requested length. If you put randomness above performance, you could do something like: def secure_random_alphanumeric(str_len: int) -> str: Since you need a "very quick way to generate an alphanumeric", this function sacrifices performance over security, since the frequencies of a and A (or whatever characters you choose to replace + and / with) will be increased compared to what urandom would give you otherwise. Return base64.b64encode(os.urandom(rand_len), altchars=b'aA').decode('ascii') Example-3: Pre-define custom requirement for random string. Example-2: Generate random string with alphanumeric characters only. Method-2: Generate random string using rand.Read () Example-1: Generate random string with integers only. Im looking for something that gives me IDs similar to what Stripe (or others) use, that look like: 'id': 'ch19iRv22eZvKYlo2CAxkjuHxZ' that are as short as possible while still containing only alphanumeric characters. You can then read as much as you like from urandom and produce a random alphanummeric as follows: import mathĭef random_alphanumeric(str_len: int) -> str: Method-1: Generate random string from pre-defined characters. I know theres genrandomuuid() for UUIDs, but I dont want to use them in this case. Os.urandom, which reads from urandom, is considered secure (see the relevant answer in a question if urandom is secure). I would prefer urandom over secrets.token_hex, as it samples from a richer character set and hence needs a smaller length to achieve the same entropy.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |