如何在Ruby和openssl中使用rdrand?

时间:2013-09-12 12:14:41

标签: ruby random openssl

我在/ opt / openssl中安装了一个新的OpenSSL安装(从源代码编译的1.1.0),还有一个Ruby安装,也是/ opt / ruby​​中的新编译(来自源代码的2.1.0dev),用--with-编译openssl-dir = / opt / openssl(所有在当前的Debian上)。 Openssl可以看到我的计算机的rdrand引擎

$ openssl engine
(rdrand) Intel RDRAND engine
(dynamic) Dynamic engine loading support

和Ruby可以看到openssl

$ ruby -ropenssl -e 'p OpenSSL::Random.random_bytes(4)'
"Q\a\"%"

OpenSSL :: Engine就在那里,也可以加载引擎:

$ ruby -ropenssl -e 'e=OpenSSL::Engine.by_id("openssl"); p e;'
#<OpenSSL::Engine id="openssl" name="Software engine support">

如果我现在尝试使用该设置来调用rdrand生成器,我会得到以下结果:

$ ruby -ropenssl -e 'OpenSSL::Engine.by_id("rdrand"); p OpenSSL::Random.random_bytes(4)'
-e:1:in `by_id': no such engine (OpenSSL::Engine::EngineError)
from -e:1:in `<main>'

我在这里做些蠢事吗?一般情况:我如何在Ruby中使用OpenSSL并需要特定的openssl引擎?

1 个答案:

答案 0 :(得分:2)

找到它,你必须先加载所有引擎:

$ ruby -ropenssl -e 'OpenSSL::Engine.load; e = OpenSSL::Engine.by_id("rdrand"); p e;'
#<OpenSSL::Engine id="rdrand" name="Intel RDRAND engine">

实际上非常简单。