Generate a prime using OpenSSL

How can I generate a large random number using openssl, I found out how to generate a random number and check if it is simple, but I could not automate the primitiveness check process, here is the command that I use: openssl rand -hex 256 | xargs openssl prime -hex openssl rand -hex 256 | xargs openssl prime -hex

Should I use a while loop to re-check if the result is simple? How can I automate the verification process if the result does not contain the keyword "not",

This is all the more so since I wrote a while loop:

while [{openssl rand -hex 256 | xargs openssl prime -hex} = *"$not"*]

+6
source share
2 answers

OpenSSL version 1.0.0 and later adds the -generate to the prime command:

 $ openssl prime -generate -bits 2048 -hex D668FDB1968891AE5D858E641B79C4BA18ABEF8C571CBE004EA5673FB3089961E4670681B794063592124D13FF553BBD5CCC81106A9E5F7D87370DD5DA6342B1DAC13CD2E584759CDEC3E76AEFB799848E48EA9C218F53FE3103E1081B8154AD41DDCB931175853FE3D433CECD886B4D94C211EAE01AE5EA93F8FBD6812A9DEF0308378EE963B3C39F80865BA0E1D957683F4ED77ADA9812091AA42E9A56F43C37185223FF9E3DD03C312E71DED072E5686873B3CA6F5F575C569FB0A10CFEA17D7FEB898A8A02549FF6E4B7A1FBCE78656D3DCF227318EEEF8E601C23AA32DF41A61F04D39FC752F70A809D636238340B7B929F0CDBA629F7DE6AAAC44D2BA5 
+10
source

There are better ways to generate prime numbers than using openssl.

If you are really tuned for this method, use something like this (a call with a range number to check):

 #!/bin/bash # Usage: $0 <starting_number> <final_number> N=$1 while (( N <= $2 )); do # use bc to convert hex to decimal openssl prime $N | awk '/is prime/ {print "ibase=16;"$1}' | bc let N++ done 

If you want to do this with random numbers generated using openssl, use this (call with the number of attempts):

 #!/bin/bash # Usage: $0 <count> N=$1 while (( N-- > 0 )); do # use bc to convert hex to decimal openssl rand -hex 256 | xargs openssl prime -hex | awk '/is prime/ {print "ibase=16;"$1}' | bc done 

If you don't care about the decimal value, replace awk '/is prime/ {print "ibase=16;"$1}' | bc awk '/is prime/ {print "ibase=16;"$1}' | bc on awk '/is prime/ {print $1}'

Adapted from: http://www.madboa.com/geek/openssl/#prime

-1
source

Source: https://habr.com/ru/post/957204/


All Articles