使用安全程序生成密码的最佳方法是什么?在Python中,我可以简单地使用os.urandom。对此有什么建议吗?
_ = require('underscore')
exports.gen = (n=10) ->
throw new Error 'Not a number!' if typeof(n) isnt 'number'
throw new Error 'Prefered (n): [10..128]' if n < 10 or n > 128
chars = (String.fromCharCode(i) for i in [33..126]).join ''
_.times(n, -> chars[(Math.random() * chars.length) | 0]).join ''发布于 2014-08-15 14:32:26
在密码模块:os.urandom中找到了与crypto.randomBytes等价的内容。该模块还包含了您可能需要的密码哈希等大多数其他功能。
若要使用与当前代码相同的密码字符集,可以执行以下操作
exports.gen = (length = 10) ->
throw new Error 'Length is not a number!' if typeof length isnt 'number'
throw new Error 'Length must be [10..128]' unless 10 <= length <= 128
range = 126 - 33
buffer = require('crypto').randomBytes length # note: may throw an error
(String.fromCharCode(33 + (range * c / 255) | 0) for c in buffer).join ''我改变了一些小事情
length而不是n10 <= length <= 128。https://codereview.stackexchange.com/questions/60107
复制相似问题