我正在尝试使用ruby生成一个包含特殊字符的随机密码。我想知道是否有一个标准来生成这样的密码。我曾考虑使用加权概率分布和分配权重,以便有更高的概率从中挑选特殊字符,但我不确定这是否是一个被广泛接受的标准。
发布于 2016-09-08 02:21:17
您可以使用SecureRandom (docs):
require 'securerandom'
password = SecureRandom.base64(15)
# => "vkVuWvPUWSMcZf9nn/dO"发布于 2016-09-08 02:22:08
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=="现在有一个上面的密码安全版本,称为SysRandom,一些人are recommending。
使用gem simple-password-gen,您还可以生成random and pronounceable passwords
require "simple-password-gen"
Password.random 8 # => "#TFJ)Vtz3"
Password.pronounceable 13 # => "vingastusystaqu"最后,为了好玩(我推荐SysRandom),我在generate random strings based on template strings上写了一小段时间。虽然它不包括特殊字符,但它将是一个微不足道的补充。如果你对它感兴趣,请随时为它提交一个问题。
发布于 2021-08-31 14:11:32
从Ruby2.5开始,ruby内置的SecureRandom模块就有了方便的方法。
require "securerandom"
# If you need A-Za-z0-9
SecureRandom.alphanumeric(10)
# If you want to specify characters (excluding similar characters)
# However, this method is NOT PUBLIC and it might be changed someday.
SecureRandom.send(:choose, [*'A'..'Z', *'a'..'z', *'0'..'9'] - ['I', 'l', '1', 'O', '0'], 10)
# Old ruby compatible version
chars = [*'A'..'Z', *'a'..'z', *'0'..'9']
10.times.map { chars[SecureRandom.random_number(chars.length)] }.joinhttps://stackoverflow.com/questions/39376428
复制相似问题