I'm trying to generate a 2048 bit long prime number, this is my code so far:
#!/bin/bash
generate_random() {
hex=$(head -c 256 /dev/urandom | xxd -p)
bc <<< "ibase=16; $hex"
}
p=$(generate_random)
echo "$p"
While running the script I get (standard_in) 1: syntax error
followed by random zeroes.
Anyone knows what is causing this error and how can I fix it? I've tried with bash -x
, but it doesn't add any useful information.
First,
bc
understands only upper-case letters as hex digits (at least by default). Second, you have separators in yourxxd
output, so you generate multiple numbers withbc
later.This should work:
-u
flag toxxd
instructs it to output upper-case letters as digits, andtr
removes separators.Example output:
To remove newline separators and backslashes, you can do
instead.