简体   繁体   English

处理异常的Crypto.randomBytes是否对熵不足?

[英]Crypto.randomBytes handling exception do to inadequate entropy?

In the documentation for this method it states that it will throw an exception if there is an inadequate amount of entropy to generate the data. 在该方法的文档中,它声明如果生成数据的熵量不足,它将抛出异常。 My question pertains to the entropy. 我的问题与熵有关。 How is it generated and can you prevent an exception from being thrown by providing adequate entropy? 如何生成并且可以通过提供足够的熵来防止异常被抛出? How common will an exception be thrown, or is it unknown? 抛出异常会有多常见,还是未知?

Documentation for crypto.randomBytes : crypto.randomBytes文档:

crypto.randomBytes(size, [callback]) crypto.randomBytes(size,[callback])

// async
crypto.randomBytes(256, function(ex, buf) {
  if (ex) throw ex;
  console.log('Have %d bytes of random data: %s', buf.length, buf);
});

Generates cryptographically strong pseudo-random data. 生成加密强大的伪随机数据。

Will throw error or invoke callback with error, if there is not enough accumulated entropy to generate cryptographically strong data . 如果没有足够的累积熵来生成加密强数据,则会抛出错误或调用带有错误的回调 In other words, crypto.randomBytes without callback will not block even if all entropy sources are drained. 换句话说,即使所有熵源都耗尽,没有回调的crypto.randomBytes也不会阻塞。

In the following example, how would I handle an exception properly and still completely fill the array, basically ensuring the array has been filled completely with the generated bytes. 在下面的示例中,我将如何正确处理异常并仍然完全填充数组,基本上确保数组已完全填充生成的字节。 Would I just catch the exception and generate a new array within the catch block, but would if that also throws an exception? 我是否只是捕获异常并在catch块中生成一个新数组,但是如果它也会引发异常吗? Essentially how would I make this code work properly 100% of the time? 基本上我将如何使这段代码100%正常工作?

var codes = [];
for(var i = 0;i < 100;i++){
     (function(i){
          crypto.randomBytes(256, function(ex, buf) {
               if (ex) throw ex;
               codes[i] = buf.toString('hex');
          });
     })(i)
}

If no entropy is available, your best bet would be to wait a bit and try again. 如果没有可用的熵,你最好的选择是等一下再试一次。 How long you'd need to wait depends on how much entropy you need and how the underlying entropy sources work. 您需要等待多长时间取决于您需要多少熵以及底层熵源如何工作。

In practice, I doubt that you'll have any problems. 在实践中,我怀疑你会有任何问题。 I don't know what Node.js does under the covers, equivalent functions in other libraries are generally implemented as calls to the OS's entropy pool - eg /dev/urandom or CryptGenRandom() - or as CSPRNGs that are seeded from the OS's entropy pool. 我不知道Node.js在幕后做了什么,其他库中的等效函数通常被实现为对OS的熵池的调用 - 例如/dev/urandomCryptGenRandom() - 或者作为从OS的熵中播种的CSPRNG池。 In either case, you'll never block. 在任何一种情况下,你永远不会阻止。

Blocking is only an issue if you're reading from /dev/random on Linux. 如果你在Linux上读取/dev/random ,阻塞只是一个问题。 This is because /dev/random may block on Linux, but doesn't on other platforms. 这是因为/dev/random可能在Linux上阻塞,但在其他平台上不会阻止。 It could also be an issue if you're reading directly from a hardware RNG. 如果您直接从硬件RNG读取,也可能是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM