Restrict RAND to producing 63 random bits

Robert Moskowitz rgm at htt-consult.com
Thu Jul 20 20:39:46 UTC 2023



On 7/20/23 14:31, Robert Moskowitz wrote:
> This seems to generate a random number between 1 and 2^23-1
>
> And print a hex
>
> printf "%X\n" $(shuf -rn 1 -i 1-9223372036854775807 
> --random-source=/dev/urandom)
>
> I don't know if I can trust this as a real random number, but it IS 
> taking its source from urandom.  Plus it is "only" for serial number thus
>
> printf "%X\n" $(shuf -rn 1 -i 1-9223372036854775807 
> --random-source=/dev/urandom) > serial
>
> Works for now until someone shows me something better.  Good way to 
> spend a couple hours with professor Google...

To get the leading zeros so it will generate an 8-byte serial number, I 
added:

printf "%016X\n" $(shuf -rn 1 -i 1-9223372036854775807 
--random-source=/dev/urandom)

an example output is:

0E12E1A664C30AE1

I had to run it a couple dozen times before it generated a small enough 
value to need to pad it.
>
> On 7/20/23 10:53, Robert Moskowitz wrote:
>> Given that the serialNumber encoding adds a 0x00 in front if the 
>> first bit of the serialNumber is a '1', how can I use the RAND 
>> function to limit itself to 63 bits rather than an 8-byte via:
>>
>> openssl rand -hex 8 > serial
>>
>> Other than pumping the output into a script that forces the 
>> high-order bit to zero.
>>
>> thanks
>>
>



More information about the openssl-users mailing list