Home > OS >  Syntax error while trying to generate a 2048bit long prime number
Syntax error while trying to generate a 2048bit long prime number

Time:12-30

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.

CodePudding user response:

First, bc understands only upper-case letters as hex digits (at least by default). Second, you have separators in your xxd output, so you generate multiple numbers with bc later.

This should work:

#!/bin/bash

generate_random() {
    hex=$(head -c 256 /dev/urandom | xxd -p -u | tr -d '\n')
    bc <<< "ibase=16; $hex"
}

p=$(generate_random)
echo "$p"

-u flag to xxd instructs it to output upper-case letters as digits, and tr removes separators.

Example output:

84404284040092528807148386035025161100484110236893077703095592941720\
00537078513504880246726730474236368181068985417211434943913923235822\
01284401417146606673073772989889733010524123703686975444423088406509\
44767677616371794606797386146855833950295071249000795855185540560405\
62673903614333076371092344026999031152809898928396395497832309795471\
93897215963003601022703133486344387720277877558264139632520964120681\
97764906669023878701319760947789227343517474218584987497204300184084\
62846775760153647010072072799120566180042021620262646969602253704108\
06274157727080642084167983313757899766696995668747042179553171962777\
5716

To remove newline separators and backslashes, you can do

p_joined=$(echo "$p" | sed -z 's=\\\n==g')
echo "$p_joined"

instead.

CodePudding user response:

An alternative way might be

printf -v hex '%s' $(od -v -An -N256 -x /dev/urandom)
read dec < <(bc <<< "ibase=16; ${hex^^}")
echo $dec
  • Related