@@ -31,44 +31,64 @@ def gen_random_password(length, complexity=nil, complex_only=false, timeout_seco
3131 require 'timeout'
3232 passwd = ''
3333 Timeout ::timeout ( timeout_seconds ) do
34- default_charlist = ( 'a' ..'z' ) . to_a + ( 'A' ..'Z' ) . to_a + ( '0' ..'9' ) . to_a
35- specific_charlist = nil
34+ lower_charlist = ( 'a' ..'z' ) . to_a
35+ upper_charlist = ( 'A' ..'Z' ) . to_a
36+ digit_charlist = ( '0' ..'9' ) . to_a
37+ symbol_charlist = nil
3638 case complexity
3739 when 1
38- specific_charlist = [ '@' , '%' , '-' , '_' , '+' , '=' , '~' ]
40+ symbol_charlist = [ '@' , '%' , '-' , '_' , '+' , '=' , '~' ]
3941 when 2
40- specific_charlist = ( ' ' ..'/' ) . to_a + ( '[' ..'`' ) . to_a + ( '{' ..'~' ) . to_a
42+ symbol_charlist = ( ' ' ..'/' ) . to_a + ( '[' ..'`' ) . to_a + ( '{' ..'~' ) . to_a
4143 else
4244 end
4345
44- unless specific_charlist . nil?
46+ unless symbol_charlist . nil?
4547 if complex_only == true
4648 charlists = [
47- specific_charlist
49+ symbol_charlist
4850 ]
4951 else
5052 charlists = [
51- default_charlist ,
52- specific_charlist
53+ lower_charlist ,
54+ upper_charlist ,
55+ digit_charlist ,
56+ symbol_charlist
5357 ]
5458 end
5559
5660 else
5761 charlists = [
58- default_charlist
62+ lower_charlist ,
63+ upper_charlist ,
64+ digit_charlist
5965 ]
6066 end
6167
62- index = 0
68+ last_list_rand = nil
69+ last_char_rand = nil
6370 Integer ( length ) . times do |i |
64- passwd += charlists [ index ] [ rand ( charlists [ index ] . length -1 ) ]
65- index += 1
66- index = 0 if index == charlists . length
71+ rand_list_index = rand ( charlists . length ) . floor
72+
73+ if rand_list_index == last_list_rand
74+ rand_list_index = rand_list_index -1
75+ end
76+
77+ last_list_rand = rand_list_index
78+
79+ rand_index = rand ( charlists [ rand_list_index ] . length ) . floor
80+
81+ if rand_index == last_char_rand
82+ rand_index = rand_index -1
83+ end
84+
85+ passwd += charlists [ rand_list_index ] [ rand_index ]
86+
87+ last_char_rand = rand_index
6788 end
6889 end
6990
7091 return passwd
7192 end
72-
7393end
7494# vim: set expandtab ts=2 sw=2:
0 commit comments