-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add random.sampleBuffer #16957
Add random.sampleBuffer #16957
Conversation
@timotheecour Can you review; thanks. |
I agree, if you are a Nim core dev is very easy to implement, optimize and test, one more reason why it should be implemented,
:) |
how about the following instead, which is more generic but not harder to use (compared a version of your PR that would add proc sampleBuffer*[T](r: var Rand, buffer: var openArray[T]; alphabet: set[T]) {.inline, since: (1, 5).} =
runnableExamples:
import sugar
doAssert newString(8).dup(sampleBuffer(randState, _, {'0'..'9'})).len == 8
import base64
doAssert newString(8).dup(sampleBuffer(randState, _, cb64safe)).len == 8
for c in buffer.mitems: c = sample(r, alphabet) pros/cons:
|
Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
AFAICT your procs implementation is a one liner. The most complex part of this PR is the alphabet, so adding this makes even less sense IMO |
I don't mind a Python inspired "secrets" module but random.nim isn't it: Do not use this module for cryptographic purposes! |
@Araq You dont like the proc ?, or you want it to be on a new module ?. |
New module but ensure it's good enough for crypto. |
Just like the secrets module of Python, it should be based on system random |
@xflywind how about instead keeping it in std/random, but changing # CSPRNG
var r = initRand(opt = useSysrand)
sampleBuffer(r, buf, alphabet)
# PRNG
var r = initRand(opt = useTime)
sampleBuffer(r, buf, alphabet)
# CSPRNG
randomize() # could be changed to: randState = initRand(opt = useSysrand)
sampleBuffer(randState, buf, alphabet) # uses default RNG |
random.randToken
.secrets.token_urlsafe()
set[char]
.runnableExamples
,since
, changelog, tiny diff.Notes
python3 -c "print(__import__('secrets').token_urlsafe())"