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,
bcunderstands only upper-case letters as hex digits (at least by default). Second, you have separators in yourxxdoutput, so you generate multiple numbers withbclater.This should work:
-uflag toxxdinstructs it to output upper-case letters as digits, andtrremoves separators.Example output:
To remove newline separators and backslashes, you can do
instead.