HMAC 中的 nonce 是否有助于提高安全性、基于种子生成随机数?
Is the nonce in a HMAC useful to increase the security, to generate a random number based on the seed?
为了根据种子生成一个数字,我写了这段代码:
var crypto = require('crypto'),
//generate the crypto random token
clientToken = crypto.createHash("sha256").update(crypto.randomBytes(128)).digest('hex')
serverToken = crypto.createHash("sha256").update(crypto.randomBytes(128)).digest('hex'),
//generate the seed
hmac = crypto.createHmac('sha512', serverToken);
hmac.update(clientToken);
var seed = hmac.digest('hex'),
//generate the random number with a PRNG algorithm
prng = new require("seedrandom")(seed),
random = Math.floor(prng() * (100000000 - 10000)) + 10000;
//log the results
console.log("clientToken: ", clientToken);
console.log("serverToken: ", serverToken);
console.log("Seed : ", seed);
console.log("Random number:", random);
如您所见,我没有 HMAC 随机数值,我想知道消化它是否会增加更多安全性。
这可能是添加了 nonce 实现的更新代码:
//generate the nonce by using a nanosecond timestamp
var hrtime = process.hrtime(),
nonce = (hrtime[0] * 1e9 + hrtime[1]).toString();
nonce = crypto.createHash("sha1").update(nonce).digest('hex');
//generate the seed
var hmac = crypto.createHmac('sha512', serverToken);
hmac.update(clientToken);
hmac.update(nonce);
var seed = hmac.digest('hex');
添加随机数,会增加安全性吗?只知道客户端令牌的用户可以猜出 hmac 种子吗? (有无 nonce 实施)
是的,如果您对随机数生成的信任度不够高而始终生成一个随机数,则可能是这种情况。一般来说,只要方法是安全的,就可以向方案中添加熵。通常这意味着基于散列或 HMAC 的构造。
如果您可以将所有安全性都建立在 serverToken
上,那么添加额外的熵似乎没有多大意义。
为了根据种子生成一个数字,我写了这段代码:
var crypto = require('crypto'),
//generate the crypto random token
clientToken = crypto.createHash("sha256").update(crypto.randomBytes(128)).digest('hex')
serverToken = crypto.createHash("sha256").update(crypto.randomBytes(128)).digest('hex'),
//generate the seed
hmac = crypto.createHmac('sha512', serverToken);
hmac.update(clientToken);
var seed = hmac.digest('hex'),
//generate the random number with a PRNG algorithm
prng = new require("seedrandom")(seed),
random = Math.floor(prng() * (100000000 - 10000)) + 10000;
//log the results
console.log("clientToken: ", clientToken);
console.log("serverToken: ", serverToken);
console.log("Seed : ", seed);
console.log("Random number:", random);
如您所见,我没有 HMAC 随机数值,我想知道消化它是否会增加更多安全性。
这可能是添加了 nonce 实现的更新代码:
//generate the nonce by using a nanosecond timestamp
var hrtime = process.hrtime(),
nonce = (hrtime[0] * 1e9 + hrtime[1]).toString();
nonce = crypto.createHash("sha1").update(nonce).digest('hex');
//generate the seed
var hmac = crypto.createHmac('sha512', serverToken);
hmac.update(clientToken);
hmac.update(nonce);
var seed = hmac.digest('hex');
添加随机数,会增加安全性吗?只知道客户端令牌的用户可以猜出 hmac 种子吗? (有无 nonce 实施)
是的,如果您对随机数生成的信任度不够高而始终生成一个随机数,则可能是这种情况。一般来说,只要方法是安全的,就可以向方案中添加熵。通常这意味着基于散列或 HMAC 的构造。
如果您可以将所有安全性都建立在 serverToken
上,那么添加额外的熵似乎没有多大意义。