在该方法的文档中,它声明如果生成数据的熵量不足,它将抛出异常.我的问题与熵有关.如何生成并且可以通过提供足够的熵来防止异常被抛出?抛出异常会有多常见,还是未知?
crypto.randomBytes的文档:
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@H_502_17@ accumulated entropy to generate cryptographically strong data. In@H_502_17@ other words,crypto.randomBytes without callback will not block even@H_502_17@ if all entropy sources are drained.
在下面的示例中,我将如何正确处理异常并仍然完全填充数组,基本上确保数组已完全填充生成的字节.我是否只是捕获异常并在catch块中生成一个新数组,但是如果它也会引发异常?基本上我将如何使这段代码100%正常工作?
var codes = []; for(var i = 0;i < 100;i++){ (function(i){ crypto.randomBytes(256,buf) { if (ex) throw ex; codes[i] = buf.toString('hex'); }); })(i) }