-
Notifications
You must be signed in to change notification settings - Fork 122
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
ref: use SmallRng #119
ref: use SmallRng #119
Conversation
Nice! |
.sample_iter(&Alphanumeric) | ||
.take(rand_len) | ||
.collect::<String>() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.sample_iter(&Alphanumeric) | |
.take(rand_len) | |
.collect::<String>() | |
.sample_iter(Alphanumeric) | |
.take(rand_len) | |
.map(char::from) | |
.collect::<String>() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's still going to allocate, unfortunately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look for a way to avoid the allocation.
EDIT: See #119 (comment)
In addition to the compile issue above, I'm a bit concerned about calling |
One solution would be to put a smallrng in a thread-local variable. |
Oh yeah, I don't remember considering performance at the time at all. You can also close this if you'd like. |
let small_rng = SmallRng::from_entropy(); | ||
buf.push(small_rng | ||
.sample_iter(&Alphanumeric) | ||
.take(rand_len) | ||
.collect::<String>() | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can avoid allocation by using char::encode_utf8.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=421dfa2eebe631075a03b06e08c6e7cf
let small_rng = SmallRng::from_entropy(); | |
buf.push(small_rng | |
.sample_iter(&Alphanumeric) | |
.take(rand_len) | |
.collect::<String>() | |
); | |
let mut tmp = [0; 1]; | |
let small_rng = SmallRng::from_entropy(); | |
small_rng | |
.sample_iter(Alphanumeric) | |
.take(rand_len) | |
.for_each(|c| buf.push((c as char).encode_utf8(&mut tmp))); |
Can we probably resolve performance issues in combination with putting a smallrng in a thread-local variable?
Merged in #141. |
I have concerns about using a non-CSPRNG to generate temporary file names. For example, the Cert C Coding standard on temporary files warns that
This OWASP page also goes into great detail of the potential security implications of using temporary files with predictable names. The documentation for |
Yeah, fair enough. Using I'm going to try to make using insecure rand opt-in... but I'm having some trouble with the feature specification. |
Alternative to #118.
My understanding is that SmallRng is much smaller and cheaper to initialize than the thread_rng, which is a thread-local ThreadRng (StdRng, with the added overhead of ReseedingRng because it needs to be secure). SmallRng is also faster than StdRng. This is all because SmallRng is not a CSPRNG.
It's worth noting that unreleased rand at this time also shakes rand_chacha from the dependency tree, which is really nice. I believe rand's planning on releasing 0.8, so if this PR looks good, I'd recommend blocking on that. Issues: rust-random/rand#965, rust-random/rand#938.