-
-
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
oids: switch from PRNG to random module #16203
Conversation
Could it be easy to convert cstring usage to strings, or at least openarray[char]? |
I don't like |
use this (I checked, it works): proc toStringImpl*[T: string | cstring](result: var T, oid: Oid) =
## Converts an oid to `str`.
type T = typeof(result)
const hex = "0123456789abcdef"
const N = 24
when T is string:
result.setLen N
var o = oid
var bytes = cast[cstring](addr(o))
var i = 0
while i < 12:
let b = bytes[i].ord
result[2 * i] = hex[(b and 0xF0) shr 4]
result[2 * i + 1] = hex[b and 0xF]
inc(i)
when T is cstring:
result[N] = '\0'
proc oidToString*(oid: Oid, str: cstring) {.deprecated: "unsafe; use `$`".} =
## assumes str is space allocated for 25 elements.
# work around a compiler bug:
var str = str
toStringImpl(str, oid)
proc `$`*(oid: Oid): string =
## Converts an oid to string.
toStringImpl(result, oid) (refs #16201 (comment)) |
@xflywind this PR introduces a regression: when true:
import std/oids
import std/random
import std/sugar
proc main()=
let s = collect(newSeq):
for i in 0..<10: rand(int.high)
echo s
main()
before PR:
I recommend splitting this PR in 2:
|
The regression has been fixed. |
The random module is not cryptographically secure. |
Since original implementation using |
Can this be merged as is? It's already an improvement. |
* switch from PRNG to random module * fix the regression * comments + tests * runnableExamples * make oids better
* switch from PRNG to random module * fix the regression * comments + tests * runnableExamples * make oids better
* switch from PRNG to random module * fix the regression * comments + tests * runnableExamples * make oids better (cherry picked from commit 89a21e4)
* switch from PRNG to random module * fix the regression * comments + tests * runnableExamples * make oids better (cherry picked from commit 89a21e4)
0x7fff
is enough to use and is often used asRAND_MAX
in PRNG.http://www.cplusplus.com/reference/cstdlib/RAND_MAX
genOid
which acceptsRand
as a parameter.Benefits:
random
moduleinitRand
,next
) and could be used to improvegenOid