In 这个问题 https://stackoverflow.com/questions/8838624/nodejs-send-email-on-registration/8842959#8842959Erik 需要在 Node.js 中生成一个安全的随机令牌。有这个方法crypto.randomBytes
生成一个随机缓冲区。然而,node中的base64编码不是url安全的,它包括/
and +
代替-
and _
。因此,我发现生成此类令牌的最简单方法是
require('crypto').randomBytes(48, function(ex, buf) {
token = buf.toString('base64').replace(/\//g,'_').replace(/\+/g,'-');
});
有更优雅的方式吗?
Try crypto.randomBytes() https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback:
require('crypto').randomBytes(48, function(err, buffer) {
var token = buffer.toString('hex');
});
“十六进制”编码适用于节点 v0.6.x 或更高版本。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)