use*_*636 3 ruby security password-generator
我正在尝试使用ruby生成包含特殊字符的随机密码.我想知道是否有生成此类密码的标准.我考虑过使用加权概率分布并指定权重,以便从中挑选特殊字符的可能性更高,但我不确定这是否是一个被广泛接受的标准.
Die*_*zar 10
Ruby带有这样一个模块SecureRandom.您可以生成随机字符串:
require "securerandom"
SecureRandom.hex 1 # => "e1"
SecureRandom.hex 2 # => "dcdd"
SecureRandom.hex 3 # => "93edc6"
SecureRandom.hex 5 # => "01bf5657ce"
SecureRandom.hex 8 # => "3cc72f70146ea286"
SecureRandom.base64 2 # => "d5M="
SecureRandom.base64 3 # => "EJ1K"
SecureRandom.base64 5 # => "pEeGO68="
SecureRandom.base64 8 # => "muRa+tO0RqU="
SecureRandom.base64 13 # => "1f8y7xsvaCEw0hwkjg=="
Run Code Online (Sandbox Code Playgroud)
现在有一个上面称为SysRandom的加密安全版本,有人建议使用.
使用gem simple-password-gen您还可以生成随机且可发音的密码:
require "simple-password-gen"
Password.random 8 # => "#TFJ)Vtz3"
Password.pronounceable 13 # => "vingastusystaqu"
Run Code Online (Sandbox Code Playgroud)
最后,为了好玩(我推荐SysRandom),我稍后写了一个小宝石,根据模板字符串生成随机字符串.虽然它不包括特殊字符,但它将是一个微不足道的补充.如果您感兴趣,请随时提交问题.
您可以使用SecureRandom(docs):
require 'securerandom'
password = SecureRandom.base64(15)
# => "vkVuWvPUWSMcZf9nn/dO"
Run Code Online (Sandbox Code Playgroud)