bash 使用 OpenSSL 生成素数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19732162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:28:46  来源:igfitidea点击:

Generate prime number using OpenSSL

bashopensslprimes

提问by user2835948

How can I generate a large random prime using openssl, I found out how to generate a random number and check if it is prime but I have not been able to automate the process of checking the primality, here is the command that i am using: openssl rand -hex 256 | xargs openssl prime -hex

我如何使用 openssl 生成一个大的随机素数,我发现了如何生成一个随机数并检查它是否是素数,但我无法自动化检查素数的过程,这是我正在使用的命令: openssl rand -hex 256 | xargs openssl prime -hex

Should I use a while loopto repeatedly check if the result is prime? How can I automate the process of checking if the result does not contain the keyword "not",

我是否应该使用while 循环反复检查结果是否为素数?如果结果不包含关键字“not”,我如何自动执行检查过程,

This is all the further i got on writing the while loop:

这是我在编写 while 循环时得到的更多信息:

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

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

回答by mykhal

OpenSSL version 1.0.0 and newer adds -generateoption to the primecommand:

OpenSSL 1.0.0 及更新版本-generateprime命令添加了选项:

$ openssl prime -generate -bits 2048 -hex
D668FDB1968891AE5D858E641B79C4BA18ABEF8C571CBE004EA5673FB3089961E4670681B794063592124D13FF553BBD5CCC81106A9E5F7D87370DD5DA6342B1DAC13CD2E584759CDEC3E76AEFB799848E48EA9C218F53FE3103E1081B8154AD41DDCB931175853FE3D433CECD886B4D94C211EAE01AE5EA93F8FBD6812A9DEF0308378EE963B3C39F80865BA0E1D957683F4ED77ADA9812091AA42E9A56F43C37185223FF9E3DD03C312E71DED072E5686873B3CA6F5F575C569FB0A10CFEA17D7FEB898A8A02549FF6E4B7A1FBCE78656D3DCF227318EEEF8E601C23AA32DF41A61F04D39FC752F70A809D636238340B7B929F0CDBA629F7DE6AAAC44D2BA5

回答by Sir Athos

There are better ways of generating prime numbers than by using openssl.

有比使用 openssl 更好的生成素数的方法。

If you are really set on this method though, use something like this (call with a number range to be checked):

但是,如果您真的设置了此方法,请使用类似的方法(使用要检查的数字范围调用):

#!/bin/bash
# Usage: 
#!/bin/bash
# Usage: ##代码## <count>
N=
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;"}' | bc
done
<starting_number> <final_number> N= while (( N <= )); do # use bc to convert hex to decimal openssl prime $N | awk '/is prime/ {print "ibase=16;"}' | bc let N++ done

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

如果您想使用 openssl 生成的随机数执行此操作,请使用此命令(使用尝试次数调用):

##代码##

If you don't care for decimal, replace awk '/is prime/ {print "ibase=16;"$1}' | bcwith awk '/is prime/ {print $1}'

如果您不关心小数,请替换awk '/is prime/ {print "ibase=16;"$1}' | bcawk '/is prime/ {print $1}'

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

改编自:http: //www.madboa.com/geek/openssl/#prime