【发布时间】:2014-05-06 21:45:18
【问题描述】:
这是我用我自己的Cryptography.random() 替换Math.random() 的尝试。我的代码会生成加密安全的随机数吗?您是否发现有任何优化机会?
NodeCrypto = require('crypto');
Cryptography = function() {
}
Cryptography.random = Promise.method(function() {
return new Promise(function(resolve, reject) {
NodeCrypto.randomBytes(4, function(ex, buffer) {
var hex = buffer.toString('hex');
var integer = parseInt(hex, 16);
var random = Number('0.'+integer);
resolve(random);
return random;
});
});
});
【问题讨论】:
-
我不熟悉node.js,但是"with my own Cryptography.random()"是一个危险信号,here is why
标签: node.js random cryptography