Rich <ri...@example.invalid> writes:
> Cecil Westerhof <
Ce...@decebal.nl> wrote:
>> I am going to write a function to generate an UUID on Linux. For this
>> I need to generate random integers. At the moment I have the
>> following:
>> proc getRandomLnx {{secure False}} {
>
>> if ${secure} {
>> set randFile /dev/random
>> } else {
>> set randFile /dev/urandom
>> }
>
> This is unnecessary, just use /dev/urandom
> (
https://www.2uo.de/myths-about-urandom/). The Linux manpage is
> incorrect in their implication that 'random' generates 'more random'
> values. The same generator is used for both, the only difference is
> 'random' will block when its 'estimate' of entropy is too low (and this
> is simply a black magic 'estimate' without much foundation in
> reality).
That is why default secure is False. I do not want to block, but when
the randomness is really important (encryption, passwords, …) I go for
random with secure is True.
>
>> set randDev [open ${randFile} rb]
>> set random [read ${randDev} 8]
>> binary scan ${random} H8 randomX
>> set randomI [scan ${randomX} %x]
>> close ${randDev}
>> return ${randomI}
>> }
>>
>> Is this a good way, or is there a better way?
>
> There is no need to binary scan then scan. If you want a 32 bit
> integer, use the i or I conversions of 'binary scan'. If you want the
> integer to be unsigned, add the 'u' qualifier.
OK, thanks: I will look into that.
>> For the moment 32 bits is good enough.
>
> Not for a proper type 4 UUID (note, I'm presuming the above is headed towards
> UUID generation due to your other thread).
Not only, but also yes.
The way I build the UUID I work with random values with 16 and 12
bits, so 32 bits is more as enough.
I already made the 64 bits version also. I was thinking that a 32 bits
version would be a good idea because in most cases it is enough. And
it would not exhaust /dev/random.
>> But probably a good idea to add a parameter that I want a 63 bits
>> version instead of a 32 bits version.
>
> In which case, look at the w or W conversion available from binary
> scan.
I will do that.