我目前正在为“A”生成一个8字符伪随机大写字符串。。“Z”:
value = ""; 8.times{value << (65 + rand(25)).chr}
但它看起来不干净,而且不能作为参数传递,因为它不是一个单独的语句。要获取混合大小写字符串“a”。。“z”加“A”。。“Z”,我将其更改为:
value = ""; 8.times{value << ((rand(2)==1?65:97) + rand(25)).chr}
但它看起来像垃圾。
有人有更好的方法吗?
我目前正在为“A”生成一个8字符伪随机大写字符串。。“Z”:
value = ""; 8.times{value << (65 + rand(25)).chr}
但它看起来不干净,而且不能作为参数传递,因为它不是一个单独的语句。要获取混合大小写字符串“a”。。“z”加“A”。。“Z”,我将其更改为:
value = ""; 8.times{value << ((rand(2)==1?65:97) + rand(25)).chr}
但它看起来像垃圾。
有人有更好的方法吗?
当前回答
我只是编写了一个小gemrandom_token来为大多数用例生成随机令牌,享受~
https://github.com/sibevin/random_token
其他回答
我喜欢使用的另一种方法:
rand(2**256).to_s(36)[0..7]
如果您真的对正确的字符串长度有疑问,请添加ljust:
rand(2**256).to_s(36).ljust(8,'a')[0..7]
我最喜欢的是(:A..:Z).to_A.shuff[0,8].join。请注意,shuffle需要Ruby>1.9。
从Ruby 2.5开始,使用SecureRandom.andlettery非常简单:
len = 8
SecureRandom.alphanumeric(len)
=> "larHSsgL"
它生成包含A-Z、A-Z和0-9的随机字符串,因此应适用于大多数用例。它们是随机安全生成的,这可能也是一个好处。
这是一个基准,用于将其与支持率最高的解决方案进行比较:
require 'benchmark'
require 'securerandom'
len = 10
n = 100_000
Benchmark.bm(12) do |x|
x.report('SecureRandom') { n.times { SecureRandom.alphanumeric(len) } }
x.report('rand') do
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten
n.times { (0...len).map { o[rand(o.length)] }.join }
end
end
user system total real
SecureRandom 0.429442 0.002746 0.432188 ( 0.432705)
rand 0.306650 0.000716 0.307366 ( 0.307745)
因此,rand解决方案只需要SecureRandom时间的3/4。如果您生成大量字符串,这可能很重要,但如果您只是时不时地创建一些随机字符串,我会始终使用更安全的实现,因为它也更容易调用,更显式。
请注意:rand对于攻击者来说是可预测的,因此可能不安全。如果这是用于生成密码的,则绝对应该使用SecureRandom。我用的是这样的东西:
length = 10
characters = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
password = SecureRandom.random_bytes(length).each_char.map do |char|
characters[(char.ord % characters.length)]
end.join
如果需要,创建空字符串或预修复:
myStr = "OID-"
使用以下代码用随机数填充字符串:
begin; n = ((rand * 43) + 47).ceil; myStr << n.chr if !(58..64).include?(n); end while(myStr.length < 12)
笔记:
(rand * 43) + 47).ceil
它将从48-91(0,1,2..Y,Z)生成随机数
!(58..64).include?(n)
它用于跳过特殊字符(因为我不想包含它们)
while(myStr.length < 12)
它将生成总共12个字符的字符串,包括前缀。
样本输出:
"OID-XZ2J32XM"